前言
我們可以在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好帥!");
}
}
訪問:
接收成功!