redis 实现发布订阅的功能
redis 除了作为缓存的功能外还可以用作消息中间件的功能,这片博客主要是介绍一下 redis 整合spring 实现消息的发布和订阅功能;
1:redis依赖,依赖两个包,redis 包, spring-redis 包用于整合redis,这里就不介绍了,具体可以参考上一篇博客 :redis 缓存 中的介绍;
2:redis和spring的整合:
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="1" />
<property name="maxTotal" value="5" />
<property name="blockWhenExhausted" value="true" />
<property name="maxWaitMillis" value="30000" />
<property name="testOnBorrow" value="true" />
</bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost" />
<property name="port" value="6379"/>
<property name="poolConfig" ref="jedisPoolConfig" />
<property name="usePool" value="true"/>
</bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean> <!-- redis 消息发布订阅 -->
<bean id="myRedisListener" class="bz.beppe.redis.MyRedisListener" scope="prototype"/>
<!-- 这里配置线程池任务 -->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="4"/>
<property name="maxPoolSize" value="4"/>
<property name="queueCapacity" value="100000"/>
</bean>
<!-- 配置redis container 将监听类注入到redis容器中,实现监听容器中指定 主题 队列的功能 -->
<bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer"
destroy-method="destroy">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="taskExecutor">
<ref bean="taskExecutor" />
</property>
<property name="messageListeners">
<map>
<entry key-ref="myRedisListener">
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="push:myredis"/>
</bean>
</entry>
</map>
</property>
</bean>
3:监听类 该类只要实现 MessageListener 接口即可,并且在重写的方法中进行需要的业务处理:
public class MyRedisListener implements MessageListener{
@Override
public void onMessage(Message message, byte[] pattern) {
byte[] body = message.getBody();
byte[] channel = message.getChannel();
// redisTemplate.convertAndSend("push:myredis","this is the redis subscribe!!");
String channelStr = new String(channel);
String bodyStr = new String(body);
System.out.println("渠道为:"+channelStr+"消息为:"+bodyStr); //这里的业务处理只做简单的打印输出
}
}
4:消息发布类:
在消息发布类中需要使用到 redisTemplate 来进行消息的发布,其中,消息发布的方法为 redisTemplate.convertAndSend(String channel,String mess);
需要指定消息发布的 通道名称,这里的通道和监听中配置的渠道名称一致 ;mess 就是你需要发布到该通道上的内容;
5:消息的发布:
@Test
public void redisSubTest() throws InterruptedException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-redis.xml");
RedisTemplate redisTemplate = (RedisTemplate) ctx.getBean("redisTemplate");
redisTemplate.convertAndSend("push:myredis","this is the redis subscribe!!");
Thread.sleep(5000);
}
到这里为止,一个简单的基于redis的订阅发布就实现了,在项目中你可以根据你的具体业务来实现功能
redis 实现发布订阅的功能的更多相关文章
- 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能
springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...
- StackExchange.Redis 使用-发布订阅 (二)
使用Redis的发布订阅功能 redis另一个常见的用途是发布订阅功能 . 它非常的简单 ,当连接失败时 ConnectionMultiplexer 会自动重新进行订阅 . ISubscriber s ...
- .net core 使用Redis的发布订阅
Redis是一个性能非常强劲的内存数据库,它一般是作为缓存来使用,但是他不仅仅可以用来作为缓存,比如著名的分布式框架dubbo就可以用Redis来做服务注册中心.接下来介绍一下.net core 使用 ...
- java实现 redis的发布订阅(简单易懂)
redis的应用场景实在太多了,现在介绍一下它的几大特性之一 发布订阅(pub/sub). 特性介绍: 什么是redis的发布订阅(pub/sub)? Pub/Sub功能(means Publ ...
- redis的发布订阅、持久化存储、redis的主从复制
redis的发布订阅 1. 创建redis配置文件 vim /opt/redis_conf/reids-6379.conf mkdir /data/6379 redis-server redis-6 ...
- Redis之发布订阅
一 什么是发布订阅 发布订阅模式又叫观察者模式,它定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖它的对象都将得到通知 Redis 发布订阅(pub/sub)是一种消息通信模式: ...
- [翻译] C# 8.0 新特性 Redis基本使用及百亿数据量中的使用技巧分享(附视频地址及观看指南) 【由浅至深】redis 实现发布订阅的几种方式 .NET Core开发者的福音之玩转Redis的又一傻瓜式神器推荐
[翻译] C# 8.0 新特性 2018-11-13 17:04 by Rwing, 1179 阅读, 24 评论, 收藏, 编辑 原文: Building C# 8.0[译注:原文主标题如此,但内容 ...
- python之上下文管理、redis的发布订阅、rabbitmq
使用with打开文件的方式,是调用了上下文管理的功能 #打开文件的两种方法: f = open('a.txt','r') with open('a.txt','r') as f 实现使用with关闭s ...
- 【spring boot】【redis】spring boot 集成redis的发布订阅机制
一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...
随机推荐
- 【javascript知识温习】设计模式--单例模式
var Singleton = (function(){ var instance; function init() { '; function privateMethod() { console.l ...
- Android 查阅博客2_APT
https://mp.weixin.qq.com/s/3zrAzOUGpovRRbuYnce3uw APT(Annotation Processing Tool) 即注解处理器,是一种注解处理工具,用 ...
- Selenium Java关闭浏览器
在学习selenium的过程中发现一个问题,各种博客/教程都是教人用selenium的quit()和close()方法关闭浏览器. 但这不是我要的结果.这两个方法的前提是,用webdriver打开浏览 ...
- AC自动机解题记录
1.HDU 2222 Keywords Search 模板题 #include <bits/stdc++.h> #define fir first #define sec second # ...
- cisco PBR
access-list 2000 permit ip 10.11.50.0 0.0.0.255 anyaccess-list 2001 permit ip 10.11.50.0 0.0.0.255 1 ...
- 创建的vue项目出错的时候,提示This dependency was not found错误的处理方法
错误如图所示: 解决方法:npm install stylus-loader css-loader style-loader --save-dev
- ES6自我总结笔记(阮一峰ES6入门)
[let和const命令] 1.var的作用域是函数体内,不是块级作用域 2.let是更完美的var,let的变量的作用是块级作用域 3.let声明的全局变量不是全局对象属性,不可以通过window. ...
- Spring源码-循环依赖源码解读
Spring源码-循环依赖源码解读 笔者最近无论是看书还是从网上找资料,都没发现对Spring源码是怎么解决循环依赖这一问题的详解,大家都是解释了Spring解决循环依赖的想法(有的解释也不准确,在& ...
- 学习UI设计书籍推荐
在学习UI设计的过程当中,特别想学或者零基础的人来说,需要学习到很多知识,比如软件 PS AI ,理论 色彩 排版 规范 UE 等,这些都是一名UI设计师需要学习的知识,而学习到这些知识,可以通过视频 ...
- AX_HelpGenerator
HelpGenerator helpGenerator; ; helpGenerator = infolog.helpGenerator(); helpGenerator.showURL(" ...