拼夕夕订单超时未支付自动关闭实现方案!

开发 架构 开发工具
在开发中,往往会遇到一些关于延时任务的需求。例如:生成订单 30 分钟未支付,则自动取消;生成订单 60 秒后,给用户发短信。

 [[422166]]

图片来自 包图网

对上述的任务,我们给一个专业的名字来形容,那就是延时任务。那么这里就会产生一个问题,这个延时任务和定时任务的区别究竟在哪里呢?

一共有如下几点区别:

  • 定时任务有明确的触发时间,延时任务没有
  • 定时任务有执行周期,而延时任务在某事件触发后一段时间内执行,没有执行周期
  • 定时任务一般执行的是批处理操作是多个任务,而延时任务一般是单个任务

下面,我们以判断订单是否超时为例,进行方案分析。

方案分析

①数据库轮询

思路:该方案通常是在小型项目中使用,即通过一个线程定时的去扫描数据库,通过订单时间来判断是否有超时的订单,然后进行 update 或 delete 等操作。

实现:博主当年早期是用 quartz 来实现的(实习那会的事),简单介绍一下。

maven 项目引入一个依赖,如下所示:

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

调用 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("要去数据库扫描啦。。。"); 
  36.  
  37.     } 
  38.  
  39.  
  40.  
  41.     public static void main(String[] args) throws Exception { 
  42.  
  43.         // 创建任务 
  44.  
  45.         JobDetail jobDetail = JobBuilder.newJob(MyJob.class) 
  46.  
  47.                 .withIdentity("job1""group1").build(); 
  48.  
  49.         // 创建触发器 每3秒钟执行一次 
  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.         // 将任务及其触发器放入调度器 
  68.  
  69.         scheduler.scheduleJob(jobDetail, trigger); 
  70.  
  71.         // 调度器开始调度任务 
  72.  
  73.         scheduler.start(); 
  74.  
  75.     } 
  76.  

运行代码,可发现每隔 3 秒,输出如下:要去数据库扫描啦!

优缺点:

  • 优点:简单易行,支持集群操作
  • 缺点:对服务器内存消耗大;存在延迟,比如你每隔 3 分钟扫描一次,那最坏的延迟时间就是 3 分钟;假设你的订单有几千万条,每隔几分钟这样扫描一次,数据库损耗极大。

②JDK 的延迟队列

思路:该方案是利用 JDK 自带的 DelayQueue 来实现,这是一个无界阻塞队列,该队列只有在延迟期满的时候才能从中获取元素,放入 DelayQueue 中的对象,是必须实现 Delayed 接口的。

DelayedQueue 实现工作流程如下图所示:

其中 Poll():获取并移除队列的超时元素,没有则返回空。take():获取并移除队列的超时元素,如果没有则 wait 当前线程,直到有元素满足超时条件,返回结果。

实现:定义一个类 OrderDelay 实现 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.     // 返回距离你自定义的超时时间还有多少 
  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+"编号的订单要删除啦。。。。"); 
  62.  
  63.     } 
  64.  

运行的测试 Demo 为,我们设定延迟时间为 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编号的订单要删除啦。。。。 
  2.  
  3. After 3003 MilliSeconds 
  4.  
  5. 00000002编号的订单要删除啦。。。。 
  6.  
  7. After 6006 MilliSeconds 
  8.  
  9. 00000003编号的订单要删除啦。。。。 
  10.  
  11. After 9006 MilliSeconds 
  12.  
  13. 00000004编号的订单要删除啦。。。。 
  14.  
  15. After 12008 MilliSeconds 
  16.  
  17. 00000005编号的订单要删除啦。。。。 
  18.  
  19. After 15009 MilliSeconds 

可以看到都是延迟 3 秒,订单被删除。

优缺点:

  • 优点:效率高,任务触发时间延迟低。
  • 缺点:服务器重启后,数据全部消失,怕宕机;集群扩展相当麻烦;因为内存条件限制的原因,比如下单未付款的订单数太多,那么很容易就出现 OOM 异常;代码复杂度较高。

③时间轮算法

思路:先上一张时间轮的图。

时间轮算法可以类比于时钟,如上图箭头(指针)按某一个方向按固定频率轮动,每一次跳动称为一个 tick。

这样可以看出定时轮由个 3 个重要的属性参数,ticksPerWheel(一轮的 tick 数),tickDuration(一个 tick 的持续时间)以及 timeUnit(时间单位)。

例如当 ticksPerWheel=60,tickDuration=1,timeUnit=秒,这就和现实中的始终的秒针走动完全类似了。

如果当前指针指在 1 上面,我有一个任务需要 4 秒以后执行,那么这个执行的线程回调或者消息将会被放在 5 上。

那如果需要在 20 秒之后执行怎么办,由于这个环形结构槽数只到 8,如果要 20 秒,指针需要多转 2 圈。位置是在 2 圈之后的 5 上面(20 % 8 + 1)。

实现:我们用 Netty 的 HashedWheelTimer 来实现。

给 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("要去数据库删除订单了。。。。"); 
  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. 要去数据库删除订单了。。。。 
  12.  
  13. 6秒过去了 

优缺点:

  • 优点:效率高,任务触发时间延迟时间比 delayQueue 低,代码复杂度比 delayQueue 低。
  • 缺点:服务器重启后,数据全部消失,怕宕机;集群扩展相当麻烦;因为内存条件限制的原因,比如下单未付款的订单数太多,那么很容易就出现 OOM 异常。

④Redis 缓存

思路一:利用 Redis 的 zset。zset 是一个有序集合,每一个元素(member)都关联了一个 score,通过 score 排序来取集合中的值。

zset 常用命令:

  • 添加元素:ZADD key score member [[score member] [score member] …]
  • 按顺序查询元素:ZRANGE key start stop [WITHSCORES]
  • 查询元素 score:ZSCORE key member
  • 移除元素:ZREM key member [member …]

测试如下:

  1. 添加单个元素 
  2.  
  3.  
  4.  
  5. redis> ZADD page_rank 10 google.com 
  6.  
  7. (integer) 1 
  8.  
  9.  
  10.  
  11.  
  12.  
  13. 添加多个元素 
  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. 查询元素的score值 
  40.  
  41. redis> ZSCORE page_rank bing.com 
  42.  
  43. "8" 
  44.  
  45.  
  46.  
  47. 移除单个元素 
  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" 

那么如何实现呢?我们将订单超时时间戳与订单号分别设置为 score 和 member,系统扫描第一个元素判断是否超时。

具体如下图所示:

实现一:

  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.     //生产者,生成5个订单放进去 
  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生成了一个订单任务:订单ID为"+"OID0000001"+i); 
  56.  
  57.         } 
  58.  
  59.     } 
  60.  
  61.  
  62.  
  63.  
  64.     //消费者,取订单 
  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("当前没有等待的任务"); 
  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消费了一个任务:消费的订单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.  

此时对应输出如下:

可以看到,几乎都是 3 秒之后,消费订单。

然而,这一版存在一个致命的硬伤,在高并发条件下,多消费者会取到同一个订单号,我们上测试代码 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.  

输出如下所示:

显然,出现了多个线程消费同一个资源的情况。

解决方案:

(1)用分布式锁,但是用分布式锁,性能下降了,该方案不细说。

(2)对 ZREM 的返回值进行判断,只有大于 0 的时候,才消费数据,于是将 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消费了一个任务:消费的订单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消费了一个任务:消费的订单OrderId为"+orderId); 
  10.  
  11.     } 
  12.  

在这种修改后,重新运行 ThreadTest 类,发现输出正常了。

思路二:该方案使用 Redis 的 Keyspace Notifications。中文翻译就是键空间机制,就是利用该机制可以在 key 失效之后,提供一个回调,实际上是 Redis 会给客户端发送一个消息。是需要 Redis 版本 2.8 以上。

实现二:在 redis.conf 中,加入一条配置:

  1. notify-keyspace-events Ex 

运行代码如下:

  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 机制存在一个硬伤,官网内容如下:

原文: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 的发布/订阅目前是即发即弃(fire and forget)模式的,因此无法实现事件的可靠通知。也就是说,如果发布/订阅的客户端断链之后又重连,则在客户端断链期间的所有事件都丢失了。因此,方案二不是太推荐。当然,如果你对可靠性要求不高,可以使用。

优缺点:

  • 优点:由于使用 Redis 作为消息通道,消息都存储在 Redis 中。如果发送程序或者任务处理程序挂了,重启之后,还有重新处理数据的可能性;做集群扩展相当方便;时间准确度高。
  • 缺点:需要额外进行 Redis 维护。

⑤使用消息队列

我们可以采用 RabbitMQ 的延时队列。RabbitMQ 具有以下两个特性,可以实现延迟队列。

  • RabbitMQ 可以针对 Queue 和 Message 设置 x-message-tt,来控制消息的生存时间,如果超时,则消息变为 dead letter。
  • lRabbitMQ的 Queue 可以配置 x-dead-letter-exchange 和 x-dead-letter-routing-key(可选)两个参数,用来控制队列内出现了 deadletter,则按照这两个参数重新路由。

结合以上两个特性,就可以模拟出延迟消息的功能。

优缺点:

  • 优点:高效,可以利用 RabbitMQ 的分布式特性轻易的进行横向扩展,消息支持持久化增加了可靠性。
  • 缺点:本身的易用度要依赖于 RabbitMQ 的运维,因为要引用 RabbitMQ,所以复杂度和成本变高。

作者:hjm4702192

编辑:陶家龙

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

 

责任编辑:武晓燕 来源: adkx.net
相关推荐

2021-01-08 08:30:04

996ICUPDD

2024-03-28 08:32:10

美团关闭订单轮训

2020-10-21 09:25:01

互联网订单自动关闭

2023-01-30 08:12:53

订单超时自动取消延长订单

2022-12-01 08:25:03

订单超时定时任务

2024-02-26 08:50:37

订单自动取消消息

2016-02-25 10:09:15

MapReduceHadoopHDFS

2018-08-17 16:53:00

商派ERP

2022-03-02 15:14:09

订单计时器持久化

2021-08-23 11:03:54

鸿蒙HarmonyOS应用

2021-08-20 14:26:17

鸿蒙HarmonyOS应用

2018-08-19 14:30:42

女性分析网站

2012-08-24 10:49:51

备份恢复

2015-08-21 17:10:03

云安全

2020-08-26 06:04:25

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

2023-08-22 21:39:25

2021-04-20 08:00:31

Redisson关闭订单支付系统

2023-10-09 16:35:19

方案Spring支付

2021-08-23 10:49:02

鸿蒙HarmonyOS应用

2011-08-06 23:25:49

笔记本导购
点赞
收藏

51CTO技术栈公众号