偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

拼夕夕訂單超時(shí)未支付自動(dòng)關(guān)閉實(shí)現(xiàn)方案!

開發(fā) 架構(gòu) 開發(fā)工具
在開發(fā)中,往往會(huì)遇到一些關(guān)于延時(shí)任務(wù)的需求。例如:生成訂單 30 分鐘未支付,則自動(dòng)取消;生成訂單 60 秒后,給用戶發(fā)短信。

 [[422166]]

圖片來自 包圖網(wǎng)

對(duì)上述的任務(wù),我們給一個(gè)專業(yè)的名字來形容,那就是延時(shí)任務(wù)。那么這里就會(huì)產(chǎn)生一個(gè)問題,這個(gè)延時(shí)任務(wù)和定時(shí)任務(wù)的區(qū)別究竟在哪里呢?

一共有如下幾點(diǎn)區(qū)別:

  • 定時(shí)任務(wù)有明確的觸發(fā)時(shí)間,延時(shí)任務(wù)沒有
  • 定時(shí)任務(wù)有執(zhí)行周期,而延時(shí)任務(wù)在某事件觸發(fā)后一段時(shí)間內(nèi)執(zhí)行,沒有執(zhí)行周期
  • 定時(shí)任務(wù)一般執(zhí)行的是批處理操作是多個(gè)任務(wù),而延時(shí)任務(wù)一般是單個(gè)任務(wù)

下面,我們以判斷訂單是否超時(shí)為例,進(jìn)行方案分析。

方案分析

①數(shù)據(jù)庫輪詢

思路:該方案通常是在小型項(xiàng)目中使用,即通過一個(gè)線程定時(shí)的去掃描數(shù)據(jù)庫,通過訂單時(shí)間來判斷是否有超時(shí)的訂單,然后進(jìn)行 update 或 delete 等操作。

實(shí)現(xiàn):博主當(dāng)年早期是用 quartz 來實(shí)現(xiàn)的(實(shí)習(xí)那會(huì)的事),簡單介紹一下。

maven 項(xiàng)目引入一個(gè)依賴,如下所示:

  1. <dependency> 
  2.  
  3.     <groupId>org.quartz-scheduler</groupId> 
  4.  
  5.     <artifactId>quartz</artifactId> 
  6.  
  7.     <version>2.2.2</version> 
  8.  
  9. </dependency> 

調(diào)用 Demo 類 MyJob,如下所示:

  1. package com.rjzheng.delay1; 
  2.  
  3. import org.quartz.JobBuilder; 
  4.  
  5. import org.quartz.JobDetail; 
  6.  
  7. import org.quartz.Scheduler; 
  8.  
  9. import org.quartz.SchedulerException; 
  10.  
  11. import org.quartz.SchedulerFactory; 
  12.  
  13. import org.quartz.SimpleScheduleBuilder; 
  14.  
  15. import org.quartz.Trigger
  16.  
  17. import org.quartz.TriggerBuilder; 
  18.  
  19. import org.quartz.impl.StdSchedulerFactory; 
  20.  
  21. import org.quartz.Job; 
  22.  
  23. import org.quartz.JobExecutionContext; 
  24.  
  25. import org.quartz.JobExecutionException; 
  26.  
  27.  
  28.  
  29. public class MyJob implements Job { 
  30.  
  31.     public void execute(JobExecutionContext context) 
  32.  
  33.             throws JobExecutionException { 
  34.  
  35.         System.out.println("要去數(shù)據(jù)庫掃描啦。。。"); 
  36.  
  37.     } 
  38.  
  39.  
  40.  
  41.     public static void main(String[] args) throws Exception { 
  42.  
  43.         // 創(chuàng)建任務(wù) 
  44.  
  45.         JobDetail jobDetail = JobBuilder.newJob(MyJob.class) 
  46.  
  47.                 .withIdentity("job1""group1").build(); 
  48.  
  49.         // 創(chuàng)建觸發(fā)器 每3秒鐘執(zhí)行一次 
  50.  
  51.         Trigger trigger = TriggerBuilder 
  52.  
  53.                 .newTrigger() 
  54.  
  55.                 .withIdentity("trigger1""group3"
  56.  
  57.                 .withSchedule( 
  58.  
  59.                         SimpleScheduleBuilder.simpleSchedule() 
  60.  
  61.                                 .withIntervalInSeconds(3).repeatForever()) 
  62.  
  63.                 .build(); 
  64.  
  65.         Scheduler scheduler = new StdSchedulerFactory().getScheduler(); 
  66.  
  67.         // 將任務(wù)及其觸發(fā)器放入調(diào)度器 
  68.  
  69.         scheduler.scheduleJob(jobDetail, trigger); 
  70.  
  71.         // 調(diào)度器開始調(diào)度任務(wù) 
  72.  
  73.         scheduler.start(); 
  74.  
  75.     } 
  76.  

運(yùn)行代碼,可發(fā)現(xiàn)每隔 3 秒,輸出如下:要去數(shù)據(jù)庫掃描啦!

優(yōu)缺點(diǎn):

  • 優(yōu)點(diǎn):簡單易行,支持集群操作
  • 缺點(diǎn):對(duì)服務(wù)器內(nèi)存消耗大;存在延遲,比如你每隔 3 分鐘掃描一次,那最壞的延遲時(shí)間就是 3 分鐘;假設(shè)你的訂單有幾千萬條,每隔幾分鐘這樣掃描一次,數(shù)據(jù)庫損耗極大。

②JDK 的延遲隊(duì)列

思路:該方案是利用 JDK 自帶的 DelayQueue 來實(shí)現(xiàn),這是一個(gè)無界阻塞隊(duì)列,該隊(duì)列只有在延遲期滿的時(shí)候才能從中獲取元素,放入 DelayQueue 中的對(duì)象,是必須實(shí)現(xiàn) Delayed 接口的。

DelayedQueue 實(shí)現(xiàn)工作流程如下圖所示:

其中 Poll():獲取并移除隊(duì)列的超時(shí)元素,沒有則返回空。take():獲取并移除隊(duì)列的超時(shí)元素,如果沒有則 wait 當(dāng)前線程,直到有元素滿足超時(shí)條件,返回結(jié)果。

實(shí)現(xiàn):定義一個(gè)類 OrderDelay 實(shí)現(xiàn) Delayed。

代碼如下:

  1. package com.rjzheng.delay2; 
  2.  
  3.  
  4.  
  5. import java.util.concurrent.Delayed; 
  6.  
  7. import java.util.concurrent.TimeUnit; 
  8.  
  9.  
  10.  
  11. public class OrderDelay implements Delayed { 
  12.  
  13.  
  14.  
  15.     private String orderId; 
  16.  
  17.     private long timeout; 
  18.  
  19.  
  20.  
  21.     OrderDelay(String orderId, long timeout) { 
  22.  
  23.         this.orderId = orderId; 
  24.  
  25.         this.timeout = timeout + System.nanoTime(); 
  26.  
  27.     } 
  28.  
  29.  
  30.  
  31.     public int compareTo(Delayed other) { 
  32.  
  33.         if (other == this) 
  34.  
  35.             return 0; 
  36.  
  37.         OrderDelay t = (OrderDelay) other; 
  38.  
  39.         long d = (getDelay(TimeUnit.NANOSECONDS) - t 
  40.  
  41.                 .getDelay(TimeUnit.NANOSECONDS)); 
  42.  
  43.         return (d == 0) ? 0 : ((d < 0) ? -1 : 1); 
  44.  
  45.     } 
  46.  
  47.  
  48.  
  49.     // 返回距離你自定義的超時(shí)時(shí)間還有多少 
  50.  
  51.     public long getDelay(TimeUnit unit) { 
  52.  
  53.         return unit.convert(timeout - System.nanoTime(),TimeUnit.NANOSECONDS); 
  54.  
  55.     } 
  56.  
  57.  
  58.  
  59.     void print() { 
  60.  
  61.         System.out.println(orderId+"編號(hào)的訂單要?jiǎng)h除啦。。。。"); 
  62.  
  63.     } 
  64.  

運(yùn)行的測試 Demo 為,我們?cè)O(shè)定延遲時(shí)間為 3 秒:

  1. package com.rjzheng.delay2; 
  2.  
  3.  
  4.  
  5. import java.util.ArrayList; 
  6.  
  7. import java.util.List; 
  8.  
  9. import java.util.concurrent.DelayQueue; 
  10.  
  11. import java.util.concurrent.TimeUnit; 
  12.  
  13.  
  14.  
  15. public class DelayQueueDemo { 
  16.  
  17.      public static void main(String[] args) {   
  18.  
  19.             // TODO Auto-generated method stub   
  20.  
  21.             List<String> list = new ArrayList<String>();   
  22.  
  23.             list.add("00000001");   
  24.  
  25.             list.add("00000002");   
  26.  
  27.             list.add("00000003");   
  28.  
  29.             list.add("00000004");   
  30.  
  31.             list.add("00000005");   
  32.  
  33.             DelayQueue<OrderDelay> queue = newDelayQueue<OrderDelay>();   
  34.  
  35.             long start = System.currentTimeMillis();   
  36.  
  37.             for(int i = 0;i<5;i++){   
  38.  
  39.                 //延遲三秒取出 
  40.  
  41.                 queue.put(new OrderDelay(list.get(i),   
  42.  
  43.                         TimeUnit.NANOSECONDS.convert(3,TimeUnit.SECONDS)));   
  44.  
  45.                     try {   
  46.  
  47.                          queue.take().print();   
  48.  
  49.                          System.out.println("After " +   
  50.  
  51.                                  (System.currentTimeMillis()-start) + " MilliSeconds");   
  52.  
  53.                 } catch (InterruptedException e) {   
  54.  
  55.                     // TODO Auto-generated catch block   
  56.  
  57.                     e.printStackTrace();   
  58.  
  59.                 }   
  60.  
  61.             }   
  62.  
  63.         }   
  64.  
  65.  
  66.  

輸出如下:

  1. 00000001編號(hào)的訂單要?jiǎng)h除啦。。。。 
  2.  
  3. After 3003 MilliSeconds 
  4.  
  5. 00000002編號(hào)的訂單要?jiǎng)h除啦。。。。 
  6.  
  7. After 6006 MilliSeconds 
  8.  
  9. 00000003編號(hào)的訂單要?jiǎng)h除啦。。。。 
  10.  
  11. After 9006 MilliSeconds 
  12.  
  13. 00000004編號(hào)的訂單要?jiǎng)h除啦。。。。 
  14.  
  15. After 12008 MilliSeconds 
  16.  
  17. 00000005編號(hào)的訂單要?jiǎng)h除啦。。。。 
  18.  
  19. After 15009 MilliSeconds 

可以看到都是延遲 3 秒,訂單被刪除。

優(yōu)缺點(diǎn):

  • 優(yōu)點(diǎn):效率高,任務(wù)觸發(fā)時(shí)間延遲低。
  • 缺點(diǎn):服務(wù)器重啟后,數(shù)據(jù)全部消失,怕宕機(jī);集群擴(kuò)展相當(dāng)麻煩;因?yàn)閮?nèi)存條件限制的原因,比如下單未付款的訂單數(shù)太多,那么很容易就出現(xiàn) OOM 異常;代碼復(fù)雜度較高。

③時(shí)間輪算法

思路:先上一張時(shí)間輪的圖。

時(shí)間輪算法可以類比于時(shí)鐘,如上圖箭頭(指針)按某一個(gè)方向按固定頻率輪動(dòng),每一次跳動(dòng)稱為一個(gè) tick。

這樣可以看出定時(shí)輪由個(gè) 3 個(gè)重要的屬性參數(shù),ticksPerWheel(一輪的 tick 數(shù)),tickDuration(一個(gè) tick 的持續(xù)時(shí)間)以及 timeUnit(時(shí)間單位)。

例如當(dāng) ticksPerWheel=60,tickDuration=1,timeUnit=秒,這就和現(xiàn)實(shí)中的始終的秒針走動(dòng)完全類似了。

如果當(dāng)前指針指在 1 上面,我有一個(gè)任務(wù)需要 4 秒以后執(zhí)行,那么這個(gè)執(zhí)行的線程回調(diào)或者消息將會(huì)被放在 5 上。

那如果需要在 20 秒之后執(zhí)行怎么辦,由于這個(gè)環(huán)形結(jié)構(gòu)槽數(shù)只到 8,如果要 20 秒,指針需要多轉(zhuǎn) 2 圈。位置是在 2 圈之后的 5 上面(20 % 8 + 1)。

實(shí)現(xiàn):我們用 Netty 的 HashedWheelTimer 來實(shí)現(xiàn)。

給 Pom 加上下面的依賴:

  1. <dependency> 
  2.  
  3.     <groupId>io.netty</groupId> 
  4.  
  5.     <artifactId>netty-all</artifactId> 
  6.  
  7.     <version>4.1.24.Final</version> 
  8.  
  9. </dependency> 

測試代碼 HashedWheelTimerTest,如下所示:

  1. package com.rjzheng.delay3; 
  2.  
  3.  
  4.  
  5. import io.netty.util.HashedWheelTimer; 
  6.  
  7. import io.netty.util.Timeout; 
  8.  
  9. import io.netty.util.Timer; 
  10.  
  11. import io.netty.util.TimerTask; 
  12.  
  13.  
  14.  
  15. import java.util.concurrent.TimeUnit; 
  16.  
  17.  
  18.  
  19. public class HashedWheelTimerTest { 
  20.  
  21.     static class MyTimerTask implements TimerTask{ 
  22.  
  23.         boolean flag; 
  24.  
  25.         public MyTimerTask(boolean flag){ 
  26.  
  27.             this.flag = flag; 
  28.  
  29.         } 
  30.  
  31.         public void run(Timeout timeout) throws Exception { 
  32.  
  33.             // TODO Auto-generated method stub 
  34.  
  35.              System.out.println("要去數(shù)據(jù)庫刪除訂單了。。。。"); 
  36.  
  37.              this.flag =false
  38.  
  39.         } 
  40.  
  41.     } 
  42.  
  43.     public static void main(String[] argv) { 
  44.  
  45.         MyTimerTask timerTask = new MyTimerTask(true); 
  46.  
  47.         Timer timer = new HashedWheelTimer(); 
  48.  
  49.         timer.newTimeout(timerTask, 5, TimeUnit.SECONDS); 
  50.  
  51.         int i = 1; 
  52.  
  53.         while(timerTask.flag){ 
  54.  
  55.             try { 
  56.  
  57.                 Thread.sleep(1000); 
  58.  
  59.             } catch (InterruptedException e) { 
  60.  
  61.                 // TODO Auto-generated catch block 
  62.  
  63.                 e.printStackTrace(); 
  64.  
  65.             } 
  66.  
  67.             System.out.println(i+"秒過去了"); 
  68.  
  69.             i++; 
  70.  
  71.         } 
  72.  
  73.     } 
  74.  

輸出如下:

  1. 1秒過去了 
  2.  
  3. 2秒過去了 
  4.  
  5. 3秒過去了 
  6.  
  7. 4秒過去了 
  8.  
  9. 5秒過去了 
  10.  
  11. 要去數(shù)據(jù)庫刪除訂單了。。。。 
  12.  
  13. 6秒過去了 

優(yōu)缺點(diǎn):

  • 優(yōu)點(diǎn):效率高,任務(wù)觸發(fā)時(shí)間延遲時(shí)間比 delayQueue 低,代碼復(fù)雜度比 delayQueue 低。
  • 缺點(diǎn):服務(wù)器重啟后,數(shù)據(jù)全部消失,怕宕機(jī);集群擴(kuò)展相當(dāng)麻煩;因?yàn)閮?nèi)存條件限制的原因,比如下單未付款的訂單數(shù)太多,那么很容易就出現(xiàn) OOM 異常。

④Redis 緩存

思路一:利用 Redis 的 zset。zset 是一個(gè)有序集合,每一個(gè)元素(member)都關(guān)聯(lián)了一個(gè) score,通過 score 排序來取集合中的值。

zset 常用命令:

  • 添加元素:ZADD key score member [[score member] [score member] …]
  • 按順序查詢?cè)兀篫RANGE key start stop [WITHSCORES]
  • 查詢?cè)?score:ZSCORE key member
  • 移除元素:ZREM key member [member …]

測試如下:

  1. 添加單個(gè)元素 
  2.  
  3.  
  4.  
  5. redis> ZADD page_rank 10 google.com 
  6.  
  7. (integer) 1 
  8.  
  9.  
  10.  
  11.  
  12.  
  13. 添加多個(gè)元素 
  14.  
  15.  
  16.  
  17. redis> ZADD page_rank 9 baidu.com 8 bing.com 
  18.  
  19. (integer) 2 
  20.  
  21.  
  22.  
  23. redis> ZRANGE page_rank 0 -1 WITHSCORES 
  24.  
  25. 1) "bing.com" 
  26.  
  27. 2) "8" 
  28.  
  29. 3) "baidu.com" 
  30.  
  31. 4) "9" 
  32.  
  33. 5) "google.com" 
  34.  
  35. 6) "10" 
  36.  
  37.  
  38.  
  39. 查詢?cè)氐膕core值 
  40.  
  41. redis> ZSCORE page_rank bing.com 
  42.  
  43. "8" 
  44.  
  45.  
  46.  
  47. 移除單個(gè)元素 
  48.  
  49.  
  50.  
  51. redis> ZREM page_rank google.com 
  52.  
  53. (integer) 1 
  54.  
  55.  
  56.  
  57. redis> ZRANGE page_rank 0 -1 WITHSCORES 
  58.  
  59. 1) "bing.com" 
  60.  
  61. 2) "8" 
  62.  
  63. 3) "baidu.com" 
  64.  
  65. 4) "9" 

那么如何實(shí)現(xiàn)呢?我們將訂單超時(shí)時(shí)間戳與訂單號(hào)分別設(shè)置為 score 和 member,系統(tǒng)掃描第一個(gè)元素判斷是否超時(shí)。

具體如下圖所示:

實(shí)現(xiàn)一:

  1. package com.rjzheng.delay4; 
  2.  
  3.  
  4.  
  5. import java.util.Calendar; 
  6.  
  7. import java.util.Set
  8.  
  9.  
  10.  
  11. import redis.clients.jedis.Jedis; 
  12.  
  13. import redis.clients.jedis.JedisPool; 
  14.  
  15. import redis.clients.jedis.Tuple; 
  16.  
  17.  
  18.  
  19. public class AppTest { 
  20.  
  21.     private static final String ADDR = "127.0.0.1"
  22.  
  23.     private static final int PORT = 6379; 
  24.  
  25.     private static JedisPool jedisPool = new JedisPool(ADDR, PORT); 
  26.  
  27.  
  28.  
  29.  
  30.     public static Jedis getJedis() { 
  31.  
  32.        return jedisPool.getResource(); 
  33.  
  34.     } 
  35.  
  36.  
  37.  
  38.  
  39.     //生產(chǎn)者,生成5個(gè)訂單放進(jìn)去 
  40.  
  41.     public void productionDelayMessage(){ 
  42.  
  43.         for(int i=0;i<5;i++){ 
  44.  
  45.             //延遲3秒 
  46.  
  47.             Calendar cal1 = Calendar.getInstance(); 
  48.  
  49.             cal1.add(Calendar.SECOND, 3); 
  50.  
  51.             int second3later = (int) (cal1.getTimeInMillis() / 1000); 
  52.  
  53.             AppTest.getJedis().zadd("OrderId",second3later,"OID0000001"+i); 
  54.  
  55.             System.out.println(System.currentTimeMillis()+"ms:redis生成了一個(gè)訂單任務(wù):訂單ID為"+"OID0000001"+i); 
  56.  
  57.         } 
  58.  
  59.     } 
  60.  
  61.  
  62.  
  63.  
  64.     //消費(fèi)者,取訂單 
  65.  
  66.     public void consumerDelayMessage(){ 
  67.  
  68.         Jedis jedis = AppTest.getJedis(); 
  69.  
  70.         while(true){ 
  71.  
  72.             Set<Tuple> items = jedis.zrangeWithScores("OrderId", 0, 1); 
  73.  
  74.             if(items == null || items.isEmpty()){ 
  75.  
  76.                 System.out.println("當(dāng)前沒有等待的任務(wù)"); 
  77.  
  78.                 try { 
  79.  
  80.                     Thread.sleep(500); 
  81.  
  82.                 } catch (InterruptedException e) { 
  83.  
  84.                     // TODO Auto-generated catch block 
  85.  
  86.                     e.printStackTrace(); 
  87.  
  88.                 } 
  89.  
  90.                 continue
  91.  
  92.             } 
  93.  
  94.             int  score = (int) ((Tuple)items.toArray()[0]).getScore(); 
  95.  
  96.             Calendar cal = Calendar.getInstance(); 
  97.  
  98.             int nowSecond = (int) (cal.getTimeInMillis() / 1000); 
  99.  
  100.             if(nowSecond >= score){ 
  101.  
  102.                 String orderId = ((Tuple)items.toArray()[0]).getElement(); 
  103.  
  104.                 jedis.zrem("OrderId", orderId); 
  105.  
  106.                 System.out.println(System.currentTimeMillis() +"ms:redis消費(fèi)了一個(gè)任務(wù):消費(fèi)的訂單OrderId為"+orderId); 
  107.  
  108.             } 
  109.  
  110.         } 
  111.  
  112.     } 
  113.  
  114.  
  115.  
  116.  
  117.     public static void main(String[] args) { 
  118.  
  119.         AppTest appTest =new AppTest(); 
  120.  
  121.         appTest.productionDelayMessage(); 
  122.  
  123.         appTest.consumerDelayMessage(); 
  124.  
  125.     } 
  126.  
  127.  
  128.  
  129.  

此時(shí)對(duì)應(yīng)輸出如下:

可以看到,幾乎都是 3 秒之后,消費(fèi)訂單。

然而,這一版存在一個(gè)致命的硬傷,在高并發(fā)條件下,多消費(fèi)者會(huì)取到同一個(gè)訂單號(hào),我們上測試代碼 ThreadTest:

  1. package com.rjzheng.delay4; 
  2.  
  3.  
  4.  
  5. import java.util.concurrent.CountDownLatch; 
  6.  
  7.  
  8.  
  9. public class ThreadTest { 
  10.  
  11.     private static final int threadNum = 10; 
  12.  
  13.     private static CountDownLatch cdl = newCountDownLatch(threadNum); 
  14.  
  15.     static class DelayMessage implements Runnable{ 
  16.  
  17.         public void run() { 
  18.  
  19.             try { 
  20.  
  21.                 cdl.await(); 
  22.  
  23.             } catch (InterruptedException e) { 
  24.  
  25.                 // TODO Auto-generated catch block 
  26.  
  27.                 e.printStackTrace(); 
  28.  
  29.             } 
  30.  
  31.             AppTest appTest =new AppTest(); 
  32.  
  33.             appTest.consumerDelayMessage(); 
  34.  
  35.         } 
  36.  
  37.     } 
  38.  
  39.     public static void main(String[] args) { 
  40.  
  41.         AppTest appTest =new AppTest(); 
  42.  
  43.         appTest.productionDelayMessage(); 
  44.  
  45.         for(int i=0;i<threadNum;i++){ 
  46.  
  47.             new Thread(new DelayMessage()).start(); 
  48.  
  49.             cdl.countDown(); 
  50.  
  51.         } 
  52.  
  53.     } 
  54.  

輸出如下所示:

顯然,出現(xiàn)了多個(gè)線程消費(fèi)同一個(gè)資源的情況。

解決方案:

(1)用分布式鎖,但是用分布式鎖,性能下降了,該方案不細(xì)說。

(2)對(duì) ZREM 的返回值進(jìn)行判斷,只有大于 0 的時(shí)候,才消費(fèi)數(shù)據(jù),于是將 consumerDelayMessage() 方法里的:

  1. if(nowSecond >= score){ 
  2.  
  3.     String orderId = ((Tuple)items.toArray()[0]).getElement(); 
  4.  
  5.     jedis.zrem("OrderId", orderId); 
  6.  
  7.     System.out.println(System.currentTimeMillis()+"ms:redis消費(fèi)了一個(gè)任務(wù):消費(fèi)的訂單OrderId為"+orderId); 
  8.  

修改為:

  1. if(nowSecond >= score){ 
  2.  
  3.     String orderId = ((Tuple)items.toArray()[0]).getElement(); 
  4.  
  5.     Long num = jedis.zrem("OrderId", orderId); 
  6.  
  7.     if( num != null && num>0){ 
  8.  
  9.         System.out.println(System.currentTimeMillis()+"ms:redis消費(fèi)了一個(gè)任務(wù):消費(fèi)的訂單OrderId為"+orderId); 
  10.  
  11.     } 
  12.  

在這種修改后,重新運(yùn)行 ThreadTest 類,發(fā)現(xiàn)輸出正常了。

思路二:該方案使用 Redis 的 Keyspace Notifications。中文翻譯就是鍵空間機(jī)制,就是利用該機(jī)制可以在 key 失效之后,提供一個(gè)回調(diào),實(shí)際上是 Redis 會(huì)給客戶端發(fā)送一個(gè)消息。是需要 Redis 版本 2.8 以上。

實(shí)現(xiàn)二:在 redis.conf 中,加入一條配置:

  1. notify-keyspace-events Ex 

運(yùn)行代碼如下:

  1. package com.rjzheng.delay5; 
  2.  
  3.  
  4.  
  5. import redis.clients.jedis.Jedis; 
  6.  
  7. import redis.clients.jedis.JedisPool; 
  8.  
  9. import redis.clients.jedis.JedisPubSub; 
  10.  
  11.  
  12.  
  13. public class RedisTest { 
  14.  
  15.     private static final String ADDR = "127.0.0.1"
  16.  
  17.     private static final int PORT = 6379; 
  18.  
  19.     private static JedisPool jedis = new JedisPool(ADDR, PORT); 
  20.  
  21.     private static RedisSub sub = new RedisSub(); 
  22.  
  23.  
  24.  
  25.  
  26.  
  27.     public static void init() { 
  28.  
  29.         new Thread(new Runnable() { 
  30.  
  31.             public void run() { 
  32.  
  33.                 jedis.getResource().subscribe(sub, "__keyevent@0__:expired"); 
  34.  
  35.             } 
  36.  
  37.         }).start(); 
  38.  
  39.     } 
  40.  
  41.  
  42.  
  43.  
  44.  
  45.     public static void main(String[] args) throws InterruptedException { 
  46.  
  47.         init(); 
  48.  
  49.         for(int i =0;i<10;i++){ 
  50.  
  51.             String orderId = "OID000000"+i; 
  52.  
  53.             jedis.getResource().setex(orderId, 3, orderId); 
  54.  
  55.             System.out.println(System.currentTimeMillis()+"ms:"+orderId+"訂單生成"); 
  56.  
  57.         } 
  58.  
  59.     } 
  60.  
  61.  
  62.  
  63.  
  64.     static class RedisSub extends JedisPubSub { 
  65.  
  66.         <ahref='http://www.jobbole.com/members/wx610506454'>@Override</a> 
  67.  
  68.         public void onMessage(String channel, String message) { 
  69.  
  70.             System.out.println(System.currentTimeMillis()+"ms:"+message+"訂單取消"); 
  71.  
  72.         } 
  73.  
  74.     } 
  75.  

輸出如下:

可以明顯看到 3 秒過后,訂單取消了。PS:Redis 的 pub/sub 機(jī)制存在一個(gè)硬傷,官網(wǎng)內(nèi)容如下:

原文:Because Redis Pub/Sub is fire and forget currently there is no way to use this feature if your application demands reliable notification of events, that is, if your Pub/Sub client disconnects, and reconnects later, all the events delivered during the time the client was disconnected are lost.

翻譯:Redis 的發(fā)布/訂閱目前是即發(fā)即棄(fire and forget)模式的,因此無法實(shí)現(xiàn)事件的可靠通知。也就是說,如果發(fā)布/訂閱的客戶端斷鏈之后又重連,則在客戶端斷鏈期間的所有事件都丟失了。因此,方案二不是太推薦。當(dāng)然,如果你對(duì)可靠性要求不高,可以使用。

優(yōu)缺點(diǎn):

  • 優(yōu)點(diǎn):由于使用 Redis 作為消息通道,消息都存儲(chǔ)在 Redis 中。如果發(fā)送程序或者任務(wù)處理程序掛了,重啟之后,還有重新處理數(shù)據(jù)的可能性;做集群擴(kuò)展相當(dāng)方便;時(shí)間準(zhǔn)確度高。
  • 缺點(diǎn):需要額外進(jìn)行 Redis 維護(hù)。

⑤使用消息隊(duì)列

我們可以采用 RabbitMQ 的延時(shí)隊(duì)列。RabbitMQ 具有以下兩個(gè)特性,可以實(shí)現(xiàn)延遲隊(duì)列。

  • RabbitMQ 可以針對(duì) Queue 和 Message 設(shè)置 x-message-tt,來控制消息的生存時(shí)間,如果超時(shí),則消息變?yōu)?dead letter。
  • lRabbitMQ的 Queue 可以配置 x-dead-letter-exchange 和 x-dead-letter-routing-key(可選)兩個(gè)參數(shù),用來控制隊(duì)列內(nèi)出現(xiàn)了 deadletter,則按照這兩個(gè)參數(shù)重新路由。

結(jié)合以上兩個(gè)特性,就可以模擬出延遲消息的功能。

優(yōu)缺點(diǎn):

  • 優(yōu)點(diǎn):高效,可以利用 RabbitMQ 的分布式特性輕易的進(jìn)行橫向擴(kuò)展,消息支持持久化增加了可靠性。
  • 缺點(diǎn):本身的易用度要依賴于 RabbitMQ 的運(yùn)維,因?yàn)橐?RabbitMQ,所以復(fù)雜度和成本變高。

作者:hjm4702192

編輯:陶家龍

出處:http://adkx.net/w73gf

 

責(zé)任編輯:武曉燕 來源: adkx.net
相關(guān)推薦

2025-04-22 08:01:00

2021-01-08 08:30:04

996ICUPDD

2024-03-28 08:32:10

美團(tuán)關(guān)閉訂單輪訓(xùn)

2025-08-01 04:10:00

RocketMQ訂單分布式

2020-10-21 09:25:01

互聯(lián)網(wǎng)訂單自動(dòng)關(guān)閉

2023-01-30 08:12:53

訂單超時(shí)自動(dòng)取消延長訂單

2022-12-01 08:25:03

訂單超時(shí)定時(shí)任務(wù)

2024-02-26 08:50:37

訂單自動(dòng)取消消息

2016-02-25 10:09:15

MapReduceHadoopHDFS

2024-12-20 08:20:46

2025-05-09 09:31:31

支付訂單Timer

2018-08-17 16:53:00

商派ERP

2021-08-20 14:26:17

鴻蒙HarmonyOS應(yīng)用

2021-08-23 11:03:54

鴻蒙HarmonyOS應(yīng)用

2022-03-02 15:14:09

訂單計(jì)時(shí)器持久化

2018-08-19 14:30:42

女性分析網(wǎng)站

2012-08-24 10:49:51

備份恢復(fù)

2015-08-21 17:10:03

云安全

2020-08-26 06:04:25

信息泄露密鑰加密信息安全

2023-08-22 21:39:25

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)