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,消息接收者获取消 ...
随机推荐
- 最小生成树--Prim及Kruskal
//prim算法#include<cstdio> #include<cmath> #include<cstring> #include<iostream> ...
- 屏幕操作录制成gif图的技巧
我呢,在记录一些做过得实例的时候,总需要上一两张效果图,截静态图太浪费时间了,于是就找了一些录制git图的软件 一.Gif动画录制工具 这是我在360软件中心下载的,用了一下,不好用,录制出来的图是黑 ...
- shiro注解权限拦截失效解决
今天在给项目添加注解拦截的时候,总是发现拦截不起作用,加入了这个注解按照角色来进行拦截.角色不是1的用户访问此方法都会被拦截. 测试了一会发现把注解bean配置到了shiro.xml中没有起作用.需要 ...
- jdk源码阅读-ConcurrentLinkedQueue(一)
说明 concurrentLinkedQueue为无界非阻塞队列,是线程安全的 内部结构为链表的形式, 内部使用cas保存线程安全.采用cas保证原子性 什么是CAS CAS 操作包含三个操作数 —— ...
- 小波变换C代码
#include <stdio.h> #include <stdlib.h> #define LENGTH 512//信号长度 /*********************** ...
- CentOS提示::unknown filesystem type 'ntfs'自动挂载NTFS分区的U盘或者移动硬盘
CentOS默认源里没有NTFS-3G,想要添加ntfs支持,无非是自己下载编译安装或者加源yum安装.重新安装了一个CentOS7,用的是添加aliyun的epel源来yum安装的方式,简单易行. ...
- vue项目 (1) vue结合mini-ui 的使用
1.安装 npm install mini-ui -S 2. 使用 import Vue from 'vue' import App from './App' import router from ' ...
- nodejs的npm修改源
在国内用npm install时,速度总是不尽如人意,解决办法是修改npm的数据源 npm config set registry https://registry.npm.taobao.org 修改 ...
- Java ----单个list 删除元素
转载:https://www.cnblogs.com/lostyears/p/8809336.html 方式一:使用Iterator的remove()方法 public class Test { pu ...
- Go 关系运算符
Go 关系运算符 package main import "fmt" func main() { var a int = 21 var b int = 10 if( a == b ...