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 实现发布订阅的功能的更多相关文章

  1. 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能

    springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...

  2. StackExchange.Redis 使用-发布订阅 (二)

    使用Redis的发布订阅功能 redis另一个常见的用途是发布订阅功能 . 它非常的简单 ,当连接失败时 ConnectionMultiplexer 会自动重新进行订阅 . ISubscriber s ...

  3. .net core 使用Redis的发布订阅

    Redis是一个性能非常强劲的内存数据库,它一般是作为缓存来使用,但是他不仅仅可以用来作为缓存,比如著名的分布式框架dubbo就可以用Redis来做服务注册中心.接下来介绍一下.net core 使用 ...

  4. java实现 redis的发布订阅(简单易懂)

    redis的应用场景实在太多了,现在介绍一下它的几大特性之一   发布订阅(pub/sub). 特性介绍: 什么是redis的发布订阅(pub/sub)?   Pub/Sub功能(means Publ ...

  5. redis的发布订阅、持久化存储、redis的主从复制

    redis的发布订阅 1. 创建redis配置文件 vim /opt/redis_conf/reids-6379.conf mkdir /data/6379 redis-server  redis-6 ...

  6. Redis之发布订阅

    一 什么是发布订阅 发布订阅模式又叫观察者模式,它定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖它的对象都将得到通知 Redis 发布订阅(pub/sub)是一种消息通信模式: ...

  7. [翻译] C# 8.0 新特性 Redis基本使用及百亿数据量中的使用技巧分享(附视频地址及观看指南) 【由浅至深】redis 实现发布订阅的几种方式 .NET Core开发者的福音之玩转Redis的又一傻瓜式神器推荐

    [翻译] C# 8.0 新特性 2018-11-13 17:04 by Rwing, 1179 阅读, 24 评论, 收藏, 编辑 原文: Building C# 8.0[译注:原文主标题如此,但内容 ...

  8. python之上下文管理、redis的发布订阅、rabbitmq

    使用with打开文件的方式,是调用了上下文管理的功能 #打开文件的两种方法: f = open('a.txt','r') with open('a.txt','r') as f 实现使用with关闭s ...

  9. 【spring boot】【redis】spring boot 集成redis的发布订阅机制

    一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...

随机推荐

  1. T-SQL中的APPLY用法

    原文出处:http://www.sqlservercentral.com/articles/Stairway+Series/121318/ 从SQL Server 2005开始,微软添加了一个新的运算 ...

  2. PostgreSQL使用笔记

    下载并安装 注意安装图形界面 pgAdmin 需要输入缺省用户 postgres 的密码 在 Windows 下安装之后注意把 bin文件夹加到 Path 环境变量中. 重置密码 使用管理员权限打开 ...

  3. nexus的安装和简介

    下载nexus Nexus 是Maven仓库管理器,通过nexus可以搭建maven仓库,同时nexus还提供强大的仓库管理功能,构件搜索功能等. 下载Nexus, 下载地址:http://www.s ...

  4. 利用PHPExcel导出excel 以及利用js导出excel

    导出excel的方法output_excel需要依赖PHPExcel 导出csv的方法csv_export不需要 <?php /** * @author ttt */ class ExcelCo ...

  5. 记一次线上bug排查-quartz线程调度相关

    记一次线上bug排查,与各位共同探讨. 概述:使用quartz做的定时任务,正式生产环境有个任务延迟了1小时之久才触发.在这一小时里各种排查找不出问题,直到延迟时间结束了,该任务才珊珊触发.原因主要就 ...

  6. android studio 创建图标

    参考 https://www.cnblogs.com/c546170667/p/5975550.html File-New-Image Asset

  7. [leetcode]75. Sort Colors三色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  8. python的基本用法(一)

    1.什么变量,什么是数据类型? 变量就是用来存放东西的, 数据类型:字符串str,整数int,浮点数(小数)float type()函数用来检验数据格式的类型 2.什么是for循环,什么是while循 ...

  9. Spring Boot+Quartz实现一个实时管理的定时任务

    转载 https://www.cnblogs.com/wujiwen/p/9615120.html 项目实践过程中碰到一个动态管理定时任务的需求:针对每个人员进行信息的定时更新,具体更新时间可随时调整 ...

  10. RNA-seq数据综合分析教程 AKAP95

    https://blog.csdn.net/l_yivs?t=1 RNA-seq数据综合分析教程 2 4,055 A+ 所属分类:Transcriptomics   收  藏 2     RNA-se ...