1. 程式人生 > >Redis釋出訂閱

Redis釋出訂閱

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是訊息