首页 > 代码库 > spring中订阅redis键值过期消息通知
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;@Servicepublic 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="http://www.mamicode.com/${redis.maxTotal}"></property> <property name="maxIdle" value="http://www.mamicode.com/${redis.maxIdle}"></property> <property name="minIdle" value="http://www.mamicode.com/${redis.minIdle}"></property> <property name="maxWaitMillis" value="http://www.mamicode.com/${redis.maxWaitMillis}"></property> <property name="testOnBorrow" value="http://www.mamicode.com/${redis.testOnBorrow}"></property> </bean> <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> <property name="hostName" value="http://www.mamicode.com/${redis.hostname}" /> <property name="port" value="http://www.mamicode.com/${redis.port}" /> <property name="timeout" value="http://www.mamicode.com/${redis.timeout}" /> <property name="database" value="http://www.mamicode.com/${redis.database}" /> <property name="password" value="http://www.mamicode.com/${redis.password}" /> <property name="usePool" value="http://www.mamicode.com/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="http://www.mamicode.com/__keyevent@1__:expired" /> </bean> <bean class="org.springframework.data.redis.listener.PatternTopic"> <constructor-arg value="http://www.mamicode.com/*" /> </bean> <bean class="org.springframework.data.redis.listener.PatternTopic"> <constructor-arg value="http://www.mamicode.com/‘__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
spring中订阅redis键值过期消息通知
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。