Mission Requirements:
Close a time-out unpaid order and place the order information in a failed state
Related technologies:
Quartz Frame Timing Scheduling
Implementation ideas:
- When the service starts, query the database in the order of the orders that have not been paid, according to a single time deposit into the queue, the first place to save the head, after the next single deposit queue tail, take the queue of the head element
- Detection and present time, if more than 40 minutes, then perform data operation, that is, close the order, but only close the unpaid orders, and then move the head element from the queue, and take the next element to detect, and so on
- If the detected time is less than 40 minutes, the thread waits for the corresponding difference, and then executes the order operation
Related issues:
- To prevent the polling task from having to rear-end after the last job is completed, the workaround is to add the @disallowconcurrentexecution annotation to the job to allow the next job to wait for the current job to finish executing
- The polling interval set is 35 minutes, the order timeout is 4 minutes, there is a 5-minute time difference in the middle, in order to prevent the order from being added to the queue more than once, pay attention to the weight when joining the order queue
Related code
PackageCom.ichunshen.dolook.module.trade.order.support;Import StaticOrg.quartz.CronScheduleBuilder.cronSchedule;Import StaticOrg.quartz.JobBuilder.newJob;Import StaticOrg.quartz.TriggerBuilder.newTrigger;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importjava.util.List;ImportOrg.apache.log4j.Logger;ImportOrg.quartz.CronTrigger;ImportOrg.quartz.JobDetail;ImportOrg.quartz.Scheduler;Importorg.quartz.SchedulerException;Importorg.quartz.SchedulerFactory;Importorg.quartz.impl.StdSchedulerFactory;ImportCom.ichunshen.dolook.module.trade.order.job.CancelOrderJob;ImportCom.ichunshen.dolook.module.trade.order.model.Order;ImportCom.ichunshen.dolook.module.trade.order.web.OrderController; Public classCancelordertask {StaticLogger Logger = Logger.getlogger (Ordercontroller.class); Public voidCancelordertask ()throwsschedulerexception {//Get a schedulerSchedulerfactory sh =Newstdschedulerfactory (); Scheduler Scheduler=Sh.getscheduler (); //Create a job Task name, Task Group, task execution classJobdetail job = Newjob (cancelorderjob.class). Withidentity ("Cancelorderjob", "Orderjob"). build (); //Create a triggerCrontrigger trigger = Newtrigger (). Withidentity ("Cancelordertrigger", "Ordertrigger"). Withschedule (Cronschedule ("0 0/2 * * *?") . Build (); //bind the job and the triggerDate date=scheduler.schedulejob (Job, trigger); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss SSS"); Logger.info (Job.getkey ()+ "Cancel order scheduled task at" +sdf.format (date) + "Start Execution"); Scheduler.start (); }}
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;/*** Job for quartz task to detect and close the database failure order *@authorwangpeiqing **/@DisallowConcurrentExecution Public classCancelorderjobImplementsScheduleTask {Logger Logger=logger.getlogger (cancelorderjob.class); @Override Public voidExecute (jobexecutioncontext arg0)throwsjobexecutionexception {//TODO auto-generated Method StubSystem.out.println ("Failed order detection task started!")); Order Order=NewOrder (); Orderqueue Queue=NewOrderqueue (); //go to the database to find the failed order and join in the queue each time the job is startedList<order> list=Order.getinvalidorder (); if(!List.isEmpty ()) { for(Order o:list) {queue.offer (o); } } //gets the header element of the queue, starting to detect if the head order is invalidatedOrder element=Queue.peek (); while(element!=NULL) {Long time= This. Checkorder (Element); if(Time! =NULL&& Time >=2400*1000) {System.out.println ("Start closing Orders" +element.getocode () + "Order Time" +element.getordertime ()); Element.cancelinvalidorderstatus (Element.getocode (), ordercancelmethod.invalid_time); Queue.poll (); Element=Queue.peek (); }Else if(time<2400*1000){ Try{System.out.println ("Waiting for inspection Orders" +element.getocode () + "Order Time" +element.getordertime () + "orders placed" +time/1000+ "seconds"); Thread.Sleep (time); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); Logger.info ("Problem with Cancelorderjob.checkorder timed tasks"); } } } } /*** Get order time and current difference *@authorwangpeiqing * April 16, 2016 *@paramOrder *@return * */ PublicLong Checkorder (Order order) {Date Date=NewDate (); SimpleDateFormat SDF=NewSimpleDateFormat ("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 blockE.printstacktrace (); } } //The return value is milliseconds returndiff; }}
PackageCom.ichunshen.dolook.module.trade.order.support;Importjavax.servlet.ServletContextEvent;ImportJavax.servlet.ServletContextListener;Importorg.quartz.SchedulerException; Public classCancelorderlistenerImplementsServletcontextlistener {@Override Public voidcontextinitialized (Servletcontextevent sce) {//TODO auto-generated Method StubCancelordertask task =NewCancelordertask (); Try{task.cancelordertask (); } Catch(schedulerexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} @Override Public voidcontextdestroyed (Servletcontextevent sce) {//TODO auto-generated Method Stub }}
E-commerce platform automatically cancels expired orders