Spring-data-redis: pub/sub消息订阅

 Redis中pub/sub特性,可以用来实现类似与JMS的“topic”功能,只不过这些消息无法被持久化而已。spring-data-redis组件中对pub/sub提供了类似JMS的编程模式,我们通过实例来展示如何使用。

    需要注意的是,在redis中消息的订阅端(subscribe)需要独占链接,那么消息接收将是阻塞的。

    代码实例中,使用了“连接池”/“消息异步接受”“消息并发处理”,请根据需要调整相关参数。

    1) Redis中"pub/sub"的消息,为"即发即失",server不会保存消息,如果publish的消息,没有任何client处于"subscribe"状态,消息将会被丢弃.如果client在subcribe时,链接断开后重连,那么此期间的消息也将丢失.Redis server将会"尽力"将消息发送给处于subscribe状态的client,但是仍不会保证每条消息都能被正确接收.

    2) 如果期望pub/sub的消息时持久的,那么需要借助额外的功能.参见"pub/sub持久化订阅"

 

一.配置文件

 

Java代码   收藏代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"   
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName">  
  4.     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
  5.         <property name="maxActive" value="32"></property>  
  6.         <property name="maxIdle" value="6"></property>  
  7.         <property name="maxWait" value="15000"></property>  
  8.         <property name="minEvictableIdleTimeMillis" value="300000"></property>  
  9.         <property name="numTestsPerEvictionRun" value="3"></property>  
  10.         <property name="timeBetweenEvictionRunsMillis" value="60000"></property>  
  11.         <property name="whenExhaustedAction" value="1"></property>  
  12.     </bean>  
  13.     <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">  
  14.         <property name="poolConfig" ref="jedisPoolConfig"></property>  
  15.         <property name="hostName" value="127.0.0.1"></property>  
  16.         <property name="port" value="6379"></property>  
  17.         <property name="password" value="0123456"></property>  
  18.         <property name="timeout" value="15000"></property>  
  19.         <property name="usePool" value="true"></property>  
  20.     </bean>  
  21.     <bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
  22.         <property name="connectionFactory" ref="jedisConnectionFactory"></property>  
  23.         <property name="defaultSerializer">  
  24.             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>  
  25.         </property>  
  26.     </bean>  
  27.       
  28.     <bean id="topicMessageListener" class="com.sample.redis.sdr.TopicMessageListener">  
  29.         <property name="redisTemplate" ref="jedisTemplate"></property>  
  30.     </bean>  
  31.     <bean id="topicContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer" destroy-method="destroy">  
  32.         <property name="connectionFactory" ref="jedisConnectionFactory"/>  
  33.         <property name="taskExecutor"><!-- 此处有个奇怪的问题,无法正确使用其他类型的Executor -->  
  34.             <bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">  
  35.                 <property name="poolSize" value="3"></property>  
  36.             </bean>  
  37.         </property>  
  38.         <property name="messageListeners">  
  39.             <map>  
  40.                 <entry key-ref="topicMessageListener">  
  41.                     <bean class="org.springframework.data.redis.listener.ChannelTopic">  
  42.                         <constructor-arg value="user:topic"/>  
  43.                     </bean>  
  44.                 </entry>  
  45.             </map>  
  46.         </property>  
  47.     </bean>  
  48. </beans>  

 

 

二.消息发布(pub):

 

Java代码   收藏代码
  1. String channel = "user:topic";  
  2. //其中channel必须为string,而且“序列化”策略也是StringSerializer  
  3. //消息内容,将会根据配置文件中指定的valueSerializer进行序列化  
  4. //本例中,默认全部采用StringSerializer  
  5. //那么在消息的subscribe端也要对“发序列化”保持一致。  
  6. redisTemplate.convertAndSend(channel, "from app 1");  

 

 

三.消息接收(subscribe):

   1) TopicMessageListener类:

Java代码   收藏代码
  1. public class TopicMessageListener implements MessageListener {  
  2.   
  3.     private RedisTemplate redisTemplate;  
  4.       
  5.     public void setRedisTemplate(RedisTemplate redisTemplate) {  
  6.         this.redisTemplate = redisTemplate;  
  7.     }  
  8.   
  9.     @Override  
  10.     public void onMessage(Message message, byte[] pattern) {  
  11.         byte[] body = message.getBody();//请使用valueSerializer  
  12.         byte[] channel = message.getChannel();  
  13.         //请参考配置文件,本例中key,value的序列化方式均为string。  
  14.         //其中key必须为stringSerializer。和redisTemplate.convertAndSend对应  
  15.         String itemValue = (String)redisTemplate.getValueSerializer().deserialize(body);  
  16.         String topic = (String)redisTemplate.getStringSerializer().deserialize(channel);  
  17.         //...  
  18.     }  
  19. }  

   2) 你会发现上述编程风格非常像JMS。需要注意的是消息体的反序列化。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值