redis发布订阅
redis配置
# 配置redis连接池属性
spring.redis.jedis.pool.max-active=10
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.max-wait=2000
spring.redis.jedis.pool.min-idle=5
#配置redis服务器属性
spring.redis.port=6379
spring.redis.host=192.168.0.106
spring.redis.password=123456
#redis连接超时时间
spring.redis.timeout=1000
消息监听器
用来接收redis渠道发送过来的消息
RedisMessageListner
package com.lay.redis.listener;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
@Component
public class RedisMessageListner 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消息体: " + body);
System.out.println("topic渠道名称: " + topic);
}
}
onMessage
方法是得到消息后的处理方法。
message
参数代表redis发送过来的消息
pattern
是渠道名称
监听器配置
package com.lay.redis.config;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lay.redis.listener.RedisMessageListner;
@Configuration
@EnableCaching
public class BootRedisConfig {
//redis连接工厂
@Autowired
private RedisConnectionFactory redisConnectionFactory = null;
// redisTemplate
@Autowired
private RedisTemplate redisTemplate = null;
//redis消息监听器
@Autowired
private RedisMessageListner redisMessageListner = null;
//任务池
private ThreadPoolTaskScheduler taskScheduler = null;
/**
* 创建任务池,运行线程等待处理redis的消息
* @return
* @Date 2018年11月4日 下午10:11:02
* @Author lay
*/
@Bean
public ThreadPoolTaskScheduler initTaskScheduler() {
if (taskScheduler != null) {
return taskScheduler;
}
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(20);
this.taskScheduler = threadPoolTaskScheduler;
return threadPoolTaskScheduler;
}
/**
* 定义redis的监听容器
* @return 监听容器
* @Date 2018年11月4日 下午10:10:26
* @Author lay
*/
@Bean
public RedisMessageListenerContainer initRedisContainer() {
RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
//redis连接工厂
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
//设置运行任务池
redisMessageListenerContainer.setTaskExecutor(taskScheduler);
//定义监听渠道,名称为topic1
Topic topic = new ChannelTopic("topic1");
//使用监听器监听redis的消息
redisMessageListenerContainer.addMessageListener(redisMessageListner, topic);
return redisMessageListenerContainer;
}
}
测试
redis客户端里
publish topic1 msg
spring中也可以使用redisTemplate
redisTemplate.convertAndSend(channel,message);
channel是渠道,message是消息