原创

Spring中订阅redis键值过期消息通知

1、首先启用redis通知功能(ubuntu下操作):
编辑/etc/redis/redis.conf文件,添加或启用以下内容(过期通知):

notify-keyspace-events Ex

或者登陆redis-cli之后,输入以下命令:

config set notify-keyspace-events Ex

更多通知详见:http://redis.io/topics/notifications#configuration
2、Java Spring中配置监听
接口类:

import java.io.Serializable;
import java.util.Map;

public interface IMessageDelegate {

  void handleMessage(String message);

  void handleMessage(Map message);

  void handleMessage(byte[] message);

  void handleMessage(Serializable message);

  void handleMessage(Serializable message, String channel);

}

实现类:

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

import org.springframework.stereotype.Service;

import rhxtune.smarthome.api.interfaces.IMessageDelegate;

import java.io.Serializable;

import java.util.Map;


@Service
public class DefaultMessageDelegate implements IMessageDelegate {

    public static Logger logger = LogManager.getLogger(DefaultMessageDelegate.class.getName());

    @Override
    public void handleMessage(String message) {
        logger.info("handleMessage1:" +  message);
    }

    @Override
    public void handleMessage(Map message) {
        logger.info("handleMessage2:" +  message);
    }

    @Override
    public void handleMessage(byte[] message) {
        logger.info("handleMessage3:" +  message);
    }

    @Override
    public void handleMessage(Serializable message) {
        logger.info("handleMessage4:" +  message);
    }

    @Override
    public void handleMessage(Serializable message, String channel) {
        logger.info("handleMessage5:" +  message + channel);
    }

}

spring-redis.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"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:redis="http://www.springframework.org/schema/redis"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis.xsd">

    <!--<context:component-scan base-package="rhxtune.smarthome.api.repositorys" />-->

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

        <property name="maxTotal" value="${redis.maxTotal}"></property>

        <property name="maxIdle" value="${redis.maxIdle}"></property>

        <property name="minIdle" value="${redis.minIdle}"></property>

        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>

        <property name="testOnBorrow" value="${redis.testOnBorrow}"></property>

    </bean>

    <bean id="jedisConnFactory"

          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">

        <property name="hostName" value="${redis.hostname}" />

        <property name="port" value="${redis.port}" />

        <property name="timeout" value="${redis.timeout}" />

        <property name="database" value="${redis.database}" />

        <property name="password" value="${redis.password}" />

        <property name="usePool" value="true" />

        <property name="poolConfig" ref="jedisPoolConfig" />

    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">

        <!-- 序列化方式 建议key/hashKey采用StringRedisSerializer。 -->

        <property name="keySerializer">

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

        </property>

        <property name="hashKeySerializer">

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

        </property>

        <property name="valueSerializer">

            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

        </property>

        <property name="hashValueSerializer">

            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

        </property>

        <property name="connectionFactory" ref="jedisConnFactory" />

    </bean>

    <!-- 对string操作的封装 -->

    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">

        <property name="connectionFactory" ref="jedisConnFactory" />

    </bean>

    <!-- 设置redis消息订阅(方式1) -->

    <!--<bean id="listener" class="rhxtune.smarthome.api.services.DefaultMessageDelegate" />

    <redis:listener-container connection-factory="jedisConnFactory">

        <redis:listener ref="listener" method="handleMessage" topic="__keyevent@1__:expired" />

    </redis:listener-container>-->

    <!-- 设置redis消息订阅(方式2) -->

    <bean id="messageListener"
          class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
        <constructor-arg>
            <bean class="rhxtune.smarthome.api.services.DefaultMessageDelegate" />
        </constructor-arg>

    </bean>

    <bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer">

        <property name="connectionFactory" ref="jedisConnFactory" />

        <property name="messageListeners">

            <map>

                <entry key-ref="messageListener">

                    <list>

                        <bean class="org.springframework.data.redis.listener.ChannelTopic">
                            <constructor-arg value="__keyevent@1__:expired" />
                        </bean>

                        <bean class="org.springframework.data.redis.listener.PatternTopic">
                            <constructor-arg value="*" />
                        </bean>

                        <bean class="org.springframework.data.redis.listener.PatternTopic">
                            <constructor-arg value="'__key*__:*" />
                        </bean>

                    </list>

                </entry>

            </map>

        </property>

    </bean>

</beans>

更多详情参见:http://docs.spring.io/spring-data/redis/docs/1.7.1.RELEASE/reference/html/#redis:pubsub:subscribe

好羞涩,内容都被你看完了。。
该篇文章的评论功能已被站长关闭