mitao
2024-06-21 ee9688b912bb993b54252a26f8ad9e0e04df21c3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.ruoyi.system.listener;
 
 
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.constants.DelayTaskEnum;
import com.ruoyi.system.api.feignClient.AuctionClient;
import com.ruoyi.system.api.feignClient.GoodsSkuClient;
import java.util.Date;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
 
 
/**
 * @author mitao
 * @date 2024/5/22
 */
@Slf4j
@Component
public class RedisListener extends KeyExpirationEventMessageListener {
 
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private GoodsSkuClient goodsSkuClient;
    @Resource
    private AuctionClient auctionClient;
    public RedisListener(RedisMessageListenerContainer listenerContainer,
                         RedisTemplate redisTemplate) {
        super(listenerContainer);
        this.redisTemplate=redisTemplate;
    }
 
    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
        String expiredKey = message.toString();
        log.info("RedisListener key={}", expiredKey);
        String time= DateUtils.dateTime(new Date());
        try {
            if(StringUtils.isNotBlank(expiredKey)){
                if(expiredKey.contains("-")){
                    String[] split = expiredKey.split("-");
                    String operation=split[0];
                    Long id = Long.valueOf(split[1]);
                    if(DelayTaskEnum.SECKILL_START_TASK.getCode().equals(operation)){
                        //自动开始秒杀任务
                        autoStartSeckill(id);
                    }else if(DelayTaskEnum.SECKILL_END_TASK.getCode().equals(operation)){
                        //自动结束秒杀任务
                        autoEndSeckill(id);
                    }else if(DelayTaskEnum.GROUP_PURCHASES_START_TASK.getCode().equals(operation)){
                        //自动开始团购任务
                        autoStartGroupPurchase(id);
                    }
                    else if(DelayTaskEnum.GROUP_PURCHASES_END_TASK.getCode().equals(operation)){
                        //自动结束团购任务
                        autoEndGroupPurchase(id);
                    } else if (DelayTaskEnum.AUCTION_GOODS_START_TASK.getCode().equals(operation)) {
                        // 自动开始拍卖商品任务
                        autoStartAuctionGoods(id);
                    } else if (DelayTaskEnum.AUCTION_GOODS_END_TASK.getCode().equals(operation)) {
                        // 自动结束拍卖商品任务
                        autoEndAuctionGoods(id);
                    }else if(DelayTaskEnum.ORDER_AUTOMATIC_CANCEL.getCode().equals(operation)){
                        //自动取消订单
                        autoCancelOrder(id);
                    }
                    //删除失效的key
                    redisTemplate.delete(expiredKey);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
    @Async
    public void autoEndAuctionGoods(Long id) {
        log.info("autoEndAuctionGoods scheduler task is running :{}", id);
        auctionClient.endAuctionGoods(id, SecurityConstants.INNER);
    }
 
    @Async
    public void autoStartAuctionGoods(Long id) {
        log.info("autoStartAuctionGoods scheduler task is running :{}", id);
        auctionClient.startAuctionGoods(id, SecurityConstants.INNER);
    }
 
    public <T> T getAndSet(final String key, T value){
        T oldValue=null;
        try {
            ValueOperations<String, Object> operations = redisTemplate.opsForValue();
            oldValue =(T) operations.getAndSet(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return oldValue;
    }
 
    //延时任务表
    @Async
    public void autoStartSeckill(Long seckillId) {
        log.info("autoStartSeckill scheduler task is running :{}", seckillId);
        goodsSkuClient.startSeckill(seckillId, SecurityConstants.INNER);
 
    }
 
    @Async
    public void autoEndSeckill(Long seckillId) {
        log.info("autoEndSeckill scheduler task is running :{}", seckillId);
        goodsSkuClient.endSeckill(seckillId, SecurityConstants.INNER);
    }
 
    @Async
    public void autoStartGroupPurchase(Long GroupPurchaseId) {
        log.info("autoStartGroupPurchase scheduler task is running :{}", GroupPurchaseId);
        goodsSkuClient.startGroupPurchase(GroupPurchaseId, SecurityConstants.INNER);
    }
 
    @Async
    public void autoEndGroupPurchase(Long GroupPurchaseId) {
        log.info("autoEndGroupPurchase scheduler task is running :{}", GroupPurchaseId);
        goodsSkuClient.endGroupPurchase(GroupPurchaseId, SecurityConstants.INNER);
    }
 
    @Async
    public void autoCancelOrder(Long orderId) {
        log.info("autoCancelOrder scheduler task is running :{}", orderId);
    }
}