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,消息接收者获取消 ...
随机推荐
- Spring Boot主要目标
Spring Boot主要目标 Spring Boot的主要目标是: 为所有Spring开发提供一个基本的,更快,更广泛的入门体验. 开箱即用,但随着需求开始偏离默认值,快速启动. 提供大型项目(例如 ...
- xshell6 远程连接时提示 WARNING! The remote SSH server rejected X11 forwarding request.
1.输入命令 vi /etc/ssh/sshd_config ,修改配置文件 2.将UserLogin no 的注释解除 3.执行yum install -y xorg-x11-font xorg ...
- centos7下的nfs配置
NFS是Network File System的缩写,即网络文件系统.客户端通过挂载的方式将NFS服务器端共享的数据目录挂载到本地目录下. nfs为什么需要RPC?因为NFS支持的功能很多,不同功能会 ...
- vue与webpack
由于最近在vue-cli生成的webpack模板项目的基础上写一个小东西,开发过程中需要改动到build和config里面一些相关的配置,所以刚好趁此机会将所有配置文件看一遍,理一理思路,也便于以后修 ...
- ajax的两种应用方式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Html+css编写太阳星系
我们都知道太阳系是以太阳为中心的,和所有受到太阳的引力约束天体的集合体.包括八大行星(由离太阳从近到远的顺序:水星.金星.地球.火星.木星.土星.天王星.海王星),而我用html和css所写的就是八大 ...
- 格式化抽象本地地址(实战linux socket编程)
格式化抽象本地地址传统AF_UNIX套接口名字的麻烦之一就在于总是调用文件系统对象.这不是必须的,而且也不方便.如果原始的文件系统对象并没有删除,而在bind调用时使用相同的文件名,名字赋值就会失败. ...
- 基本TCP Sockets编程
一.socket 函数 #include <sys/socket.h> int socket (int family, int type, int protocol); Returns: ...
- 二分法的应用:最大化最小值 POJ2456 Aggressive cows
/* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- BASS HOME
http://www.un4seen.com/