Spring+Redis(keyspace notification)實現定時任務(訂單到期自動關閉)

來源:互聯網
上載者:User

標籤:san   extends   例子   ble   tor   自動   isp   htm   disk   

1.起因

最近公司項目要做訂單超期未支付需自動關閉,首先想到的是用spring的定時器(@Schedule),結果領導舉各種例子說會影響效能,只能作罷。後來想能不能基於redis實現,學習(baidu)之後,大概就是使用redis的Keyspace Notifications,大概就是利用該機制可以在key失效之後,提供一個回調,實際上是redis會給用戶端發送一個訊息。是需要redis版本2.8以上,conf配置需設定notify-keyspace-events Ex,請示領導之後也得到了同意。

2.整合實現

大致思路就是讓spring做客訂閱‘[email protected]__:expired‘頻道就可以了。在這裡給出兩種實現方式。

1.利用MessageListenerAdapter,spring本身已經提供了的實現方式。

首先自訂一個MessageDelegate 介面並實現

 1 public interface MyMessageDelegate { 2   void handleMessage(String message); 3   void handleMessage(Map message); void handleMessage(byte[] message); 4   void handleMessage(Serializable message); 5   // pass the channel/pattern as well 6   void handleMessage(Serializable message, String channel); 7  } 8  9 public class MyRedisKeyExpiredMessageDelegate implements MessageDelegate {10   // implementation elided for clarity...11 }

xml增加相關配置

     <bean id="messageListener"          class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">        <constructor-arg>            <bean class="com.xxx.MyRedisKeyExpiredMessageDelegate" />        </constructor-arg>    </bean>    <bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer">        <property name="connectionFactory" ref="connectionFactory" />        <property name="messageListeners">            <map>                <entry key-ref="messageListener">                    <list>                        <bean class="org.springframework.data.redis.listener.ChannelTopic">                            <constructor-arg value="[email protected]__:expired" />                        </bean>                    </list>                </entry>            </map>        </property>    </bean>    

具體可參考官方文檔:http://docs.spring.io/spring-data/redis/docs/1.7.8.RELEASE/reference/html/#redis:pubsub:subscribe

2.即自訂一個OrderPubSub類繼承自JedisPubSub,然後在spring啟動的時候就訂閱這個OrderPubSub。

 1 public class OrderSubscribe extends JedisPubSub { 2      3     public void onPSubscribe(String pattern, int subscribedChannels) {    4          5     }   6        7     public void onPMessage(String pattern, String channel, String message) {     8             if ("[email protected]__:expired".equals(channel)) { 9                 //do some thing10             }11     }12 }13 14 public class RedisInitSubscrib implements InitializingBean{  15     16     JedisPool pool;17 18     @Override19     public void afterPropertiesSet() throws Exception {20         pool.getResource().psubscribe(new OrderSubscribe(), "*");21         22     }23     24 } 

當key失效後,收到訊息的內容(即方法中的message參數)就是key的值,這樣就可以做自訂的操作了。

3.後記

歡迎大家留言交流,關於訂單自動關閉如果有更好的方式,還望不吝賜教,O(∩_∩)O謝謝。

 

 

 

 

 

Keyspace Notifications

Spring+Redis(keyspace notification)實現定時任務(訂單到期自動關閉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.