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) {
|
auctionClient.endAuctionGoods(id, SecurityConstants.INNER);
|
}
|
|
@Async
|
public void autoStartAuctionGoods(Long 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);
|
}
|
}
|