redis用list做消息隊(duì)列的實(shí)現(xiàn)示例
leftPush消息入隊(duì),rightPop對(duì)應(yīng),消息出隊(duì)。
rightPop(RedisConstant.MQ_LIST, 0L, TimeUnit.SECONDS)阻塞出隊(duì),0表示永久阻塞
生產(chǎn)消息服務(wù)
@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對(duì)應(yīng),左邊入隊(duì),右邊出隊(duì) ? ? ? ? listOperations.leftPush(RedisConstant.MQ_LIST, s); ? ? ? ? //因?yàn)槌鲫?duì)是阻塞讀取的,所以上一步入隊(duì)后,數(shù)據(jù)立刻就被驅(qū)走了,下一步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; ? ? } }
測(cè)試
@RestController @RequestMapping("redisList") public class RedisListController { ? ? @Autowired ? ? private RedisService redisService; ? ? @GetMapping("publish") ? ? public Object publish() { ? ? ? ? return redisService.publish(); ? ? } }
消費(fèi)消息服務(wù),定時(shí)任務(wù)
@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時(shí)間,表示阻塞永久 ? ? ? ? //待機(jī)一小時(shí)后,再次發(fā)消息,消費(fèi)不了了,阻塞有問題啊。還得輪尋啊 ? ? ? ? //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("任務(wù)開始, " + lockKey + ", " + new Date()); ? ? ? ? ? ? return pjp.proceed(); ? ? ? ? } finally { ? ? ? ? ? ? lock.unlock(); ? ? ? ? ? ? System.out.println("任務(wù)結(jié)束, " + lockKey + ", " + new Date()); ? ? ? ? } ? ? } }
測(cè)試
http://localhost:9040/redisList/publish
["{“createTime”:1574394538430,“id”:1,“money”:“12.34”,“orderNo”:“orderNo1”}"]
下面一直阻塞,任務(wù)開始了,不收到消息,永遠(yuǎn)不會(huì)結(jié)束。
阻塞有問題,改用輪詢了。
先啟動(dòng)發(fā)送消息服務(wù),發(fā)送消息。后啟動(dòng)消費(fèi)消息服務(wù),可以消費(fèi)消息。這一點(diǎn),比發(fā)布訂閱要穩(wěn)定。
關(guān)聯(lián)項(xiàng)目https://github.com/mingwulipo/cloud-demo.git
到此這篇關(guān)于redis用list做消息隊(duì)列的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)redis list消息隊(duì)列內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis數(shù)據(jù)庫(kù)的使用場(chǎng)景介紹(避免誤用Redis)
這篇文章主要介紹了Redis數(shù)據(jù)庫(kù)的使用場(chǎng)景介紹(避免誤用Redis),本文用簡(jiǎn)要的語(yǔ)言總結(jié)了Redis數(shù)據(jù)庫(kù)的適應(yīng)場(chǎng)合,人而避免錯(cuò)誤的使用它而產(chǎn)生昂貴的維護(hù)代價(jià),需要的朋友可以參考下2015-03-03淺析Redis底層數(shù)據(jù)結(jié)構(gòu)Dict
Redis是一個(gè)鍵值型的數(shù)據(jù)庫(kù),我們可以根據(jù)鍵實(shí)現(xiàn)快速的增刪改查,而鍵與值的映射關(guān)系正是通過Dict來(lái)實(shí)現(xiàn)的,當(dāng)然?Dict?也是?Set?Hash?的實(shí)現(xiàn)方式,本文就詳細(xì)帶大家介紹一下Redis底層數(shù)據(jù)結(jié)構(gòu)?Dict,,需要的朋友可以參考下2023-05-05Redis?異常?read?error?on?connection?的解決方案
這篇文章主要介紹了Redis異常read?error?on?connection的解決方案,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08Redis分布式限流組件設(shè)計(jì)與使用實(shí)例
本文主要講解基于 自定義注解+Aop+反射+Redis+Lua表達(dá)式 實(shí)現(xiàn)的限流設(shè)計(jì)方案。實(shí)現(xiàn)的限流設(shè)計(jì)與實(shí)際使用。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08redis-benchmark并發(fā)壓力測(cè)試的問題解析
這篇文章主要介紹了redis-benchmark并發(fā)壓力測(cè)試的問題解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01redis與memcached的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric為首開發(fā)的高性能分布式內(nèi)存緩存服務(wù)器。那么redis與memcached有什么區(qū)別呢?下面小編給大家介紹下redis與memcached的區(qū)別,感興趣的朋友參考下吧2017-08-08Redis與本地緩存的結(jié)合實(shí)現(xiàn)
我們開發(fā)中經(jīng)常用到Redis作為緩存,本文主要介紹了Redis與本地緩存的結(jié)合實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Redis之常用數(shù)據(jù)結(jié)構(gòu)哈希表
這篇文章主要介紹了Redis常用的數(shù)據(jù)結(jié)構(gòu)哈希表,哈希表是一種保存鍵值對(duì)的數(shù)據(jù)結(jié)構(gòu),具有一定的參考價(jià)值,需要的朋友可以參考閱讀2023-04-04