package com.ruoyi.system.listener;
|
|
|
import com.ruoyi.common.core.utils.DateUtils;
|
import com.ruoyi.common.core.utils.StringUtils;
|
import com.ruoyi.system.api.constants.DelayTaskEnum;
|
import java.util.Date;
|
import lombok.extern.log4j.Log4j2;
|
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;
|
|
|
/**
|
* @author mitao
|
* @date 2024/5/22
|
*/
|
@Log4j2
|
@Component
|
public class RedisListener extends KeyExpirationEventMessageListener {
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
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.SECKILL_START_TASK.getCode().equals(operation)){
|
//自动开始任务
|
}else if(DelayTaskEnum.SECKILL_END_TASK.getCode().equals(operation)){
|
//自动结束任务
|
}else if(DelayTaskEnum.GROUP_PURCHASES_START_TASK.getCode().equals(operation)){
|
//自动开始任务
|
}
|
else if(DelayTaskEnum.GROUP_PURCHASES_END_TASK.getCode().equals(operation)){
|
//自动结束任务
|
}else if(DelayTaskEnum.ORDER_AUTOMATIC_CANCEL.getCode().equals(operation)){
|
//自动结束任务
|
}
|
//删除失效的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 autoStartActivity(String activityId){
|
log.info("autoStartActivity scheduler task is running :" + activityId);
|
}
|
|
private void autoEndActivity(String activityId){
|
log.info("autoEndActivity scheduler task is running :" + activityId);
|
}
|
|
private void autoCancelOrder(String orderId){
|
log.info("autoCancelOrder scheduler task is running :" + orderId);
|
}
|
}
|