栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

                            监听适配器绑定对应的类和方法,通过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;


@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;
    }

    
    @Bean
    MessageListenerAdapter listenerAdapter1(RedisController redisController) {
        return new MessageListenerAdapter(redisController, "receiveMessage1");
    }

    
    @Bean
    MessageListenerAdapter listenerAdapter2(RedisController redisController) {
        return new MessageListenerAdapter(redisController, "receiveMessage2");
    }


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


}

消息发布者

    @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");
    }

}

 输出结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/685324.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号