Redis實現訂單過期刪除的方法步驟
前言
設計訂單過期,不能單純靠Redis,需要兜底策略
代碼實現:
import com.coolplay.trade.dto.req.CancelOrderReq; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.springframework.data.redis.core.ZSetOperations; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Set; import java.util.concurrent.TimeUnit; @Service @Slf4j public class OrderRedisDelayQueueOperator extends AbstractOrderScheduleDelayQueue { @Resource(name = "redisTemplate") private ZSetOperations<String, String> orderRedis; /** * 預售、現貨生成訂單15分鐘後未支付,需要取消訂單 */ private static final String DELAY_QUEUE_NAME = "order"; /** * 每1秒執行一次 */ @Override @Scheduled(cron = "0/1 * * * * ? ") public void orderEventProcess() { if (!redisLock.tryLock(this.getClass().getSimpleName(), TimeUnit.MILLISECONDS, 10, 100)) { return; } Set<String> dq = orderRedis.range(DELAY_QUEUE_NAME, 0L, Long.MAX_VALUE); if (CollectionUtils.isEmpty(dq)) { return; } for (String orderNo : dq) { Double xs = orderRedis.score(DELAY_QUEUE_NAME, orderNo); Double now = System.currentTimeMillis() * 1.0; if (xs <= now) { log.info("{} timed out", orderNo); super.threadPoolTaskExecutor.execute(() -> { CancelOrderReq req = new CancelOrderReq(); req.setOrderNo(orderNo); req.setCancelType(OrderActionEnum.TIME_OUT_CANCEL); orderService.cancelOrder(req); }); } else { //log.info("{} no time out", orderNo); //如果最小的都沒有過期,剩餘的則不用處理瞭 break; } } } public void addToRedis(String orderNo, long delayTime) { orderRedis.add(DELAY_QUEUE_NAME, orderNo, delayTime * 1.0); } public void removeFromRedis(String orderNo) { orderRedis.remove(DELAY_QUEUE_NAME, orderNo); } }
兜底策略
/** * 取消訂單--10分鐘--20分鐘執行一次 */ @XxlJob("cancelOrder20Minutes") public void cancelOrderTenMinutes() { log.info("*****[開始:下單十分鐘以後系統自動取消訂單]*****"); Date start = DateUtil.dateRoll(new Date(), Calendar.MINUTE,-20); Date end = new Date(); List<ClOrder> clorderList =clOrderMapper.selectListAllOrdrWaiting(start,end); if(ObjectUtil.isNotEmpty(clorderList)){ for(int i=0;i<clorderList.size();i++){ ClOrder clOrder = clorderList.get(i); if(ObjectUtil.isNotEmpty(clOrder)){ Date orderTime = clOrder.getOrderTime(); long between = cn.hutool.core.date.DateUtil.between(orderTime, new Date(), DateUnit.MINUTE); if(between>10){ ClOrder clOrderTemp = new ClOrder(); clOrderTemp.setOrderState("3"); clOrderTemp.setId(clOrder.getId()); clOrderTemp.setMemberId(clOrder.getMemberId()); String msg="您的訂單已經取消,訂單金額已發放至您的賬戶請查收~"; try { boolean b = orderService.cancelOrder(clOrderTemp,msg); if(!b){ log.info("[訂單失效:定時任務兜底策略更新失敗]**訂單ID: {}",clOrderTemp.getId()); } log.info("[Redis訂單取消訂單失效,定時任務兜底策略生效]"); }catch (Exception e){ log.info("[訂單失效:定時任務兜底策略更新失敗]**訂單ID: {}",clOrderTemp.getId()); e.printStackTrace(); } } } } } log.info("*****[結束:下單十分鐘以後系統自動取消訂單]*****"); }
到此這篇關於Redis實現訂單過期刪除的方法步驟的文章就介紹到這瞭,更多相關Redis 訂單過期刪除內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- redis zset實現滑動窗口限流的代碼
- Java使用延時隊列搞定超時訂單處理的場景
- redis實現隊列的阻塞、延時、發佈和訂閱
- Java redisTemplate阻塞式處理消息隊列
- Java實現Redis延時消息隊列