1. 程式人生 > >Spring+Redis(keyspace notification)實現定時任務(訂單過期自動關閉)

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

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>
複製程式碼

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 thing
10             }
11     }
12 }
13 
14 public class RedisInitSubscrib implements InitializingBean{  
15     
16     JedisPool pool;
17 
18     @Override
19     public void afterPropertiesSet() throws Exception {
20         pool.getResource().psubscribe(new OrderSubscribe(), "*");
21         
22     }
23     
24 } 
複製程式碼

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

3.後記

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

相關推薦

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

1.起因 最近公司專案要做訂單超期未支付需自動關閉,首先想到的是用spring的定時器(@Schedule),結果領導舉各種例子說會影響效能,只能作罷。後來想能不能基於redis實現,學習(baidu)之後,大概就是使用redis的Keyspace Notificat

利用Redis keyspace notification 實現定時執行

EventConstants類, 主要是用來定義事件相關常量/** * 事件相關常量 * @author victor * */ public class EventConstants { public static final String KEY_EVENT_AUTO_ID = "KEY_E

Redis有序集合和定時任務解決訂單15分鐘關閉

直接上程式碼   下單減去庫存    public String updatePersonStock(PageData pd) throws Exception {Map<String, Object> resmap = new HashMap<>()

Spring+Quartz框架實現定時任務集群,分布式

log cor alt 放置 這一 表達 mod 建數據庫 strac 1、定時任務的必要性:定時任務在應用中的重要性不言而喻,大多是應用,特別是金融應用更是離不開定時任務,能用定時任務來處理異常訂單,完成跑批,定時活動(雙11)等。在初期應用的訪問量並不是那麽大,

Spring整合Quartz框架實現定時任務跑批Maven完整版

觸發器 delay cut www 方法 lin job 定時任務 任務調度 Quartz 介紹Quartz is a full-featured, open source job scheduling service that can be integrated with

Spring的@schedule註解實現定時任務

專案經常會用到定時任務,實現定時任務的方式有很多種。在Spring框架中,實現定時任務很簡單,常用的實現方式是使用註解@Schedule。 @Schedule 常用來實現簡單的定時任務。例如凌晨1點跑批,每1小時更新訂單狀態等 非SpringBoot專案 在springmv

Spring不加@EnableScheduling實現定時任務

先上一段前同事寫的程式碼,看完下面的程式碼,你可能會想,我滴天啊,這個類上是不是少了個@EnableScheduling註解,不然這個定時任務怎執行呢?然並卵,這個定時任務真的執行了,我僵住了。為

SpringBoot整合Quartz實現定時任務任務、多工)

前言:為了實現如:定時傳送郵件,訂單超時等操作需要使用到任務排程,為了節約自己封裝的時間所以使用Quatrz開源專案來幫助我們實現這些繁雜的操作,Quatrz內部有自己的執行緒池,不需要我們去手動維護執行緒,而且支援cron表示式等,下面我們講解一下使用Springboot2

spring boot 整合quartz實現定時任務排程

1.pom檔案引入 <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId&

Spring Boot整合quartz實現定時任務並支援切換任務資料來源

org.quartz實現定時任務並自定義切換任務資料來源 在工作中經常會需要使用到定時任務處理各種週期性的任務,org.quar

springspringboot利用註解實現定時任務實現非同步處理

spring3.0之後就實現了自己的定時任務,而且非常簡單 1.在啟動類上加 @EnableScheduling 註解開啟定時任務支援 2.在自己的定時任務類上加 @EnableScheduling 即可,第一步不用說了,第二步程式碼如下 @Configuration/

spring定時任務實現動態定時任務啟停,週期修改

使用方法:             複製下方程式碼,在業務需要處呼叫(定時任務狀態和執行週期被修改後),一定要在專案啟動時後立刻執行一次庫中全資料呼叫此方法,List<Cron> crons   Cron中一定要有業務類的包加類名(com.rails.trave

Spring+Quartz實現動態新增定時任務

在最近工作中,由於涉及到定時任務特別多,而這些工作又是由下屬去完成的,在生成環境中經常會出現業務邏輯錯誤,分析下來多數是定時任務執行問題,所以就希望把定時任務優化一下,主要實現2個方面1.定時任務動態配置及持久化2.視覺化的管理介面,可以非常清晰的管理自己的所有定時任務首先,

使用quartz+spring實現簡單的可配置化的定時任務可和資料庫配套使用

需要的jar包: spring-context-support-4.0.1.RELEASE.jar quartz-2.2.1.jar 首先定義一個job要實現Job介面,以後各種各樣的任務都放在這個job中執行: import org.quartz.Job; import

spring整合quartz定時任務註解實現

必備jar:quartz-1.6.5.jar、commons-collections-3.2.jar、commons-logging-1.1.jar //applicationContext.xml增加 xmlns:task="http://www.springframew

Spring與Quartz的整合實現定時任務調度

java代碼 content sta 一個bug next 小時 change nsa 發現 Spring與Quartz的整合實現定時任務調度 博客分類: Java Web SpringQuartz任務調度定時任務 最近在研究Spring中的定時任務功能,最好的

Spring+Quartz實現定時任務的配置方法

detail 包含 範例 empty beans ref tail 可選 creat 1、Scheduler的配置 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&g

spring boot 1.5.4 定時任務和異步調用

springboot springboot1.5.4 springboot之web開發 springboot定時任務 springboot異步回調 上一篇:spring boot1.5.4 統一異常處理(九) 1 Spring Boot定時任務和異步調用我們在編寫Spring B

使用Spring的@Scheduled實現定時任務

特殊字符 scheduled class hat table clas ng- ould color 轉載: Spring配置文件xmlns加入 xmlns:task="http://www.springframework.org/schema/task" xsi:sc

使用Spring實現定時任務

-1 rep instance 繼承 rom ann ise 而是 們的 一.分類 從實現的技術上來分類,目前主要有三種技術(或者說有三種產品): Java自帶的java.util.Timer類,這個類允許你調度一個java.util.TimerTask任務。使用