|
|
@@ -33,6 +33,8 @@ import com.yami.shop.service.hb.IHBOrderService;
|
|
|
import com.yami.shop.service.impl.FeiEYunApi;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.logging.log4j.core.util.JsonUtils;
|
|
|
+import org.redisson.api.RLock;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -43,6 +45,7 @@ import org.springframework.stereotype.Component;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
@@ -67,39 +70,40 @@ public class OrderTask {
|
|
|
@Autowired
|
|
|
private IHBOrderService ihbOrderService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ RedissonClient redissonClient;
|
|
|
private String lockKey = "cancel_timeout_orders_lock";
|
|
|
|
|
|
public void cancelOrder() {
|
|
|
Date now = new Date();
|
|
|
+ RLock lock = redissonClient.getLock(lockKey);
|
|
|
logger.info("取消超时未支付订单。。。");
|
|
|
// 使用分布式锁或数据库行锁
|
|
|
try {
|
|
|
- if (!RedisUtil.tryLockFast(lockKey, "locked", 30)) {
|
|
|
+ boolean locked = lock.tryLock(0, 30, TimeUnit.SECONDS);
|
|
|
+ if (!locked) {
|
|
|
logger.info("任务正在执行中,跳过本次执行");
|
|
|
return;
|
|
|
}
|
|
|
// 获取15分钟之前未支付的订单
|
|
|
- try {
|
|
|
- List<Order> orders = orderService.selectCancelOrders(OrderStatus.UNPAY.value(), DateUtil.offsetMinute(now, -15));
|
|
|
- if (CollectionUtil.isEmpty(orders)) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
-// List<Order> cancelOrderList = this.chackOrders(orders);
|
|
|
|
|
|
+ List<Order> orders = orderService.selectCancelOrders(OrderStatus.UNPAY.value(), DateUtil.offsetMinute(now, -15));
|
|
|
+ if (CollectionUtil.isEmpty(orders)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //TODO zx定时任务加回积分待完善
|
|
|
+ orderService.cancelOrders(orders, "未按时支付");
|
|
|
|
|
|
- //TODO zx定时任务加回积分待完善
|
|
|
- orderService.cancelOrders(orders, "未按时支付");
|
|
|
+ // 移除缓存
|
|
|
+ this.removeCache(orders);
|
|
|
|
|
|
- // 移除缓存
|
|
|
- this.removeCache(orders);
|
|
|
- } finally {
|
|
|
- // 释放锁
|
|
|
- RedisUtil.del(lockKey);
|
|
|
- }
|
|
|
} catch (Exception e) {
|
|
|
// 释放锁
|
|
|
- RedisUtil.del(lockKey);
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ }finally {
|
|
|
+ if (lock.isHeldByCurrentThread()) {
|
|
|
+ lock.unlock();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|