SpringBoot-Redis订阅发布-消息队列

本文介绍了如何在Spring Boot中配置Redis消息队列,创建发布者和订阅者,通过监听适配器实现消息监听与消费。示例代码展示了如何使用RedisTemplate发送消息,并在RedisController中定义接收消息的方法。
摘要由CSDN通过智能技术生成

消息队列: 创建发布者-----创建订阅者-----配置监听器-----监听队列-----配置监听适配器

                            监听适配器绑定对应的类和方法,通过java反射进行订阅消费

配置监听器-----监听队列-----配置监听适配器

package com.example.demo.config;

import com.example.demo.controller.RedisController;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Description:redis配置类
 * @Author: ZhaoYaJun
 * @Date: 2021/01/25
 */
@Configuration
public class RedisConfig {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter1,
                                            MessageListenerAdapter listenerAdapter2) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 每一个监听器messageListener都需要一个适配器,队列可以是同一个
        container.addMessageListener(listenerAdapter1, new PatternTopic("redis:test"));
        container.addMessageListener(listenerAdapter2, new PatternTopic("redis:prod"));
        return container;
    }

    /**
     * 消息监听器适配器,绑定消息处理器,利用反射技术调用消息处理器的业务方法
     * @return
     */
    @Bean
    MessageListenerAdapter listenerAdapter1(RedisController redisController) {
        return new MessageListenerAdapter(redisController, "receiveMessage1");
    }

    /**
     * 消息监听器适配器,绑定消息处理器,利用反射技术调用消息处理器的业务方法
     * @return
     */
    @Bean
    MessageListenerAdapter listenerAdapter2(RedisController redisController) {
        return new MessageListenerAdapter(redisController, "receiveMessage2");
    }


    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }


}

消息发布者

/**
     * 消息发布者
     * @return
     */
    @RequestMapping("/redisTest")
    public String redisTest() {
        for(int i = 1; i <= 5; i++) {
            redisTemplate.convertAndSend("redis:test","通知你该开始了");
        }
        redisTemplate.convertAndSend("redis:prod","通知你该结束了");
        return "";
    }

消息订阅者

package com.example.demo.controller;

import org.springframework.stereotype.Component;

@Component
public class RedisController {

    public void receiveMessage1(String message) {
        System.out.println(message+":收到");
    }

    public void receiveMessage2(String message) {
        System.out.println(message+":OK");
    }

}

 输出结果

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后端三郎@ZYJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值