zoukankan      html  css  js  c++  java
  • quartz 防止上一任务未执行完毕,下一时间点重复执行

    /**
     * 订单监控类
     * 定时扫描所有待付款订单,超时自动取消
     * Created by huangbaidong
     * 2017/3/29.
     */
    @Component
    public class OrderMonitorJob {
    
        @Resource
        private RedisUtil redisUtil;
    
        @Resource
        private BsdOrderService bsdOrderService;
    
        @Resource
        private BsdDFKOrderCacheManager bsdDFKOrderCacheManager;
    
        private boolean isLock = false;
    
        /**
         * 待付款订单定时扫描类
         * 定时扫描所有待付款订单,超时自动取消
         */
        public void paymentTimeoutScan() {
            if(!isLock) {
                //加锁,防止定时器重复执行,
                isLock = true;
                try {
                    List<BsdDFKOrderCacheEntity> orders = bsdDFKOrderCacheManager.getAll();
                    if (BlankUtil.isNotEmpty(orders)) {
    
                        List<String> removeIds = new ArrayList<>();//需要移除的订单
    
                        for (BsdDFKOrderCacheEntity order : orders) {
    
                            if (Dictionary.ORDER_STATUS_DFK == order.getOrderStatus()) {
                                boolean isExpire = false;
                                if (order.isHasPromotion() == 1) {
                                    //如果是有促销规则的订单,1小时未支付,自动取消订单
                                    if (System.currentTimeMillis() - order.getOrderTime().getTime() > CommonConstants.hasRulePayTimeout) {
                                        isExpire = true;
                                    }
                                } else {
                                    //如果是没有促销规则的订单,24小时未支付,自动取消订单
                                    if (System.currentTimeMillis() - order.getOrderTime().getTime() > CommonConstants.hasNotRulePayTimeout) {
                                        isExpire = true;
                                    }
                                }
                                if (isExpire) {
    
                                    //1、自动取消订单(操作人ID传0)
                                    bsdOrderService.cancel(order.getId(), 0l);
    
                                    //2、记录过期的订单,将订单从缓存中清除
                                    removeIds.add(order.getId().toString());
    
                                    System.out.println("------>订单[" + order.getCode() + "]超时未付款自动取消订单");
                                }
                            } else {
                                //不是待付款状态的订单,记录从缓存中清除
                                removeIds.add(order.getId().toString());
                            }
                        }
    
                        //将不是待支付状态的订单,以及过期的未支付的订单从缓存中删除
                        if (BlankUtil.isNotEmpty(removeIds)) {
                            bsdDFKOrderCacheManager.removeFromCache(removeIds);
                        }
                    }
                } finally {
                    //运行完后释放锁
                    isLock = false;
                }
            }
        }
    
    }
    /**
    * 订单监控类
    * 定时扫描所有待付款订单,超时自动取消
    * Created by huangbaidong
    * 2017/3/29.
    */
    @Component
    public class OrderMonitorJob {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private BsdOrderService bsdOrderService;

    @Resource
    private BsdDFKOrderCacheManager bsdDFKOrderCacheManager;

    private boolean isLock = false;

    /**
    * 待付款订单定时扫描类
    * 定时扫描所有待付款订单,超时自动取消
    */
    public void paymentTimeoutScan() {
    if(!isLock) {
    //加锁,防止定时器重复执行,
    isLock = true;
    try {
    List<BsdDFKOrderCacheEntity> orders = bsdDFKOrderCacheManager.getAll();
    if (BlankUtil.isNotEmpty(orders)) {

    List<String> removeIds = new ArrayList<>();//需要移除的订单

    for (BsdDFKOrderCacheEntity order : orders) {

    if (Dictionary.ORDER_STATUS_DFK == order.getOrderStatus()) {
    boolean isExpire = false;
    if (order.isHasPromotion() == 1) {
    //如果是有促销规则的订单,1小时未支付,自动取消订单
    if (System.currentTimeMillis() - order.getOrderTime().getTime() > CommonConstants.hasRulePayTimeout) {
    isExpire = true;
    }
    } else {
    //如果是没有促销规则的订单,24小时未支付,自动取消订单
    if (System.currentTimeMillis() - order.getOrderTime().getTime() > CommonConstants.hasNotRulePayTimeout) {
    isExpire = true;
    }
    }
    if (isExpire) {

    //1、自动取消订单(操作人ID0)
    bsdOrderService.cancel(order.getId(), 0l);

    //2、记录过期的订单,将订单从缓存中清除
    removeIds.add(order.getId().toString());

    System.out.println("------>订单[" + order.getCode() + "]超时未付款自动取消订单");
    }
    } else {
    //不是待付款状态的订单,记录从缓存中清除
    removeIds.add(order.getId().toString());
    }
    }

    //将不是待支付状态的订单,以及过期的未支付的订单从缓存中删除
    if (BlankUtil.isNotEmpty(removeIds)) {
    bsdDFKOrderCacheManager.removeFromCache(removeIds);
    }
    }
    } finally {
    //运行完后释放锁
    isLock = false;
    }
    }
    }

    }
  • 相关阅读:
    29. Divide Two Integers
    leetCode 17. Letter Combinations of a Phone Number
    查找
    快速排序
    希尔排序
    插入排序
    归并排序,还有非递归方式没写
    堆排序--还有递归法没有写
    c++实现字符串全排序
    归并排序
  • 原文地址:https://www.cnblogs.com/cocoat/p/6654632.html
Copyright © 2011-2022 走看看