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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值