欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 之未支付订单30分钟后自动取消

程序员文章站 2022-03-19 16:01:53
一、生成订单时发起延时30分钟的任务/** * 订单发起延迟任务 * @param order 订单 */ @Override public void startCancelOrderTask(Order order){ if(order.getState().equals(OrderStateEnum.ORDER_STATE_1.getCode())){ OrderSchedule.getInstance().star...
一、生成订单时发起延时30分钟的任务
/**
     * 订单发起延迟任务
     * @param order 订单
     */
    @Override
    public void startCancelOrderTask(Order order){
        if(order.getState().equals(OrderStateEnum.ORDER_STATE_1.getCode())){
            OrderSchedule.getInstance().startTask( new OrderFailureJob(order.getOrderSn()), Const.ORDER_CANCEL_TIME, TimeUnit.MINUTES);
        }
    }

Const.ORDER_CANCEL_TIME 为取消时间 30分钟定义常量,单位分钟

二、OrderSchedule
package com.ghgcn.cigarbox.task.job;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

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;
    }

    /**
     * 生成订单时调用,一段时间后订单取消
     */
    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 
 *
 */
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{
                /**
                更改订单状态
                */
                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;

@Component
public final class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext springContext;

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

    /**
     * 返回spring上下文环境对象
     * 
     * @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);
    }

 

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

本文地址:https://blog.csdn.net/weixin_44138647/article/details/111993104