Redis发布订阅模式浅谈

当使用银行卡消费的时候,银行往往会通过微信、短信或者邮件通知用户这笔交易的信息,这便是一种发布订阅模式,这里的发布使交易信息的发布,订阅则是各个渠道。这在实际工作中十分常用,Redis也支持这样的一个模式。
发布订阅模式首先需要消息源,也就是要有消息发布出来,比如上面例子中的银行通知。我用一个图来描述交易信息发布订阅机制。
在这里插入图片描述
这里建立了一个消息渠道,短信系统、邮件系统和微信系统都在监听这个渠道,一旦记账系统把交易消息发送到消息渠道,则监听这个渠道的各个系统就可以拿到这个消息,这样就能处理各自的任务了。

下面我用一个彩信平台来实现发布订阅模式。

在Spring的工作环境中,首先提供接收消息的类,它将实现MessageListener接口,并实现接口定义的方法OnMessage,Redis发布订阅监听类代码如下:

RedisMessageListener.java

package com.ssm.redisSubscribe.listener;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * Redis发布订阅监听类
 */
public class RedisMessageListener implements MessageListener {
    private RedisTemplate redisTemplate;

    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void onMessage(Message message, byte[] bytes) {
        //获取消息
        byte[] body=message.getBody();
        //使用值序列化器转换
        String msgBody=(String)getRedisTemplate().getValueSerializer().deserialize(body);
        System.out.println(msgBody);
        //获取channel
        byte[] channel=message.getChannel();
        //使用字符串序列化器转换
        String channelStr=(String)getRedisTemplate().getStringSerializer().deserialize(channel);
        System.out.println(channelStr);
        //渠道名称转换
        String bytesStr=new String(bytes);
        System.out.println(bytesStr);
    }
}

为了在Spring使用这个类,需要对其进行配置。

 <!-- 配置Redis发布的订阅监听类-->
    <bean id="redisMsgListener" class="com.ssm.redisSubscribe.listener.RedisMessageListener">
        <property name="redisTemplate" ref="redisTemplate" />
    </bean>

这样就在Spring上下文中定义了监听器。下面还要给一个监听容器,在Spring中已有类RedisMessageListenerContainer。它可以用于监听Redis的发布订阅消息。

 <bean id="topicContainner" class="org.springframework.data.redis.listener.RedisMessageListenerContainer" destroy-method="destroy">
        <!-- Redis连接工厂-->
        <property name="connectionFactory" ref="connectionFactory" />
        <!-- 连接池,这里只要线程池生存,才能够继续监听-->
        <property name="taskExecutor">
            <bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
                <property name="poolSize" value="3" />
            </bean>
        </property>
        <!-- 消息监听Map-->
        <property name="messageListeners" >
            <map>
                <!-- 配置监听器,key-ref和bean id定义一致-->
                <entry key-ref="redisMsgListener">
                    <!-- 监听类-->
                    <bean class="org.springframework.data.redis.listener.ChannelTopic">
                        <constructor-arg value="chat" />
                    </bean>
                </entry>
            </map>
        </property>
    </bean>

这里配置了线程池,这个线程池将会持续地生存以等待消息传入,而这里配置了容器用id为redisMsgListener的Bean进行渠道chat的监听。当消息通过渠道chat发送的时候,就会使用id为redisMsgListener的Bean进行处理消息。

为了方便读者,我把全部的xml内容贴出来。

redisSubscibe-cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="50" />
        <property name="maxTotal" value="100" />
        <property name="maxWaitMillis" value="20000" />
    </bean>

    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />

    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="localhost" />
        <property name="port" value="6379" />
        <property name="password" value="123456" />
        <property name="poolConfig" ref="poolConfig" />
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultSerializer" ref="stringRedisSerializer" />
        <property name="keySerializer" ref="stringRedisSerializer" />
        <property name="valueSerializer" ref="stringRedisSerializer" />
    </bean>

    <bean id="topicContainner" class="org.springframework.data.redis.listener.RedisMessageListenerContainer" destroy-method="destroy">
        <!-- Redis连接工厂-->
        <property name="connectionFactory" ref="connectionFactory" />
        <!-- 连接池,这里只要线程池生存,才能够继续监听-->
        <property name="taskExecutor">
            <bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
                <property name="poolSize" value="3" />
            </bean>
        </property>
        <!-- 消息监听Map-->
        <property name="messageListeners" >
            <map>
                <!-- 配置监听器,key-ref和bean id定义一致-->
                <entry key-ref="redisMsgListener">
                    <!-- 监听类-->
                    <bean class="org.springframework.data.redis.listener.ChannelTopic">
                        <constructor-arg value="chat" />
                    </bean>
                </entry>
            </map>
        </property>
    </bean>

    <!-- 配置Redis发布的订阅监听类-->
    <bean id="redisMsgListener" class="com.ssm.redisSubscribe.listener.RedisMessageListener">
        <property name="redisTemplate" ref="redisTemplate" />
    </bean>
</beans>

最后编写一个测试类:

package com.ssm.redisSubscribe.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;

public class testSubscribe {
    public static void main(String[] args){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("redisSubscibe-cfg.xml");
        RedisTemplate redisTemplate=applicationContext.getBean(RedisTemplate.class);
        String channel="chat";
        redisTemplate.convertAndSend(channel,"I am smart!!!");
    }
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值