Subscribe to Redis Key-value expiration message notification in spring

Source: Internet
Author: User

1, first enable the REDIS notification function (under Ubuntu operation):
Edit the/etc/redis/redis.conf file to add or enable the following (expiration notification):

Notify-keyspace-events Ex

or after logging in to REDIS-CLI, enter the following command:

Config set notify-keyspace-events Ex

For more information, see: Http://redis.io/topics/notifications#configuration

2. Configuring Monitoring in Java Spring

Interface class:

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);}

Implementation class:

ImportOrg.apache.logging.log4j.LogManager;ImportOrg.apache.logging.log4j.Logger;ImportOrg.springframework.stereotype.Service;Importrhxtune.smarthome.api.interfaces.IMessageDelegate;Importjava.io.Serializable;ImportJava.util.Map; @Service Public classDefaultmessagedelegateImplementsImessagedelegate { Public StaticLogger Logger = Logmanager.getlogger (defaultmessagedelegate.class. GetName ()); @Override Public voidhandlemessage (String message) {Logger.info ("HandleMessage1:" +message); } @Override Public voidhandlemessage (Map message) {Logger.info ("HandleMessage2:" +message); } @Override Public voidHandlemessage (byte[] message) {Logger.info ("HandleMessage3:" +message); } @Override Public voidhandlemessage (Serializable message) {Logger.info ("HandleMessage4:" +message); } @Override Public voidhandlemessage (Serializable message, String Channel) {Logger.info ("HandleMessage5:" + message +channel); }}

Configuration in 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.xsdHttp://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= "mi Nidle "value=" ${redis.minidle} "></property> <property name=" Maxwaitmillis "value=" ${redis.maxwaitmilli s} "></property> <property name=" Testonborrow "value=" ${redis.testonborrow} "></property> &lt ;/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" > <!--Serialization recommended Key/hashkey adopt Stringredisserializer. --<property name= "Keyserializer" > <beanclass= "Org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property Name = "Hashkeyserializer" > <beanclass= "Org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property Name = "ValueSerializer" > <beanclass= "Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> <pro Perty name= "Hashvalueserializer" > <beanclass= "Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> <pro Perty name= "ConnectionFactory" ref= "Jedisconnfactory"/> </bean> <!--Encapsulation of string operations--<bean ID = "Stringredistemplate"class= "Org.springframework.data.redis.core.StringRedisTemplate" > <property name= "connectionfactory" ref= " Jedisconnfactory "/> </bean> <!--setting up Redis message subscriptions (Mode 1)-<!--<bean id=" Listener "class= "Rhxtune.smarthome.api.services.DefaultMessageDelegate"/> <redis:listener-container connection-factory= " Jedisconnfactory "> <redis:listener ref=" Listener "method=" Handlemessage "topic=" [email protected]__:expired] /> </redis:listener-container>--> <!--setting up Redis message subscriptions (Mode 2)-<bean id= "MessageListener"class= "Org.springframework.data.redis.listener.adapter.MessageListenerAdapter" > <constructor-arg> &L T;beanclass= "Rhxtune.smarthome.api.services.DefaultMessageDelegate"/> </constructor-arg> </bean> <bea n id= "Rediscontainer"class= "Org.springframework.data.redis.listener.RedisMessageListenerContainer" > <property name= "                ConnectionFactory "ref=" jedisconnfactory "/> <property name=" Messagelisteners "> <map> <entry key-ref= "MessageListener" > <list> <beanclass= "Org.springframework.data.redis.listener.ChannelTopic" > <constructor-arg value= "[Email p Rotected]__:expired "/> </bean> <beanclass= "Org.springframework.data.redis.listener.PatternTopic" > <constructor-arg value= "*"/> </bean> <beanclass= "Org.springframework.data.redis.listener.PatternTopic" > <constructor-arg value= "' __key*_            _:* "/> </bean> </list> </entry> </map> </property> </bean></beans>

For more details see: http://docs.spring.io/spring-data/redis/docs/1.7.1.RELEASE/reference/html/#redis:p Ubsub:subscribe

Subscribe to Redis Key-value expiration message notification in spring

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.