首页 > 数据库 >redis消息队列——发布订阅

redis消息队列——发布订阅

时间:2023-09-23 11:46:30浏览次数:36  
标签:订阅 container 队列 redis springframework org import public

一、相关依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

 



二、redis 监听器配置

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
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
@EnableCaching
public class RedisConfig{
    /**
     * Redis消息监听器容器
     * @param connectionFactory
     * @return
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {

        RedisMessageListenerContainer container =  new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //订阅了一个叫pmp和channel 的通道,多通道
        container.addMessageListener(listenerAdapter( new RedisPmpSub()), new PatternTopic( "pmp" ));
        container.addMessageListener(listenerAdapter( new RedisChannelSub()), new PatternTopic( "channel" ));
        //这个container 可以添加多个 messageListener
        return container;
    }

    /**
     * 配置消息接收处理类
     * @param redisMsg  自定义消息接收类
     * @return
     */
    @Bean()
    @Scope( "prototype" )
    MessageListenerAdapter listenerAdapter(RedisMsg redisMsg) {
        //这个地方 是给messageListenerAdapter 传入一个消息接受的处理器,利用反射的方法调用“receiveMessage”
        //也有好几个重载方法,这边默认调用处理器的方法 叫handleMessage 可以自己到源码里面看
        return new MessageListenerAdapter(redisMsg,  "receiveMessage" ); //注意2个通道调用的方法都要为receiveMessage
    }

}

  

三、消息处理器

import org.springframework.stereotype.Component;
 
 
@Component
public interface RedisMsg {
 
    public void receiveMessage(String message);
}
 
 
public class RedisPmpSub implements RedisMsg{
 
    /**
     * 接收消息的方法
     * @param message 订阅消息
     */
    public void receiveMessage(String message){
        //注意通道调用的方法名要和RedisConfig2的listenerAdapter的MessageListenerAdapter参数2相同
        System.out.println(message);
    }
}

 

、消息发布者

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
//定时器
@EnableScheduling
@Component
public class TestSenderController {
    @Autowired
        private StringRedisTemplate stringRedisTemplate;
 
    //向redis消息队列index通道发布消息
    @Scheduled(fixedRate = 2000)
    public void sendMessage(){
        stringRedisTemplate.convertAndSend("pmp",String.valueOf(Math.random()));
        stringRedisTemplate.convertAndSend("channel",String.valueOf(Math.random()));
    }
}

 

备注: 使用策略模式优化消息处理器,模板化建立通道

参考链接 https://www.cnblogs.com/IT-study/p/11352254.html  

标签:订阅,container,队列,redis,springframework,org,import,public
From: https://www.cnblogs.com/kkvt/p/17724074.html

相关文章

  • 【POJ 1521】Entropy 题解(贪心算法+优先队列+哈夫曼树)
    熵编码器是一种数据编码方法,通过对删除了“浪费”或“额外”信息的消息进行编码来实现无损数据压缩。换句话说,熵编码去除了最初不需要的信息,以准确编码消息。高度的熵意味着一条消息包含大量浪费的信息;以ASCII编码的英文文本是具有极高熵的消息类型的示例。已经压缩的消息,如JPEG图......
  • Spring Boot中的消息队列集成
    介绍在现代应用程序中,消息队列已经成为了一种非常流行的解决方案,它可以帮助我们实现异步通信、解耦和扩展性。SpringBoot提供了对多种消息队列的集成支持,包括RabbitMQ、Kafka、ActiveMQ等。在本文中,我们将深入探讨SpringBoot中的消息队列集成。RabbitMQ集成RabbitMQ是一个流行......
  • Redis7 10大数据类型(Redis位域)
    一、是什么二、能干嘛位域修改溢出控制三、概述将一个redis字符串看作是一个由二进制位组成的数组并能对变长位宽和任意没有字节对齐的指定整型位域进行寻址和修改四、命令基本语法五、案例Ascii码表https://ascii.org.cn/基本命令代码实操BITFIELDkey[GETtypeoffset]BITFI......
  • k8s部署redis单节点
    创建pvc.yamlkind:PersistentVolumeClaimapiVersion:v1metadata:name:nfspvc1namespace:sqqqqspec:accessModes:-ReadWriteOnceresources:requests:storage:5GistorageClassName:nfs-storage创建redis-configmap.yamlkind:Confi......
  • redis多数据源
    参考:https://zhuanlan.zhihu.com/p/595032370<dependency><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId><version>6.1.6.RELEASE</version></dependency>示例importio.lettuce.cor......
  • Redis怎么设置过期时间
    pexpire(Stringkey,longmilliseconds):设置n毫秒后过期。expireAt(Stringkey,longunixTime):设置某个时间戳后过期(精确到秒)。pexpireAt(Stringkey,longmillisecondsTimestamp):设置某个时间戳后过期(精确到毫秒)。persist(Stringkey):移除过期时间。setkvexseconds......
  • Redis中是如何实现分布式锁的?
    分布式锁常见的三种实现方式:数据库乐观锁;基于Redis的分布式锁;基于ZooKeeper的分布式锁。本次面试考点是,你对Redis使用熟悉吗?Redis中是如何实现分布式锁的。要点Redis要实现分布式锁,以下条件应该得到满足互斥性在任意时刻,只有一个客户端能持有锁。不能死锁客......
  • JS实现任务队列
    引言假设有这么一个场景:前端订阅后台数据的变化,如果发生变化,则触发订阅回调;回调函数中,会执行一些耗时操作,如:请求接口,发送短信,存历史数据等;要求以上所有的操作都必须按照订阅触发的顺序执行;我们都知道,回调本身就是一种异步操作,我们仅仅依靠订阅回调无法保证回调中任务执行顺......
  • docker部署Redis
    标题:mac使用docker运行redis,并且在springboot中使用redis,怎么操作?0-前置配置:在mac下载好Docker:https://www.docker.com/Docker:启动Redis容器:在终端中执行以下命令,以启动Redis容器:终端命令:dockerrun--namemy-redis-p6379:6379-dredis//这......
  • redisde持久化机制
    他的持久化机制有两种一种是(rdb)快照一种是(aof)日志快照的话是全量的一个备份日志是连续的增加备份.快照机制是redis默认开启de,她会根据配置的策略将内存的数据保存在本地的二进制文件中官方提供两种方式生成快照一种是save命令但是有缺点会阻塞我们的主进程当如果数......