文章正文
springboot集成Redis队列的发布订阅功能
【文档】2020-04-23
简介springboot集成Redis队列的发布订阅功能
注册监听:
package com.example.demo.config;
import com.example.demo.controller.RedisController;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter1,
MessageListenerAdapter listenerAdapter2) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
// 每一个监听器messageListener都需要一个适配器,队列可以是同一个
container.addMessageListener(listenerAdapter1, new PatternTopic("redis:test"));
container.addMessageListener(listenerAdapter2, new PatternTopic("redis:prod"));
return container;
}
/**
* 消息监听器适配器,绑定消息处理器,利用反射技术调用消息处理器的业务方法
* @return
*/
@Bean
MessageListenerAdapter listenerAdapter1(RedisController redisController) {
return new MessageListenerAdapter(redisController, "receiveMessage1");
}
/**
* 消息监听器适配器,绑定消息处理器,利用反射技术调用消息处理器的业务方法
* @return
*/
@Bean
MessageListenerAdapter listenerAdapter2(RedisController redisController) {
return new MessageListenerAdapter(redisController, "receiveMessage2");
}
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
}消息发布:
/**
* 消息发布者
* @return
*/
@RequestMapping("/redisTest")
public String redisTest() {
for(int i = 1; i <= 5; i++) {
redisTemplate.convertAndSend("redis:test","通知你该开始了");
}
redisTemplate.convertAndSend("redis:prod","通知你该结束了");
return "";
}消息订阅:
package com.example.demo.controller;
import org.springframework.stereotype.Component;
@Component
public class RedisController {
public void receiveMessage1(String message) {
System.out.println(message+":收到");
}
public void receiveMessage2(String message) {
System.out.println(message+":OK");
}
}
打赏支持
感谢您的支持,加油!
打开微信扫码打赏,你说多少就多少
找书费时,联系客服快速获取!
在线客服8:30-22:30,若离线请留言!
获取教程,请联系在线客服!
在线客服8:30-22:30,若离线请留言!
热门阅读
-
20世纪日本文学史 叶渭渠,唐月梅著 青岛:青岛出版社 PDF 7543617285 2004.pdf
20世纪日本文学史 叶渭渠,唐月梅著 青岛:青岛出版社 PDF 7543617285 2004.pdf ...
-
20世纪日本文学史 以小说为中心 谢志宇著 杭州:浙江大学出版社 PDF 7308043584 2005.pdf
20世纪日本文学史 以小说为中心 谢志宇著 杭州:浙江大学出版社 PDF 7308043584 2005.pdf ...
-
一桩案件的几种说法 中短篇小说珍藏本 尤凤伟著 北京:中国社会出版社 PDF 7508705521 2005.pdf
一桩案件的几种说法 中短篇小说珍藏本 尤凤伟著 北京:中国社会出版社 PDF 7508705521 2005.pdf ...
-
成功潜规则 中国式人脉网 常桦编著 武汉:武汉大学出版社 PDF 9787307076860 2010.pdf
成功潜规则 中国式人脉网 常桦编著 武汉:武汉大学出版社 PDF 9787307076860 2010.pdf ...
-
航海秘史 航海史上离奇却真实的故事 (英)约恩·哈丁(John Harding)著;任静,孙旻等译 北京:蓝天出版社 PDF 7801586018 2006.pdf
航海秘史 航海史上离奇却真实的故事 (英)约恩·哈丁(John Harding)著;任静,孙旻等译 北京:蓝天出版社 PDF 7801586018 2006.pdf ...
-
C程序设计经典教程 第4版 (美)戴特尔(Deitel,H.M.),(美)戴特尔(Deitel,P.J.)著;聂雪军,贺军译 北京:清华大学出版社 PDF 7302117802 2006.pdf
C程序设计经典教程 第4版 (美)戴特尔(Deitel,H.M.),(美)戴特尔(Deitel,P.J.)著;聂雪军,贺军译 北京:清华大学出版社 PDF 7302117802 2006.pdf ...
-
法语听写450题 初级 刘成富等编著;(法)让-路易·古塞插图 上海:上海译文出版社 PDF 7532736350 出版时间:2005.pdf
法语听写450题 初级 刘成富等编著;(法)让-路易·古塞插图 上海:上海译文出版社 PDF 7532736350 出版时间:2005.pdf ...
-
世界摄影大师社会纪实摄影经典500幅鉴赏 上下 紫都,程文超编著;岳鑫,李浩图片 呼和浩特:远方出版社 PDF 7807230126 2005.pdf
世界摄影大师社会纪实摄影经典500幅鉴赏 上下 紫都,程文超编著;岳鑫,李浩图片 呼和浩特:远方出版社 PDF 7807230126 2005.pdf ...
-
女人是一种态度 徐俐著 桂林:漓江出版社 PDF 9787540751104 2011.pdf
女人是一种态度 徐俐著 桂林:漓江出版社 PDF 9787540751104 2011.pdf ...
-
金沙考古 黄剑华著 成都:成都时代出版社 PDF 7807052058 出版时间:2005.pdf
金沙考古 黄剑华著 成都:成都时代出版社 PDF 7807052058 出版时间:2005.pdf ...