1. Causes
Recently, the company's project to do the overdue payment needs to be automatically closed, the first thought is the spring timer (@Schedule), the results of various examples of leadership will affect performance, can only give up. Later, I would like to be able to implement based on Redis, after learning (Baidu), probably is the use of Redis keyspace notifications, presumably this mechanism can be used after the key fails to provide a callback, in fact, Redis will send a message to the client. Redis version 2.8 is required, the Conf configuration needs to be set notify-keyspace-events Ex, after the request of the leadership has also been agreed.
2. Integrated implementation
The general idea is to have spring do a client subscription ' [Email protected]__:expired' channel is OK. Here are two ways to achieve this.
1. Use Messagelisteneradapter,Spring itself has provided a way to implement.
first customize a messagedelegate interface and implement
1 Public InterfaceMymessagedelegate {2 voidhandlemessage (String message);3 voidHandlemessage (Map message);voidHandlemessage (byte[] message);4 voidhandlemessage (Serializable message);5 //Pass the Channel/pattern as well6 voidhandlemessage (Serializable message, String Channel);7 }8 9 Public classMyrediskeyexpiredmessagedelegateImplementsMessagedelegate {Ten //implementation elided for clarity ... One}
XML adds related configuration
<BeanID= "MessageListener"class= "Org.springframework.data.redis.listener.adapter.MessageListenerAdapter"> <Constructor-arg> <Beanclass= "Com.xxx.MyRedisKeyExpiredMessageDelegate" /> </Constructor-arg> </Bean> <BeanID= "Rediscontainer"class= "Org.springframework.data.redis.listener.RedisMessageListenerContainer"> < Propertyname= "ConnectionFactory"ref= "ConnectionFactory" /> < Propertyname= "Messagelisteners"> <Map> <entryKey-ref= "MessageListener"> <List> <Beanclass= "Org.springframework.data.redis.listener.ChannelTopic"> <Constructor-argvalue= "[Email protected]__:expired" /> </Bean> </List> </entry> </Map> </ Property> </Bean>
For details, refer to the official documentation: http://docs.spring.io/spring-data/redis/docs/1.7.8.RELEASE/reference/html/#redis:p Ubsub:subscribe
2. That is, customizing a Orderpubsub class inherits from Jedispubsub, and then subscribes to this orderpubsub when spring is started .
1 Public classOrdersubscribeextendsJedispubsub {2 3 Public voidOnpsubscribe (String pattern,intsubscribedchannels) { 4 5 } 6 7 Public voidonpmessage (string pattern, string channel, String message) {8 if("[Email protected]__:expired"). Equals (channel)) {9 //Do some thingTen } One } A } - - Public classRedisinitsubscribImplementsinitializingbean{ the - Jedispool Pool; - - @Override + Public voidAfterpropertiesset ()throwsException { -Pool.getresource (). Psubscribe (NewOrdersubscribe (), "*"); + A } at -}
When key fails, the content of the message received (that is, the message parameter in the method) is the value of the key, so you can do a custom operation.
3. PostScript
Welcome to the message of exchange, about the automatic closing of orders if there is a better way, but also look at the generous enlighten, O (∩_∩) o Thank you.
Keyspace Notifications
Spring+redis (keyspace notification) implements timed tasks (order expiration automatically closes)