最近写后端用延迟队列实现订单支付过期的功能,之前测试的时候一直没有问题,但是就在刚才下了几单等过期的时候发现订单始终没有过期(过期时间2分钟),后来远程调试发现延迟队列里有三个早已经过期的任务(截图时间18:50),附上Delayed接口实现方法,请问大佬们这是什么原因导致的呢?
expire 时间为 2021-06-23 18:17:33
private final static long DELAY = 2 * 60 * 1000L;
public DelayTask(String payOrderNo, int amount, int unionId, int addressId, String openId, int tmpStock, String orderMsg){
super();
//省略
this.expire = System.currentTimeMillis() + DELAY;
this.now = new Date();
}
public DelayTask(String payOrderNo){
super();
//省略
this.expire = System.currentTimeMillis() + DELAY;
this.now = new Date();
}
@Override
public long getDelay(@NotNull TimeUnit unit) {
return unit.convert(this.expire - System.currentTimeMillis() , TimeUnit.MILLISECONDS);
}
@Override
public int compareTo(@NotNull Delayed o) {
return (int) (this.getDelay(TimeUnit.MILLISECONDS) -o.getDelay(TimeUnit.MILLISECONDS));
}
DelayQueue
是到了时间才能获取,延迟获取队列,不是用来处理过期的。你这种用本地缓存 或者自己加个ttl字段都可以。