java订单超时取消设计_电子商务平台自动取消失效订单

packagecom.ichunshen.dolook.module.trade.order.job;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.List;importorg.apache.log4j.Logger;importorg.quartz.DisallowConcurrentExecution;importorg.quartz.Job;importorg.quartz.JobExecutionContext;importorg.quartz.JobExecutionException;importorg.quartz.PersistJobDataAfterExecution;importorg.quartz.StatefulJob;importcom.ichunshen.dolook.module.trade.order.model.Order;importcom.ichunshen.dolook.module.trade.order.service.OrderService;importcom.ichunshen.dolook.module.trade.order.support.OrderQueue;importcom.ichunshen.dolook.support.DoLookConstant.OrderCancelMethod;importcn.joy.framework.plugin.quartz.ScheduleTask;/*** quartz任务的job,用于检测数据库失效订单并将其关闭

*@authorwangpeiqing

**/@DisallowConcurrentExecutionpublic class CancelOrderJob implementsScheduleTask {

Logger logger=Logger.getLogger(CancelOrderJob.class);

@Overridepublic void execute(JobExecutionContext arg0) throwsJobExecutionException {//TODO Auto-generated method stub

System.out.println("失效订单检测任务开始执行!");

Order order=newOrder();

OrderQueue queue= newOrderQueue();//在每次启动Job时去数据库查找失效订单,并加入到队列中

List list=order.getInvalidOrder();if(!list.isEmpty()){for(Order o : list) {

queue.offer(o);

}

}//获取队列的头元素,开始检测头订单是否失效

Order element=queue.peek();while (element!=null) {

Long time=this.checkOrder(element);if (time != null && time >=2400*1000) {

System.out.println("开始关闭订单"+element.getOcode()+"下单时间"+element.getOrderTime());

element.cancelInvalidOrderStatus(element.getOcode(), OrderCancelMethod.INVALID_TIME);

queue.poll();

element=queue.peek();

}else if(time<2400*1000){try{

System.out.println("等待检测订单"+element.getOcode()+"下单时间"+element.getOrderTime()+"已下单"+time/1000+"秒");

Thread.sleep(time);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

logger.info("CancelOrderJob.checkOrder定时任务出现问题");

}

}

}

}/*** 获取订单的下单时间和现在的时间差

*@authorwangpeiqing

* 2016年4月16日

*@paramorder

*@return**/

publicLong checkOrder(Order order) {

Date date= newDate();

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

OrderQueue queue= newOrderQueue();

Long diff= null;if (order != null) {

Date orderTime=order.getOrderTime();try{

diff= sdf.parse(sdf.format(date)).getTime() -sdf.parse(sdf.format(orderTime)).getTime();

}catch(ParseException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}//返回值为毫秒

returndiff;

}

}

你可能感兴趣的:(java订单超时取消设计)