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,消息接收者获取消 ...
随机推荐
- HTML中列表中元素居中问题
想让列表<table>中某一行元素居中的话,单纯使用<align = 'center'>是达不到效果的,需要先将该行进行合并,使用colspan属性. <html> ...
- CM集群管理
用CM装完cdh 版本后,将不同机架的服务器已经认为拆掉网线,但是主机管理那边还是显示已经下载的服务器存在. 如图: 该怎么配置 node8 和node9会自动消失: DataNode已经显示 ...
- JS:面向对象(基础篇)
面向对象(Object-Oriented,OO)的语言有一个标志,那就是它们都有类的概念.long long ago,js是没有类的概念(ES6推出了class,但其原理还是基于原型),但是它是基于原 ...
- 商城sku的选择功能--客户端
前段时间,刚好做到了有关sku这个功能.客户端的sku,和后台管理系统的sku.当初查了大量资料,遂做个记录,以免忘记. 这篇先写客户端的sku功能把,类似于去淘宝京东等购物,就会有个规格让你选择.如 ...
- SDL系列之 - 字体显示测试
例9.7:设计一个程序,初始化视频子系统,设置显示模式为640*480,表面的色深为16位,使用SDL_ttf库在屏幕上显示“Linux下TrueType字体显示示例”,字体大小为38,颜色为红色.设 ...
- python接口自动化(put请求)
python接口自动化(put请求) 一.put请求的作用:更新资源 二.应用 导包:import requests 调用requests.put()方法 参数有URL.data.headers,方法 ...
- AVR446步进电机算法推导及应用
https://blog.csdn.net/Renjiankun/article/details/80513839?utm_source=copy
- 驾驶心率和呼吸,疲劳检测系统,通过安全带,坐垫等内置sensor
HARKEN - Heart and respiration in car embedded nonintrusive sensors 2 https://www.youtube.com/watch? ...
- 解决vi显示文件不能全屏的问题
https://blog.csdn.net/ly890700/article/details/52735092 docker外: vi ~/.vimrc
- NOI2019赛前两周被吊打记录
7.1 T1看了半天发现会个暴力FWT,然后突然返发现随便容斥一下就好了 T2猜了个只有13和23的,结果打个表发现伪了,然后标号不只一种连搜索都写错了,也没想过可以轮廓线dp,菜哭了o(╥﹏╥)o ...