redis-PatternTopic

redis 发布订阅

1.0 Redis配置

开启队列配置
spring:
  redis:
    topics:
      enable: false
package com.connext.soa.platform.price.config;

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.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
        // value值的序列化采用fastJsonRedisSerializer
        template.setValueSerializer(jsonSerializer);
        template.setHashValueSerializer(jsonSerializer);
        // key的序列化采用StringRedisSerializer
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

1.1 生产者

 public void carryOnOrderedQueue(Long userId) {
        redisTemplate.convertAndSend(SUB_KEY, userId.toString());
 }

1.2 初始化监听

package com.connext.soa.platform.price.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
@Slf4j
public class RedisSubConfig {
    public static final String SUB_KEY = "DROOLS:MESSAGE";

    @Bean(name = "droolsContainer")
    RedisMessageListenerContainer droolsContainer(RedisConnectionFactory connectionFactory, @Qualifier("droolsListenerAdapter") MessageListenerAdapter droolsListenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(droolsListenerAdapter, new PatternTopic(SUB_KEY));
        return container;
    }

    @Bean(name = "droolsListenerAdapter")
    MessageListenerAdapter droolsListenerAdapter(MessageSubListener redisPubSubListener) {
        log.info("drools listenerAdapter register");
        return new MessageListenerAdapter(redisPubSubListener, "onMessage");
    }
}

1.2 消费者

package com.connext.soa.platform.price.listener;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

/**
 * @author maple Yu
 * @version 1.0
 * @date 2023/9/4 下午5:36
 */
@Component
@Slf4j
public class MessageSubListener implements MessageListener {

    @Override
    public void onMessage(Message message, byte[] bytes) {
        String messageBody = new String(message.getBody());
        String msgResult = StringEscapeUtils.unescapeJava(messageBody);
        log.info("onMessage.msgResult {} {}", msgResult, messageBody);
    }
}

1.3 执行结果

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值