这篇文章主要介绍Java redisTemplate阻塞式处理消息队列的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Redis 消息队列redis五种数据结构队列生产者package c
这篇文章主要介绍Java redisTemplate阻塞式处理消息队列的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
package cn.stylefeng.guns.knowledge.modular.knowledge.schedule;import lombok.extern.slf4j.Slf4j;import org.springframework.data.redis.core.RedisTemplate;import java.util.Random;import java.util.UUID;@Slf4jpublic class QueueProducer implements Runnable { public static final String QUEUE_PRODUCTER = "queue-producter"; private RedisTemplate<String, Object> redisTemplate; public QueueProducer(RedisTemplate<String, Object> redisTemplate) { this.redisTemplate = redisTemplate; } @Override public void run() { Random random = new Random(); while (true) { try { Thread.sleep(random.nextInt(600) + 600); // 1.模拟生成一个任务 UUID queueProducerId = UUID.randomUUID(); // 2.将任务插入任务队列:queue-producter redisTemplate.opsForList().leftPush(QUEUE_PRODUCTER, queueProducerId.toString()); log.info("生产一条数据 >>> {}", queueProducerId.toString()); } catch (Exception e) { e.printStackTrace(); } } }}
package cn.stylefeng.guns.knowledge.modular.knowledge.schedule;import lombok.extern.slf4j.Slf4j;import org.springframework.data.redis.core.RedisTemplate;import java.util.Random;@Slf4jpublic class QueueConsumer implements Runnable { public static final String QUEUE_PRODUCTER = "queue-producter"; public static final String TMP_QUEUE = "tmp-queue"; private RedisTemplate<String, Object> redisTemplate; public QueueConsumer(RedisTemplate<String, Object> redisTemplate) { this.redisTemplate = redisTemplate; } @Override public void run() { Random random = new Random(); while (true) { // 1.从任务队列"queue-producter"中获取一个任务,并将该任务放入暂存队列"tmp-queue" Long ququeConsumerId = redisTemplate.opsForList().rightPush(QUEUE_PRODUCTER, TMP_QUEUE); // 2.处理任务----纯属业务逻辑,模拟一下:睡觉 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // 3.模拟成功和失败的偶然现象,模拟失败的情况,概率为2/13 if (random.nextInt(13) % 7 == 0) { // 4.将本次处理失败的任务从暂存队列"tmp-queue"中,弹回任务队列"queue-producter" redisTemplate.opsForList().rightPush(TMP_QUEUE, QUEUE_PRODUCTER); log.info(ququeConsumerId + "处理失败,被弹回任务队列"); } else { // 5. 模拟成功的情况,将本次任务从暂存队列"tmp-queue"中清除 redisTemplate.opsForList().rightPop(TMP_QUEUE); log.info(ququeConsumerId + "处理成功,被清除"); } } }}
@Test public void QueueThreadTotalEntry() throws Exception { // 1.启动一个生产者线程,模拟任务的产生 new Thread(new QueueProducer(redisTemplate)).start(); Thread.sleep(15000); // 2.启动一个线程者线程,模拟任务的处理 new Thread(new QueueConsumer(redisTemplate)).start(); // 3.主线程 Thread.sleep(Long.MAX_VALUE); }
线程一:
Long increment = redisTemplate.opsForValue().increment("increment", 1L); log.info("队列消费者 >> increment递增: {}", increment);
线程二:
Long increment = redisTemplate.opsForValue().increment("increment", 1L); log.info("生产者队列 >> increment递增: {}", increment);
redisTemplate处理/获取redis消息队列
(参考代码)
@Componentpublic class RedisQueue { @Autowired private RedisTemplate redisTemplate; public boolean lpush(String key, Object value) { try { redisTemplate.opsForList().leftPush(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } public Object rpop(String key) { try { return redisTemplate.opsForList().rightPop(key); } catch (Exception e) { e.printStackTrace(); return null; } } public Object brpop(String key, long timeout, TimeUnit timeUnit) { try { return redisTemplate.opsForList().rightPop(key, timeout, timeUnit); } catch (Exception e) { e.printStackTrace(); return null; } } public List<Object> lrange(String key, long start, long end) { try { return redisTemplate.opsForList().range(key, start, end); } catch (Exception e) { e.printStackTrace(); return null; } } }
以上是“Java redisTemplate阻塞式处理消息队列的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网精选频道!
--结束END--
本文标题: Java redisTemplate阻塞式处理消息队列的示例分析
本文链接: https://www.lsjlt.com/news/301147.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0