一、簡介
本次文章主要實現功能是接入redis作為定時器,倒計時實現訂單超時未處理時,超時提醒。
本次講解中代碼是在已經接入redis作為緩存的前提下進一步開發,具體可參考 SpringBoot配置redis作為緩存(含源碼)
使用步驟分別是①調用下單接口,存入redis數據 ② 調用訂單處理接口,手動處理redis數據,手動處理后不再會有超時提醒 ③未處理的訂單會自動監聽超時提醒
二、使用前提
在windows中啟動redis和redis桌面管理工具,分享連接在文章最下方
三、實現效果

三、代碼實現步驟
1.添加redis配置類
package com.cpl.tsl.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.listener.RedisMessageListenerContainer; /** * Redis配置類 * * @author: lll * @date: 2022年03月07日 13:03:27 */ @Configuration public class RedisTaskConfig { @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; } }
2.添加redis監聽
package com.cpl.tsl.listener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.stereotype.Component; import java.util.Date; /** * RedisKey鍵監聽以及業務邏輯處理 * * @author: lll * @date: 2022年03月07日 14:03:49 */ @Component public class RedisTaskListener extends KeyExpirationEventMessageListener { private static final Logger logger = LoggerFactory.getLogger(RedisTaskListener.class); @Value("${applicationName:tsl}") private String applicationName; /** * @param listenerContainer 監聽器 */ public RedisTaskListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { String expiredKey = message.toString(); // 將拿到的過期鍵使用之前拼接時的特殊符號分割成字符數組 String[] expiredKeyArr = expiredKey.split("\\|"); String businessSign = expiredKeyArr[0].toString(); String expiredTimeSign = expiredKeyArr[1].toString(); logger.info(businessSign +":"+ expiredTimeSign); Date date = new Date(); // 只有本業務才執行以下操作 if (businessSign.equals(applicationName + ":ORDERINFO")) { logger.info("訂單超時,已取消"); } else { logger.error("非訂單業務不做處理"); } } }
3.添加controller調動接口
package com.cpl.tsl.controller; import com.cpl.tsl.utils.RedisUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.Date; import java.util.UUID; /** * 測試類 * * @author: lll * @date: 2021年06月02日 18:06:58 */ @RestController @RequestMapping("/") @Api(tags = "測試模塊") public class TestController { private static final Logger logger = LoggerFactory.getLogger(TestController.class); @Resource private RedisTemplate<String, String> template; @Resource private RedisUtil redisUtil; @Value("${applicationName:tsl}") private String applicationName; /** * redis設置定時key,模擬訂單下單,超時提醒或者去掉訂單 * * @param * @return java.lang.String * @author: lll * @date: 2022年03月07日 13:03:59 */ @RequestMapping(value = "/putkeys", method = RequestMethod.POST) @ApiOperation(value = "測試redis存儲參數", notes = "測試redis存儲參數") public String putRedisTaskKeys() { /** * 存入訂單信息 */ Date date = new Date(); //設置超時時間30秒 Long overTime = new Long(30); //創建訂單號 String orderNo = UUID.randomUUID().toString(); //訂單信息 String orderInfo = "這是訂單號為:" + orderNo + " 的訂單,價格是:2000元,下單時間是:" + date; //redis key String redisKey = applicationName + ":ORDERINFO|" + orderNo; redisUtil.set(redisKey, orderInfo, overTime); logger.info("下單時間:" + date); logger.info("訂單的redisKey " + redisKey + " 訂單信息 " + orderInfo); return "下單成功"; } /** * 手動處理訂單,從redis移除訂單 * * @param orderNo 訂單號 * @return java.lang.String * @author: lll * @date: 2022年03月07日 14:03:58 */ @RequestMapping(value = "/removeKeys", method = RequestMethod.POST) @ApiOperation(value = "測試redis移除參數", notes = "測試redis移除參數") public String removeRedisTaskKeys(@ApiParam(name = "orderNo", value = "訂單號", required = true) @RequestParam("orderNo") String orderNo) { /** * 處理訂單 */ //拼接redis key String redisKey = applicationName + ":ORDERINFO|" + orderNo; //刪除redis key redisUtil.del(redisKey); logger.info("訂單redisKey " + redisKey + " 已處理"); return "處理完成"; } }
四、資源
源碼:https://github.com/CodingPandaLLL/tsl.git
源碼壓縮包地址:https://codeload.github.com/CodingPandaLLL/tsl/zip/refs/tags/1.0.4
軟件:Redis(提取碼:vkun)、Redis Desktop Manager(提取碼:2syd)
