SpringBoot Redis 订阅发布
一 配置application.yml
spring:
redis:
jedis:
pool:
max-active: 10
min-idle: 5
max-idle: 10
max-wait: 2000
port: 6379
host: 192.168.1.88
timeout: 1000
二 实现监听
package com.example.demo.common; import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component; /**
* @author Tyler
* @date 2019/7/11
*/ @Component
public class RedisMessageListener implements MessageListener { @Override
public void onMessage(Message message, byte[] bytes) {
String body=new String(message.getBody());
String topic=new String(bytes);
System.out.println(body);
System.out.println(topic);
}
}
三 注入spring
package com.example.demo; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Repository; @SpringBootApplication
@MapperScan(basePackages ="com.example.demo.common", annotationClass = Repository.class)
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} @Autowired
private RedisConnectionFactory connectionFactory;
@Autowired
private MessageListener redisMsgListener; private ThreadPoolTaskScheduler taskScheduler; @Bean
public ThreadPoolTaskScheduler initTaskScheduler()
{
if(taskScheduler!=null)
{
return taskScheduler;
}
taskScheduler=new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(20);
return taskScheduler;
} @Bean
public RedisMessageListenerContainer initRedisContainer()
{
RedisMessageListenerContainer container=new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setTaskExecutor(initTaskScheduler());
Topic topic=new ChannelTopic("topic1");
container.addMessageListener(redisMsgListener,topic);
return container;
} }
四 结果:
1 接收exe消息

2 接收controller消息
controller:
@Controller
@RequestMapping("/redis")
public class RedisController {
@Autowired
private StringRedisTemplate stringRedisTemplate; @RequestMapping("/stringAndHash")
@ResponseBody
public Map<String,Object> testStringAndHash()
{
stringRedisTemplate.convertAndSend("topic1","hello"); Map<String,Object> map=new HashMap<>();
map.put("success",true);
return map;
}
}

SpringBoot Redis 订阅发布的更多相关文章
- 补习系列(13)-springboot redis 与发布订阅
目录 一.订阅发布 常见应用 二.Redis 与订阅发布 三.SpringBoot 与订阅发布 A. 消息模型 B. 序列化 C. 发布消息 D. 接收消息 小结 一.订阅发布 订阅发布是一种常见的设 ...
- 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能
springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...
- ServiceStack.Redis订阅发布服务的调用(Z)
1.Redis订阅发布介绍Redis订阅发布是一种消息通信模式:发布者(publisher)发送消息,订阅者(Subscriber)接受消息.类似于设计模式中的观察者模式.发布者和订阅者之间使用频 ...
- ServiceStack.Redis订阅发布服务的调用
1.Redis订阅发布介绍 Redis订阅发布是一种消息通信模式:发布者(publisher)发送消息,订阅者(Subscriber)接受消息.类似于设计模式中的观察者模式. 发布者和订阅者之间使用频 ...
- Redis 订阅发布 - Jedis实现
Redis 订阅发布 - Jedis实现 我想到使用Redis的订阅发布模式是用来解决推送问题的-. 对于概念性的叙述,多多少少还是要提一下的: 什么是Redis发布订阅?Redis发布订阅是一种 ...
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
ASP.NET MVC 学习笔记-2.Razor语法 1. 表达式 表达式必须跟在“@”符号之后, 2. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...
- 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用
责任链模式的具体应用 1.业务场景 生产车间中使用的条码扫描,往往一把扫描枪需要扫描不同的条码来处理不同的业务逻辑,比如,扫描投入料工位条码.扫描投入料条码.扫描产出工装条码等,每种类型的条码位数 ...
- Springboot+Redis(发布订阅模式)跨多服务器实战
一:redis中发布订阅功能(http://www.redis.cn/commands.html#pubsub) PSUBSCRIBE pattern [pattern -]:订阅一个或者多个符合pa ...
- 2016022611 - redis订阅发布命令集合
redis消息订阅发布命令 参考地址:http://www.yiibai.com/redis/redis_pub_sub.html 消息发送者发送消息,通过redis的channal,消息接收者获取消 ...
随机推荐
- 记录阿里云ECS搭建Wordpress(Centos7+LAMP)
占位 改变wordpress文件夹属主属组 cd /var/www/html chown -R apache:apache wordpress <VirtualHost *:> Docum ...
- c# winForm DotNetBar控件之SuperGridControl
1.添加表头 sgc.PrimaryGrid.SelectionGranularity = SelectionGranularity.Row;//点击选中一行 DevComponents.DotNet ...
- python基础讲解部分&纯小白需要扎实基础
第一章知识点 一.Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum),在中国人称龟叔 Python崇尚优美.清晰.简单 应用领域: (1)云计算,写 ...
- <爬虫>相关的知识
1.概念.工具和HTTP 什么是爬虫 模拟客户端发送网络请求,获取响应,按照规则提取数据 爬虫的数据去哪了 展示到网页上(百度新闻,今日头条) 进行分析,从数据中寻找规律(指数网站:百度指数) 需要的 ...
- CF#541 D. Gourmet choice /// BFS 拓扑
题目大意: 给定n m 第一行有n个数 第二行有m个数 接下来n行每行m列 有 = < > 位于 i j 的符号表示 第一行第i个数与第二行第j个数的大小关系 1.将n+m个数 当做按顺序 ...
- Echart中X轴数据过多时横向拉动展示
chart.setOption( { tooltip: { trigger: 'axis' }, toolbox: { feature: { saveAsImage: {} } }, grid: { ...
- ZMQ相关
一般来说,做bind的是服务端,做connect的是客服端.zmq的bind和connect与我们通常的socket中bind和connect是不一样的,最起码的,我们它没有启动的先后顺序,而在我们通 ...
- ES6点点点运算符
1. rest(可变)参数 * 用来取代arguments 但比arguments灵活,只能是最后部分形参参数 function add(...values) { let sum = ; for(va ...
- Razor页面之添加TagHelper
1.右键项目-----添加------新建项 2.创建Razor试图导入 (Razor View Imports) 3.在创建的_ViewImports.cshtml中添加 @AddTagHelpe ...
- windows10 自动配置切换IP
办公室与家里的ip网段不一样.每次都要来回修改本地无线WLAN IP段.写了个脚本bat,自动配置WLAN IP,方便切换. 建立 auto-set-ip.bat,保存格式需为ANSI,否则中文会乱码 ...