jiangqs
2023-07-13 cfd0118ef446a66f91999afc7b1e46ca607adf50
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
package com.ruoyi.system.listener;
 
 
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.constant.DelayTaskEnum;
import com.ruoyi.system.api.service.RemoteActivityService;
import com.ruoyi.system.api.service.RemoteConfigService;
import com.ruoyi.system.api.service.RemoteCouponService;
import com.ruoyi.system.api.service.RemoteOrderService;
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.stereotype.Component;
 
import java.util.Date;
 
/**
 * @author jqs34
 * @ClassName RedisListener
 * @description: redis监听
 * @date 2023年06月06日
 * @version: 1.0
 */
@Slf4j
@Component
public class RedisListener extends KeyExpirationEventMessageListener {
 
    private RedisTemplate<String, Object> redisTemplate;
 
    private RemoteConfigService remoteConfigService;
 
    private RemoteCouponService remoteCouponService;
 
    private RemoteActivityService remoteActivityService;
 
    private RemoteOrderService remoteOrderService;
 
    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];
                    if(DelayTaskEnum.COUPON_SEND_DELAY_TASK.getCode().equals(operation)){
                        //自动发送优惠券
                        autoTimeSendCoupon(split[1]);
                    }else if(DelayTaskEnum.ACTIVITY_START_TASK.getCode().equals(operation)){
                        //自动开始任务
                        autoStartActivity(split[1]);
                    }else if(DelayTaskEnum.ACTIVITY_END_TASK.getCode().equals(operation)){
                        //自动结束任务
                        autoEndActivity(split[1]);
                    }else if(DelayTaskEnum.ORDER_AUTOMATIC_CANCEL.getCode().equals(operation)){
                        //自动结束任务
                        autoCancelOrder(split[1]);
                    }
 
                    //删除失效的key
                    redisTemplate.delete(expiredKey);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
    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;
    }
 
    //延时任务表
    //自动定时发送优惠券
    private void autoTimeSendCoupon(String couponId){
        log.info("autoTimeSendCoupon scheduler task is running :" + couponId);
        remoteCouponService.autoTimeSendCoupon(couponId);
        //删除定时任务
        remoteConfigService.deleteDelayTask(DelayTaskEnum.COUPON_SEND_DELAY_TASK.getCode()+"-"+couponId);
    }
 
    private void autoStartActivity(String activityId){
        log.info("autoStartActivity scheduler task is running :" + activityId);
        remoteActivityService.startActivity(activityId);
        //删除定时任务
        remoteConfigService.deleteDelayTask(DelayTaskEnum.ACTIVITY_START_TASK.getCode()+"-"+activityId);
    }
 
    private void autoEndActivity(String activityId){
        log.info("autoEndActivity scheduler task is running :" + activityId);
        remoteActivityService.endActivity(activityId);
        //删除定时任务
        remoteConfigService.deleteDelayTask(DelayTaskEnum.ACTIVITY_END_TASK.getCode()+"-"+activityId);
    }
 
    private void autoCancelOrder(String orderId){
        log.info("autoCancelOrder scheduler task is running :" + orderId);
        remoteOrderService.autoCancelOrder(orderId);
        //删除定时任务
        remoteConfigService.deleteDelayTask(DelayTaskEnum.ORDER_AUTOMATIC_CANCEL.getCode()+"-"+orderId);
    }
}