網站首頁 編程語言 正文
leftPush消息入隊,rightPop對應,消息出隊。
rightPop(RedisConstant.MQ_LIST, 0L, TimeUnit.SECONDS)阻塞出隊,0表示永久阻塞
生產消息服務
@Service public class RedisService { ? ? @Autowired ? ? private RedisTemplate<String, String> redisTemplate; ? ? public Object publish() { ? ? ? ? OrderDTO dto = new OrderDTO(); ? ? ? ? dto.setId(1); ? ? ? ? dto.setCreateTime(new Date()); ? ? ? ? dto.setMoney("12.34"); ? ? ? ? dto.setOrderNo("orderNo1"); ? ? ? ? String s = JSON.toJSONString(dto); ? ? ? ? ListOperations<String, String> listOperations = redisTemplate.opsForList(); ? ? ? ? //leftPush和rightPop對應,左邊入隊,右邊出隊 ? ? ? ? listOperations.leftPush(RedisConstant.MQ_LIST, s); ? ? ? ? //因為出隊是阻塞讀取的,所以上一步入隊后,數據立刻就被驅走了,下一步size=0 ? ? ? ? Long size = listOperations.size(RedisConstant.MQ_LIST); ? ? ? ? List<String> list = new ArrayList<>(); ? ? ? ? if (size != null && size > 0) { ? ? ? ? ? ? ?list = listOperations.range(RedisConstant.MQ_LIST, 0, size - 1); ? ? ? ? } ? ? ? ? return list; ? ? } }
測試
@RestController @RequestMapping("redisList") public class RedisListController { ? ? @Autowired ? ? private RedisService redisService; ? ? @GetMapping("publish") ? ? public Object publish() { ? ? ? ? return redisService.publish(); ? ? } }
消費消息服務,定時任務
@Component public class RedisConsumeTask { ? ? @Autowired ? ? private RedisService redisService; ? ? @TaskLock(RedisConstant.CONSUME_REDIS_LIST) ? ? @Scheduled(cron = "0/10 * * * * ?") ? ? public void consumeMqList() { ? ? ? ? redisService.consumeMqList(); ? ? } } @Service @Slf4j public class RedisService { ? ? @Autowired ? ? private RedisTemplate<String, String> redisTemplate; ? ? public void consumeMqList() { ? ? ? ? ListOperations<String, String> listOperations = redisTemplate.opsForList(); ? ? ? ? //0時間,表示阻塞永久 ? ? ? ? //待機一小時后,再次發消息,消費不了了,阻塞有問題啊。還得輪尋啊 ? ? ? ? //String s = listOperations.rightPop(RedisConstant.MQ_LIST, 0L, TimeUnit.SECONDS); ? ? ? ? String s = listOperations.rightPop(RedisConstant.MQ_LIST); ? ? ? ? if (s == null) { ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? log.info("{} = {}", RedisConstant.MQ_LIST, s); ? ? ? ? OrderDTO dto = JSON.parseObject(s, OrderDTO.class); ? ? ? ? log.info("dto = {}", dto); ? ? } }
日志
@Component @Aspect public class TaskLockAop { ? ? @Autowired ? ? private RedisLockRegistry redisLockRegistry; ? ? @Around("execution(@TaskLock * * (..))") ? ? public Object taskAround(ProceedingJoinPoint pjp) throws Throwable { ? ? ? ? TaskLock taskAnnotation = ((MethodSignature)pjp.getSignature()).getMethod().getAnnotation(TaskLock.class); ? ? ? ? String lockKey = taskAnnotation.value(); ? ? ? ? Lock lock = redisLockRegistry.obtain(lockKey); ? ? ? ? try { ? ? ? ? ? ? lock.tryLock(30L, TimeUnit.SECONDS); ? ? ? ? ? ? System.out.println("任務開始, " + lockKey + ", " + new Date()); ? ? ? ? ? ? return pjp.proceed(); ? ? ? ? } finally { ? ? ? ? ? ? lock.unlock(); ? ? ? ? ? ? System.out.println("任務結束, " + lockKey + ", " + new Date()); ? ? ? ? } ? ? } }
測試
http://localhost:9040/redisList/publish
["{“createTime”:1574394538430,“id”:1,“money”:“12.34”,“orderNo”:“orderNo1”}"]
下面一直阻塞,任務開始了,不收到消息,永遠不會結束。
阻塞有問題,改用輪詢了。
先啟動發送消息服務,發送消息。后啟動消費消息服務,可以消費消息。這一點,比發布訂閱要穩定。
原文鏈接:https://blog.csdn.net/mingwulipo/article/details/103195582
相關推薦
- 2022-11-30 Git基礎學習之分支基本操作詳解_相關技巧
- 2022-09-13 go開源項目用戶名密碼驗證的邏輯鬼才寫法_Golang
- 2022-08-05 C語言示例講解for循環的用法_C 語言
- 2023-01-20 Python輸入整數進行排序方式_python
- 2022-04-15 C語言各種操作符透徹理解上篇_C 語言
- 2022-10-30 Python接口傳輸url與flask數據詳解_python
- 2023-06-18 c#關于非托管內存的釋放問題及解讀_C#教程
- 2022-04-01 k8s報錯error: You must be logged in to the server (U
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支