SpringBoot监听redis订阅监听和发布订阅

编程

redis订阅监听配置类

 

image.png

代码如下:
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")

publicclass 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;

}

}

监听类

 

image.png

RedisMessageListener.java

packagecom.wzq.redis.config;

importorg.springframework.data.domain.Page;

importorg.springframework.data.redis.connection.Message;

importorg.springframework.data.redis.connection.MessageListener;

importorg.springframework.stereotype.Component;

/**

* @description: redis监听类

* @author: Wzq

* @create: 2019-12-23 15:58

*/

@Component

publicclassRedisMessageListenerimplementsMessageListener{

@Override

publicvoidonMessage(Message message,byte[] pattern){

//消息

String body =newString(message.getBody());

//渠道名称

String topic =newString(pattern);

System.out.println(body);

System.out.println(topic);

}

}

发布订阅(有两种方式)

1.使用redis命令行发布

命令:

PUBLISH topic1 hello!

 

image.png

 

image.png

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好帅!");

}

}

访问:

 

 

image.png

接收成功!

 

image.png

个人微信公众,经常更新一些实用的干货:

 

 

以上是 SpringBoot监听redis订阅监听和发布订阅 的全部内容, 来源链接: utcz.com/z/513263.html

回到顶部