未支付訂單30分鐘后,自動取消

未支付訂單30分鐘后,自動取消

生成訂單時發(fā)起延時30分鐘的任務(wù)

  /**
     * 取消訂單的任務(wù)
     * @Title: startCancelOrderTask
     * @Description: 取消訂單的任務(wù)
     * @param orderInfo
     * @throws
     */
    private void startCancelOrderTask(OrderInfo orderInfo){
        if(orderInfo.getPayStatus().equals(PayStatusEnum.NOPAY.ordinal())){
  //SysParam.getOrderDelay() 時間30
            OrderSchedule.getInstance().startTask( new OrderFailureJob(orderInfo.getOrderId()), SysParam.getOrderDelay(), TimeUnit.MINUTES);
        }
    }

OrderSchedule

package com.ghgcn.cigarbox.task.job;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
 * 
 * @ClassName: OrderSchedule
 * @Description: 訂單取消的預(yù)定計劃
 * @author 
 * @date 2018年4月10日 下午5:13:34
 *
 */
public class OrderSchedule {
    private static ScheduledExecutorService Manager;
    private static OrderSchedule orderSchedule;

    static{
        Manager = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
    }

    private OrderSchedule(){
    }

    public synchronized static OrderSchedule getInstance(){
        if(orderSchedule == null){
            orderSchedule = new OrderSchedule();
        }
        return orderSchedule;
    }

    /**
     * 生成訂單時調(diào)用,一段時間后訂單取消
     */
    public void startTask(Runnable task,long delay ,TimeUnit time ){
        Manager.schedule(task,delay,time);
    }
}

OrderFailureJob

package com.ghgcn.cigarbox.task.job;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.jflame.toolkit.util.DateHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ghgcn.cigarbox.common.enums.OrderEnums.OrderStatusEnum;
import com.ghgcn.cigarbox.common.enums.OrderEnums.PayStatusEnum;
import com.ghgcn.cigarbox.entity.OrderInfo;
import com.ghgcn.cigarbox.service.IOrderInfoService;
import com.ghgcn.cigarbox.support.SpringContextHolder;
/**
 * 
 * @ClassName: OrderFailureJob
 * @Description: 定時取消訂單
 * @author 
 * @date 2018年4月10日 下午5:13:15
 *
 */
public class OrderFailureJob implements Runnable {

    private Logger logger = LoggerFactory.getLogger(OrderFailureJob.class);
    private Integer orderId;
    private static Lock lock = new ReentrantLock();
    /**
    獲取Servcie
    */
    private IOrderInfoService orderInfoService =  SpringContextHolder.getBean(IOrderInfoService.class);

    public OrderFailureJob(Integer orderId) {
        super();
        this.orderId = orderId;
logger.debug("orderId  {} ",orderId);
logger.debug("orderInfoService  {} ",orderInfoService.toString());
    }

    @Override
    public void run() {
        
        /**
         * 查詢訂單
         */
        OrderInfo info = orderInfoService.selectOrderInfoByOrderId(orderId);
        logger.info(" 訂單信息  {} ",info.toString());
        if (info.getOrderStatus().equals(OrderStatusEnum.NORMAL.ordinal())
                && info.getPayStatus().equals(PayStatusEnum.NOPAY.ordinal())) {

            lock.lock();
            
            try{
                /**
                更改訂單狀態(tài)
                */
                info.setOrderStatus(OrderStatusEnum.CANCEL.ordinal());
                info.setUpdateDate(DateHelper.nowTimestamp());
                orderInfoService.updateOrderInfo(info);
            }finally{
                lock.unlock();
            }
        }
    }

}

SpringContextHolder

package com.ghgcn.cigarbox.support;

import java.util.Locale;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;

/**
 * spring ApplicationContext holder
 *  
 * 
 */
public final class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext springContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.springContext = applicationContext;
    }

    /**
     * 返回spring上下文環(huán)境對象
     * 
     * @return ApplicationContext
     */
    public static ApplicationContext getSpringContext() {
        return springContext;
    }

    
    /**
     * 按名稱獲取spring bean
     * 
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return springContext.getBean(name);
    }

    /**
     * 按類型獲取spring bean
     * 
     * @param requiredType
     * @return
     */
    public static <T> T getBean(Class<T> requiredType) {
        return springContext.getBean(requiredType);
    }

 

    /**
     * 發(fā)布事件到spring
     * @param event
     */
    public static void pushEvent(ApplicationEvent event) {
        springContext.publishEvent(event);
    }
    
}

  • 在xml中聲明或者注解
<bean id="springContext" class="com.ghgcn.cigarbox.support.SpringContextHolder"></bean>

@Component
public final class SpringContextHolder implements ApplicationContextAware {

另外

如果有消息隊列可以發(fā)送延時消息,來處理

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • http://liuxing.info/2017/06/30/Spring%20AMQP%E4%B8%AD%E6%...
    sherlock_6981閱讀 16,208評論 2 11
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,563評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,041評論 25 709
  • 沒有云影也沒有樹影, 小河象一張蒼老的臉, 水波象它額上深深的皺紋。 潮濕的大道已被吹干, 滿天的烏云在麥田上空舞...
    傅仁斌閱讀 207評論 0 4
  • 小聚 地點 不重要 話題一 就餐飲偏好加以討論 過程中 有的人就是踏踏實實米飯饅頭花卷包子 主食是主食 菜是菜 有...
    sandosandoku閱讀 637評論 0 0

友情鏈接更多精彩內(nèi)容