未支付订单30分钟后,自动取消
生成订单时发起延时30分钟的任务
/**
* 取消订单的任务
* @Title: startCancelOrderTask
* @Description: 取消订单的任务
* @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: 订单取消的预定计划
* @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;
}
/**
* 生成订单时调用,一段时间后订单取消
*/
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{
/**
更改订单状态
*/
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上下文环境对象
*
* @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);
}
}
- 在xml中声明或者注解
<bean id="springContext" class="com.ghgcn.cigarbox.support.SpringContextHolder"></bean>
@Component
public final class SpringContextHolder implements ApplicationContextAware {
另外
如果有消息队列可以发送延时消息,来处理