前言

我们可以在redis中发布一条订阅到通道中,所有监听了这个通道的都可以收到这个发布的内容!

redis订阅监听配置类

代码如下:

RedisListenerConfig.java

package com.wzq.redis.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
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; /**
* @description: redis监听器配置
* @author: Wzq
* @create: 2019-12-23 15:47
*/
@Configuration(value = "RedisListenerConfigTopic")
public class RedisListenerConfig { //redisTemplate
@Autowired
private RedisTemplate redisTemplate; //redis连接工厂
@Autowired
private RedisConnectionFactory connectionFactory; //redis 消息监听器
@Autowired
private MessageListener redisMsgListener; //任务池
private ThreadPoolTaskScheduler taskScheduler; /**
*@Description 创建任务池,运行线程等待处理redis消息
*@Param []
*@Return org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
*@Author Wzq
*@Date 2019/12/23
*@Time 15:51
*/
@Bean
public ThreadPoolTaskScheduler iniTaskScheduler(){
if(taskScheduler != null){
return taskScheduler;
}
taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(20);
return taskScheduler;
} /**
*@Description 定义Redis的监听容器
*@Param []
*@Return org.springframework.data.redis.listener.RedisMessageListenerContainer
*@Author Wzq
*@Date 2019/12/23
*@Time 15:52
*/
@Bean
public RedisMessageListenerContainer initRedisContainer(){
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
//redis 连接工厂
container.setConnectionFactory(connectionFactory);
//设置运行任务池
container.setTaskExecutor(iniTaskScheduler());
//定义监听渠道,名称为topic1
Topic topic = new ChannelTopic("topic1");
//定义监听器监听的Redis的消息
container.addMessageListener(redisMsgListener,topic);
return container;
} }

监听类

RedisMessageListener.java

package com.wzq.redis.config;

import org.springframework.data.domain.Page;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component; /**
* @description: redis监听类
* @author: Wzq
* @create: 2019-12-23 15:58
*/
@Component
public class RedisMessageListener implements MessageListener {
@Override
public void onMessage(Message message, byte[] pattern) {
//消息
String body = new String(message.getBody());
//渠道名称
String topic = new String(pattern);
System.out.println(body);
System.out.println(topic);
}
}

发布订阅(有两种方式)

1.使用redis命令行发布

命令:

PUBLISH topic1 hello!

2.使用redisTemplate对象发布

redisTemplate.convertAndSend("topic1","wzq好帅!");

详细代码TestController.java

package com.wzq.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @description:
* @author: Wzq
* @create: 2019-12-23 16:16
*/
@RestController
public class Test { @Autowired
StringRedisTemplate redisTemplate; @GetMapping(value = "/test")
public void test(){
redisTemplate.convertAndSend("topic1","wzq好帅!");
} }

访问:

接收成功!

SpringBoot监听redis订阅监听和发布订阅的更多相关文章

  1. php redis pub/sub(Publish/Subscribe,发布/订阅的信息系统)之基本使用

    一.场景介绍 最近的一个项目需要用到发布/订阅的信息系统,以做到最新实时消息的通知.经查找后发现了redis pub/sub(发布/订阅的信息系统)可以满足我的开发需求,而且学习成本和使用成本也比较低 ...

  2. redis源码分析之发布订阅(pub/sub)

    redis算是缓存界的老大哥了,最近做的事情对redis依赖较多,使用了里面的发布订阅功能,事务功能以及SortedSet等数据结构,后面准备好好学习总结一下redis的一些知识点. 原文地址:htt ...

  3. redis教程(一)-----redis数据类型、基本命令、发布订阅以及持久化

    简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMwa ...

  4. Redis事务、持久化、发布订阅

    一.Redis事物 1. 概念 Redis 事务可以一次执行多个命令, 并且带有以下两个重要的保证: 事务是一个单独的隔离操作:事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他 ...

  5. Redis 4.x 安装及 发布/订阅实践和数据持久化设置

    1.或者源码安装包 #wget http://download.redis.io/releases/redis-4.0.6.tar.gz 2.解压源码包 #tar -zxf redis-4.0.6.t ...

  6. SpringBoot进阶教程(二十九)整合Redis 发布订阅

    SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...

  7. SpringBoot Redis 发布订阅模式 Pub/Sub

    SpringBoot Redis 发布订阅模式 Pub/Sub 注意:redis的发布订阅模式不可以将消息进行持久化,订阅者发生网络断开.宕机等可能导致错过消息. Redis命令行下使用发布订阅 pu ...

  8. Redis 发布订阅,小功能大用处,真没那么废材!

    今天小黑哥来跟大家介绍一下 Redis 发布/订阅功能. 也许有的小伙伴对这个功能比较陌生,不太清楚这个功能是干什么的,没关系小黑哥先来举个例子. 假设我们有这么一个业务场景,在网站下单支付以后,需要 ...

  9. Spring Data Redis实现消息队列——发布/订阅模式

    一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式.利用redis这两种场景的消息队列都能够实现. 定义:生产者消费者模式:生产者生产消息放到队列里,多个消费者同时监听队列, ...

  10. redis实现消息队列&发布/订阅模式使用

    在项目中用到了redis作为缓存,再学习了ActiveMq之后想着用redis实现简单的消息队列,下面做记录.   Redis的列表类型键可以用来实现队列,并且支持阻塞式读取,可以很容易的实现一个高性 ...

随机推荐

  1. 如何修改Windows 11 任务栏大小

    1.首先Win+R输入regedit打开注册表编辑器         2.进入注册表编辑器后,在地址栏中输入: HKEY_CURRENT_USER\Software\Microsoft\ Window ...

  2. AspNetCore&MassTransit Courier实现分布式事务

    在之前的一篇博文中,CAP框架可以方便我们实现非实时.异步场景下的最终一致性,而有些用例总是无法避免的需要在实时.同步场景下进行,可以借助Saga事务来解决这一困扰.在一些博文和仓库中也搜寻到了.Ne ...

  3. UI自动化测试框架Gauge 碰到无法识别Undefined Steps 红色波纹标记

    如果碰到无法识别的情况,例如下面的红色波纹,可以试一下: 第一步: 第二步: 不勾选'offline work' 第三部:刷新之后可以重新编译.

  4. JAVA 中日志的记录于使用

    java中常用的日志框架 日志接口 Commons Logging Apache Commons Logging是一个基于Java的日志记录实用程序,是用于日志记录和其他工具包的编程模型.它通过其他一 ...

  5. C语言:总结

    1除法运算:两整数相除,结果为整数: 任意浮点数参与的除法运算结果为浮点型.所以pow(16,1/2)=1   pow(16,1.0/2)=4.00  pow(64,1.0/3)=4.00 球的体积v ...

  6. PAT甲级:1025 PAT Ranking (25分)

    PAT甲级:1025 PAT Ranking (25分) 题干 Programming Ability Test (PAT) is organized by the College of Comput ...

  7. 手写一个超简单的Vue

    基本结构 这里我根据自己的理解模仿了Vue的单文件写法,通过给Vue.createApp传入参数再挂载元素来实现页面与数据的互动. 其中理解不免有错,希望大佬轻喷. 收集数据 这里将Vue.creat ...

  8. NOI2021游记

    NOI2021游记 前言 写于 2021.7.28,成绩榜刚出后几个小时.总分 345 拿到银牌 183 名. 我的高中 OI 生活在这里画上句号.结局对我而言虽然不够完美,但是无论怎样都是我人生道路 ...

  9. python 实现自动部署测试环境

    预设条件 产品运行在Linux CentOS6 X64上 python3,Djanggo,Cherrypy安装好手动安装过程 登录服务器 检查是否有以前的版本的产品在运行,有,停掉 如果有原来的代码包 ...

  10. 图解java多线程设计模式之一一synchronized实例方法体

    synchronized实例方法体和synchronized代码块 synchronied void method(){ ....... } 这个等同于下面将方法体用synchronized(this ...