spring集成redis实现发布/订阅(订阅任意频道)

前言

本案例是使用的原生的spring,未使用spring boot,但是原理是相同的,xml配置文件可以使用@Configration 配置类代替
在阅读本文章请注意:

  • 默认读者有spring基础,适合已整合spring管理redis操作数据(jedis或lettuce),但不清楚spring操作发布订模式的

spring-data-redis官方文档:

https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#pubsub.

依赖
1.spring ioc相关依赖(略)
2.
spring依赖spring-data-redis

    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.3.0.RELEASE</version>
    </dependency>

spring boot依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

一、发布

To publish a message, you can use, as with the other operations, either the low-level RedisConnection or the high-level RedisTemplate. Both entities offer the publish method, which accepts the message and the destination channel as arguments. While RedisConnection requires raw data (array of bytes), the RedisTemplate lets arbitrary objects be passed in as messages, as shown in the following example:

大致意思是RedisTemplate和RedisConnection都提供了发布消息的方法,你可以这样发布消息:

redisTemplate.convertAndSend(String chanel,String message)

二、订阅

Subscription commands in Spring Data Redis are blocking. That is, calling subscribe on a connection causes the current thread to block as it starts waiting for messages. The thread is released only if the subscription is canceled, which happens when another thread invokes unsubscribe or pUnsubscribe on the same connection. See “Message Listener Containers” (later in this document) for a solution to this problem.

redis是单线程的,直接使用RedisConnection提供的subcriber方法会导致当前线程阻塞,一直等待接收消息,不能进行其他操作
所以spring-data-redis提供了两个类RedisMessageListenerContainerMessageListenerAdapter /MessageListener

MessageListenerAdapter实现了MessageListener接口,本文章提供的是实现MessageListener接口,使用MessageListenerAdapter可参考官方文档。

1.RedisMessageListenerContainer

RedisMessageListenerContainer 是监听器容器,它用于从Redis通道接收消息,并注入其中的MessageListener实例,并将消息根据频道分派给MessageListener处理

主要方法有:

  • setConnectionFactory(RedisConnectionFactory connectionFactory)
    配置连接工厂
  • setMessageListeners(Map<? extends MessageListener, Collection<? extends Topic>> listeners)
    配置初始监听器和频道
  • addMessageListener(MessageListener listener, Collection<? extends Topic> topics)
    添加监听器和频道(用于订阅其他频道)
2.MessageListenerAdapter/MessageListener

MessageListener是频道监听器,当使用RedisMessageListenerContainer 将监听器和频道绑定以后,一旦频道发布信息后,MessageListener会接收到消息并执行onmessage方法

    public class RedisMessageListener implements MessageListener {
   
   		@Override
	    public void onMessage(Message message, byte[] bytes) {
   
			//需要执行的方法
	    }
    }
三、实例

监听器
RedisMessageListener .java

public class RedisMessageListener implement
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值