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. apache配置报错:Unrecognized LogFormat directive %I

    跟着阿里云调日志教程(https://help.aliyun.com/document_detail/87740.html)时出现报错: AH00526: Syntax error on line . ...

  2. Linux命令:unlias

    语法 unalias [-a] name [name ...] 说明 取消别名. 可以一次取消多个别名,写几个取消几个.不写,取消所有别名. 参数 -a 取消所有别名,不论后面是否跟一个还是多个nam ...

  3. Log4J2用法

    一.    关于Log4J 2015年5月,Apache宣布Log4J 1.x 停止更新.最新版为1.2.17. 如今,Log4J 2.x已更新至2.7. 官方网址:http://logging.ap ...

  4. weld

    weld - 必应词典 美[weld]英[weld] v.焊接:熔接:锻接:使紧密结合 n.焊接点:焊接处 网络焊缝

  5. [Java核心技术]第四章-对象与类(4.1-4.6总结)

    4.1面向对象程序设计概述 OOP(面向对象编程Object Oriented Programming) OOP中数据第一位,算法第二位. 类 封装:关键在于不能让其他方法直接访问类的实例域,程序仅通 ...

  6. 如何配置Java环境变量[转]

    https://jingyan.baidu.com/article/fd8044fa2c22f15031137a2a.html

  7. [leetcode]2. Add Two Numbers两数相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  8. spring整合kafka(配置文件方式 生产者)

    Kafka官方文档有   https://docs.spring.io/spring-kafka/reference/htmlsingle/ 这里是配置文件实现的方式 先引入依赖 <depend ...

  9. Piwis Tester II V18.100 with CF30 Laptop for Porsche

    Porsche Piwis Tester II is the latest professional tester for Porshe,the most poweful diagnose and o ...

  10. Spring 添加属性集中常见方法

    //创建容器,索要对象, package cn.lijun.Test; import org.junit.Test;import org.springframework.context.Applica ...