最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

redis用list做消息隊(duì)列的實(shí)現(xiàn)示例

 更新時(shí)間:2022年02月18日 11:28:48   作者:絕世奇才  
本文主要介紹了redis用list做消息隊(duì)列的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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 MGET命令深度解析

    Redis MGET命令深度解析

    Redis的MGET命令是一種高效的批量讀取操作,可以顯著提高讀取性能,減少網(wǎng)絡(luò)往返的次數(shù),本文從MGET命令的機(jī)制實(shí)現(xiàn)、底層原理、應(yīng)用場(chǎng)景及性能優(yōu)化等多個(gè)維度,深入解析Redis中的MGET命令的工作方式,并對(duì)它與其他批量操作命令的對(duì)比進(jìn)行了詳細(xì)介紹
    2024-09-09
  • redis緩存預(yù)熱的實(shí)現(xiàn)示例

    redis緩存預(yù)熱的實(shí)現(xiàn)示例

    本文主要介紹了Java中實(shí)現(xiàn)緩存預(yù)熱的多種策略,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • Redis 在真實(shí)世界的 5 個(gè)用法

    Redis 在真實(shí)世界的 5 個(gè)用法

    Redis是一個(gè)開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API這篇文章主要介紹了Redis 在真實(shí)世界的 5 個(gè)用法,需要的朋友可以參考下
    2018-03-03
  • Redis:Redisson分布式鎖的使用方式(推薦使用)

    Redis:Redisson分布式鎖的使用方式(推薦使用)

    這篇文章主要介紹了Redis:Redisson分布式鎖的使用方式(推薦使用),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • redis中5種數(shù)據(jù)基礎(chǔ)查詢命令

    redis中5種數(shù)據(jù)基礎(chǔ)查詢命令

    本文主要介紹了redis中5種數(shù)據(jù)基礎(chǔ)查詢命令,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • docker安裝redis的完整步驟詳解

    docker安裝redis的完整步驟詳解

    這篇文章主要介紹了docker安裝redis的完整步驟,包括拉取鏡像、設(shè)置配置文件、編寫docker-compose.yml文件啟動(dòng)Redis以及測(cè)試Redis連接,文中通過圖文及代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-03-03
  • redis cluster支持pipeline的實(shí)現(xiàn)思路

    redis cluster支持pipeline的實(shí)現(xiàn)思路

    本文給大家介紹redis cluster支持pipeline的實(shí)現(xiàn)思路,在 cluster 上執(zhí)行 pipeline 可能會(huì)由于 redis 節(jié)點(diǎn)擴(kuò)縮容 中途 redirection 切換連接導(dǎo)致結(jié)果丟失,具體細(xì)節(jié)問題請(qǐng)參考下本文
    2021-06-06
  • redis通過lua腳本,獲取滿足key pattern的所有值方式

    redis通過lua腳本,獲取滿足key pattern的所有值方式

    這篇文章主要介紹了redis通過lua腳本,獲取滿足key pattern的所有值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Redis是單線程的嗎

    Redis是單線程的嗎

    Redis使用單線程的原因就是多線程并不能有效提升Redis的性能,相反可能還會(huì)降低性能,所以自然而然使用單線程,本文給大家詳細(xì)介紹了Redis為什么是單線程的,感興趣的朋友跟隨小編一起看看吧
    2023-06-06
  • Jackson2JsonRedisSerializer和GenericJackson2JsonRedisSerializer區(qū)別

    Jackson2JsonRedisSerializer和GenericJackson2JsonRedisSerializ

    本文主要介紹了Jackson2JsonRedisSerializer和GenericJackson2JsonRedisSerializer區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04

最新評(píng)論

枣强县| 涪陵区| 若羌县| 吉首市| 吴忠市| 淮北市| 禹城市| 屯门区| 宝丰县| 凤凰县| 万载县| 安国市| 临潭县| 蒙自县| 五河县| 仁寿县| 巴东县| 海宁市| 皋兰县| 新郑市| 象山县| 凯里市| 宝应县| 台前县| 曲周县| 溧阳市| 县级市| 崇明县| 黄浦区| 龙井市| 惠东县| 平谷区| 黄大仙区| 鄂尔多斯市| 东山县| 西安市| 旌德县| 北碚区| 沅陵县| 武宣县| 新化县|