SpringBoot2.0中使用订阅redis的多个频道的消息
声明:参考文章:https://blog.csdn.net/myNameIssls/article/details/75471012?locationNum=2&fps=1
一·使用maven,在项目中引入redis启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二.注册一个redis消息监听类
@Configuration
public class RedisSubListenerConfig {
//不同的频道名
private static final String channel = "testchannel";
private static final String channel2 = "chat"; /**
* redis消息监听器容器
* 可以添加多个监听不同话题的redis监听器,只需要把消息监听器和相应的消息订阅处理器绑定,该消息监听器
* 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理
* @param connectionFactory
* @param listenerAdapter
* @return
*/
@Bean //相当于xml中的bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter,MessageListenerAdapter listenerAdapter2) { RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//订阅了一个叫testchannel 的通道
container.addMessageListener(listenerAdapter, new PatternTopic(RedisSubListenerConfig.channel));
//订阅了一个叫chat的频道
container.addMessageListener(listenerAdapter2, new PatternTopic(RedisSubListenerConfig.channel2));
return container;
} /**
* 消息监听器适配器,绑定消息处理器,利用反射技术调用消息处理器的业务方法
* @param receiver
* @return
*/
@Bean
MessageListenerAdapter listenerAdapter(MessageReceiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
} @Bean
MessageListenerAdapter listenerAdapter2(MessageReceiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage2");
} /**redis 读取内容的template */
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
} }
三·编写处理订阅消息的类
@Component
public class MessageReceiver {
/**接收消息的方法*/
public void receiveMessage(String message){
System.out.println("收到一条消息:"+message);
} /**接收消息的方法*/
public void receiveMessage2(String message){
System.out.println("收到一条消息2:"+message);
} }
四.启动程序,向redis的chat以及testchannel频道中发送消息,都可以监听到
SpringBoot2.0中使用订阅redis的多个频道的消息的更多相关文章
- SpringBoot2.0中的事务@Transactional
在SpringBoot2.0中使用使用需要注意的地方. 1. 加@Transactional的方法不能是private和protected修饰,private会直接报编译错误,protected不会报 ...
- Springboot2.0中jpa默认创建的mysql表为myisam引擎问题
使用Springboot2.0后,使用jpa操作mysql数据库时,默认创建的表的引擎是myisam,myisam是不能加外键的,找了一些资源,最终可以用此方法解决! yml格式: spring: j ...
- springBoot2.0 配置 mybatis+mybatisPlus+redis
一.Idea新建springBoot项目 next到完成,然后修改使用自己的maven 等待下载包 二.pom.xml文件 <?xml version="1.0" encod ...
- SpringBoot2.0中使用自定义properties文件
一.在resources目录下添加自定义的test.properties文件 test.properties内容如下: host=127.0.0.1 port=8080 二.编写一个读取配置文件内容的 ...
- Springboot2.0整合Redis(注解开发)
一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...
- springBoot2.0 配置shiro实现权限管理
一.前言 基于上一篇springBoot2.0 配置 mybatis+mybatisPlus+redis 这一篇加入shiro实现权限管理 二.shiro介绍 2.1 功能特点 Shiro 包含 10 ...
- SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上 ...
- SpringBoot2.0 基础案例(03):配置系统全局异常映射处理
一.异常分类 这里的异常分类从系统处理异常的角度看,主要分类两类:业务异常和系统异常. 1.业务异常 业务异常主要是一些可预见性异常,处理业务异常,用来提示用户的操作,提高系统的可操作性. 常见的业务 ...
- (六)SpringBoot2.0基础篇- Redis整合(JedisCluster集群连接)
一.环境 Redis:4.0.9 SpringBoot:2.0.1 Redis安装:Linux(Redhat)安装Redis 二.SpringBoot整合Redis 1.项目基本搭建: 我们基于(五) ...
随机推荐
- asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值
asp.net mvc 自定义模型绑定 有潜在的Requset.Form 自定义了一个模型绑定器.前端会传过来一些敏感字符.调用bindContext. valueProvider.GetValue( ...
- HDU 4394 BFS
M2%10x=N (x=0,1,2,3....) 给出N.找到最小的满足条件的M 因为:N的个位仅仅由M的个位决定.N十位由M的个位和十位决定,N的百位由M的个位十位百位决定.以此类推 全部从个位開始 ...
- 在IntelliJ IDEA中创建Maven多模块项目
在IntelliJ IDEA中创建Maven多模块项目 1,创建多模块项目选择File>New>Project 出现New Project窗口左侧导航选择Maven,勾选右侧的Create ...
- 如何在IE浏览器里模仿DomContentLoaded
稍微了解一点框架的事件绑定的都知道 window.onload 事件需要在页面所有内容(包括图片.flash.iframe等)加载完后,才执行,但往往我们更希望在 DOM 一加载完就执行脚本,而各大框 ...
- c#自己实现线程池功能(二)
介绍 在上一篇c#自己实现线程池功能(一)中,我们基本实现了一个能够执行的程序.而不能真正的称作线程池.因为是上篇中的代码有个致命的bug那就是没有任务是并非等待,而是疯狂的进行while循环,并试图 ...
- dnscapy使用——本质上是建立ssh的代理(通过dns tunnel)
git clone https://github.com/cr0hn/dnscapy.git easy_install Scapy 服务端: python dnscapy_server.py a.fr ...
- hdoj--1162--Eddy's picture(最小生成树)
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- UINavi中push控制器的时候隐藏TabBar
当一个UITabbarController管理多个UINavigationController的时候,我们又从这每一个UINavigationController中push一个ViewControll ...
- 当安装了ubuntu操作系统怎么也调用不出中文输入法时,可以用以下方式尝试解决。
卸载 fcitx sudo apt-get remove fcitx 重启 sudo reboot 重新安装 fcitxsudo apt-get isntall fcitx 安装拼音输入法sudo a ...
- 阿里云主机ssh 免密码登录
云主机配置: 操作系统: CentOS 7.0 64位CPU: 1 核公网IP: 78.129.23.45用户名: root密码:bugaosuni 本地环境:我在VMware下安装的Ubuntu 1 ...