網站首頁 編程語言 正文
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-10-11 詳解pandas?df.iloc[]的典型用法_python
- 2022-05-15 Qt中QPixmap、QImage、QPicture、QBitmap四者區別詳解_C 語言
- 2022-07-09 Docker項目啟動報設備忙錯誤
- 2022-12-01 docker?容器網絡模式詳解_docker
- 2022-11-25 詳解C++中類的六大默認成員函數_C 語言
- 2023-04-13 next 配置全局scss變量、函數
- 2022-11-02 Kotlin掛起函數的詳細介紹_Android
- 2023-01-19 python使用?f?格式化字符串的用法_python
- 最近更新
-
- 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同步修改后的遠程分支