SpringBoot使用Redis实现消息订阅

文章目录

    • 关联知识
    • 搭建项目环境
    • 创建监听者类
    • 配置类
    • 测试类发送消息
    • 测试结果

Redis不仅仅是一个优秀的非关系型缓存数据库,更是内置了一套消息机制。

关联知识

  • Redis安装及常用命令
  • Redis持久化
  • Redis实现商品秒杀
  • Redis实现排行榜

搭建项目环境


<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.0.4.RELEASEversion>
parent>

<dependencies>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-data-redisartifactId>
    dependency>
dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
        plugin>
    plugins>
build>

创建监听者类

监听者1

@Component
public class MyListener1 implements MessageListener {

    @Override
    public void onMessage(Message message, byte[] pattern) {
        System.out.println("MyListener1收到消息:"+message);
        System.out.println("Mylistener1:"+new String(pattern));
    }
}

监听者2

@Component
public class MyListener2 implements MessageListener {

    @Override
    public void onMessage(Message message, byte[] pattern) {
        System.out.println("MyListener2收到消息:"+message);
        System.out.println("Mylistener2:"+new String(pattern));
    }
}

配置类

@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurationSelector {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter MyListener1,
                                            MessageListenerAdapter MyListener2) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //订阅了一个叫chanel01的通道,这个container可以添加多个messageListener
        container.addMessageListener(MyListener1, new PatternTopic("Mylistener1"));
        //订阅了一个叫chanel02的通道
        container.addMessageListener(MyListener2, new PatternTopic("Mylistener2"));
        return container;
    }

    @Bean
    MessageListenerAdapter MyListener1() {
        return new MessageListenerAdapter(new MyListener1());
    }

    @Bean
    MessageListenerAdapter MyListener2() {
        return new MessageListenerAdapter(new MyListener2());
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate template = new StringRedisTemplate(factory);
        //定义value的序列化方式
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

测试类发送消息

@SpringBootTest
@RunWith(SpringRunner.class)
public class PubSubTest {
    
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void test01(){
        stringRedisTemplate.convertAndSend("Mylistener1","Redis发布的第一条消息");
        stringRedisTemplate.convertAndSend("Mylistener2","Redis发布的第二条消息");
    }
}

测试结果

MyListener2收到消息:Redis发布的第二条消息
Mylistener2:Mylistener2
MyListener1收到消息:Redis发布的第一条消息
Mylistener1:Mylistener1

你可能感兴趣的:(Spring,消息队列,数据库,redis,数据库,spring,boot,java)