In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to use list to do message queue in redis". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use list to do message queue in redis" can help you solve your doubts.
LeftPush message enters the team, rightPop corresponds, message goes out of the team.
RightPop (RedisConstant.MQ_LIST, 0L, TimeUnit.SECONDS) blocks out of the queue. 0 means permanent blocking.
Production message service @ Servicepublic class RedisService {@ Autowired private RedisTemplate 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 listOperations = redisTemplate.opsForList () / / leftPush corresponds to rightPop, left in the team, right out of the team listOperations.leftPush (RedisConstant.MQ_LIST, s); / / because dequeue is blocking read, so after the last step into the team, the data is immediately driven away, the next step size=0 Long size= listOperations.size (RedisConstant.MQ_LIST); List list = new ArrayList () If (size! = null & & size > 0) {list = listOperations.range (RedisConstant.MQ_LIST, 0, size-1);} return list;}}
test
@ RestController@RequestMapping ("redisList") public class RedisListController {@ Autowired private RedisService redisService; @ GetMapping ("publish") public Object publish () {return redisService.publish ();}} consumption of messaging services, scheduled tasks @ Componentpublic class RedisConsumeTask {@ Autowired private RedisService redisService; @ TaskLock (RedisConstant.CONSUME_REDIS_LIST) @ Scheduled (cron = "0amp 10 *?") Public void consumeMqList () {redisService.consumeMqList ();}} @ Service@Slf4jpublic class RedisService {@ Autowired private RedisTemplate redisTemplate; public void consumeMqList () {ListOperations listOperations = redisTemplate.opsForList (); / 0 indicates that the blocking is permanent / / after an hour of standby, the message will be sent again, and the consumption will not be available. There is a problem with the blocking. Still have to look for / / 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);}} log @ Component@Aspectpublic 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 ("Task start," + lockKey + "," + new Date ()) Return pjp.proceed ();} finally {lock.unlock (); System.out.println ("Task over," + lockKey + "," + new Date ());} Test
Http://localhost:9040/redisList/publish
["{" createTime ": 1574394538430," id ": 1," money ":" 12.34 "," orderNo ":" orderNo1 "}"]
The following has been blocked, the task begins, does not receive a message, it will never end.
There is a problem with the congestion, so we use polling instead.
First start the sending message service and send the message. Then start the consumer message service, and you can consume the message. This is more stable than publish subscriptions.
After reading this, the article "how to use list to do message queuing in redis" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.