前言

我们可以在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. python根据窗口标题找句柄,将窗口前置活动

    import time, threading, copy import win32api, win32con import win32gui import win32gui def zhaojb(aa ...

  2. 【动画消消乐】HTML+CSS 自定义加载动画:怦然心跳 066

    前言 Hello!小伙伴! 非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出-   自我介绍 ଘ(੭ˊᵕˋ)੭ 昵称:海轰 标签:程序猿|C++选手|学生 简介:因C语言结识编程,随后转入计 ...

  3. 第十篇 -- 学习C++宝典2005版

    最近看了C++宝典,看时间是2005的,对于里面的程序自己也进行了编写,由于时间过久,可能有些函数的用法发生了改变,自己也对其进行了修改,用VS2017可以编译通过. 前四章学习内容 CPlusPlu ...

  4. 1.4matlab矩阵的表示

    1.4matlab矩阵的表示 矩阵的建立 利用直接输入法建立矩阵:将矩阵的元素用中括号括起来,按矩阵的顺序输入各元素,同一行的各元素之间用逗号或空格分隔,不同行的元素之间用分号分隔. 利用已建立好的矩 ...

  5. labview系列-中级计算器开发

    本例子通过对中级计算器的操练,实现对结构/字符串等基础知识的掌握和理解,为后续的编程工作提供基础. 计算器开发原理:通过按钮触发事件,再各个事件中编写相应加减乘除方法,并显示在结果中即可. 要点:临时 ...

  6. xshell中操作服务器笔记

    sudo su 获取root权限 cd 切换到相应文件夹 ll ls 查看文件夹内容 cp file folder 复制文件到文件夹 \cp为强制覆盖不提示 cp -r /packageA/* /cp ...

  7. Thinkphp 分页应用

    $Table = M('Table'); $count = $Table ->where()->count();        $Page = new \Think\Page($count ...

  8. 异地远程访问群晖NAS中的文件

    异地远程访问群晖NAS中的文件   我以群晖DS720+网络存储服务器为例,介绍我是如何异地远程访问群晖NAS中的文件的.   此文章只介绍部署操作的大概步骤,具体的操作方法和技巧可以在西瓜视频.抖音 ...

  9. python aes pkcs7加密

    # -*- coding: UTF-8 -*- from Crypto.Util.Padding import pad from Crypto.Cipher import AES import bas ...

  10. js hook

    //cookie hook (function () { 'use strict'; var cookie_cache = document.cookie; Object.defineProperty ...