Log Description:
1. Do not elaborate the basic environment configuration of spring;
2, just record a temporary oneself from the online and the reference manual to find the tested useful information
3, record, convenient later own or need to use friends, follow up with new useful information will be updated in time
4, can see Spring4.0 reference manual: Partⅵ.integration 27.6Using the Quartz Scheduler
5, the test is used Spring3.1.3
Note: It is best to use 1.8.5 when referring to Quartz(currently the latest is 2.2.1, this version is incompatible with Spring3.1.1, the startup project will be error when measured, the specific errors to forget)
<!-- Task scheduling test implement a : Custom Task object Com.bocloud.equipment.test.ExampleJob must inherit the Quartzjobbean class, Implementing an abstract method Executeinternal creates a new task object each time a task is executed.--><bean id= "Myjobdetail" class= " Org.springframework.scheduling.quartz.JobDetailBean "> <!-- Property Jobclass cannot be specified as a Examplejob object through ref, which receives a class-type parameter when a task is scheduled, Each time is a new Jobclass object to execute the Executeinternal method --> <property Name= "Jobclass" value= "Com.bocloud.equipment.test.ComputerInfoGatherJob" /></bean> <bean id= "Crontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean" > <property name= "Jobdetail" ref= "Myjobdetail" /> <property name= "Cronexpression" value= "0/10&NBSP;*&NBSP;*&NBSP;*&NBSP;*&NBSP;?" /></bean> <bean id= "Computerinfogatherscheduler" class= "ORg.springframework.scheduling.quartz.SchedulerFactoryBean "> <property name=" Triggers "> <list> <ref bean= "Crontrigger" /> </list> </property></bean>
We only need to focus on the implementation of the Executeinternal method in the Quartzjobbean subclass, which only needs to place the task code we need to execute on a regular basis.
<!-- Task debugging implementation test two : Property TargetObject: Specifies the object property that performs the task Targetmethod: Specifies the method that performs the task, which must be an argument-free method--><bean id = "Jobdetail" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > <property name= "TargetObject" ref= "Computerservice" /> <property name= "Targetmethod" value= "list" /></bean> <bean id= "Crontrigger" class= " Org.springframework.scheduling.quartz.CronTriggerFactoryBean "> <property Name= "Jobdetail" ref= "Jobdetail" /> <property name= "cronExpression " value=" 0/10&NBSP;*&NBSP;*&NBSP;*&NBSP;*&NBSP;? " /></bean> <bean id= "Scheduler" class= " Org.springframework.scheduling.quartz.SchedulerFactoryBean "> <property name= "Triggers" > <list> <ref Bean= "Crontrigger" /> </list> </property> </bean>
The TargetObject Reference object is the spring framework that helps us build a good custom task bean object, but note that the method specified by Targetmehthod must be non-parametric (spring also supports task methods that can have parameters, with no specific notice , later to understand the update up, but generally we do not need dynamic parameters to perform regular tasks.
package com.bocloud.equipment.test; import java.text.parseexception;import java.util.date; import org.quartz.CronTrigger;import org.quartz.JobExecutionContext;import org.quartz.jobexecutionexception;import org.quartz.scheduler;import org.quartz.schedulerexception; import org.quartz.impl.stdscheduler;import org.springframework.scheduling.quartz.quartzjobbean; import org.springframework.web.context.contextloaderlistener;import org.springframework.web.context.webapplicationcontext; import com.bocloud.equipment.service.computerserviceimpl; public class computerinfogatherjob Extends quartzjobbean { private static long count = 0L; @Override protected void executeinternal (Jobexecutioncontext context) throws JobExecutionException { system.out.println ("*******************************"); count++; System.out.println (New date () + "\ t: Task Scheduler starts with" + count + "; " Webapplicationcontext webcontext = contextloaderlistener.getcurrentwebapplicationcontext (); computerserviceimpl csi = webcontext.getbean ( Computerserviceimpl.class); /* * Change the cycle time for task scheduling: * 1, do not call Stdscheduler's shutdown () method, shutdown () can no longer start () * &NBSP;2, you can use standby () to pause the dispatch task, then start () &nbSp; * 3, after setting up cron, to call Reschedulejob (triggerkey triggerkey, trigger Newtrigger) */ /* * here to get Schedulerfactorybean, do not pass Getbean (Class<t> requiredtype) * or Getbean (string name, class<t> requiredtype) * you will be prompted to change the error: * org.quartz.impl.stdscheduler cannot be transformed to the specified required type: * org.springframework.scheduling.quartz.SchedulerFactoryBean * Direct use Getbean (string name) to Org.quartz.impl.StdScheduler */ StdScheduler scheduler = (Stdscheduler) Webcontext.getbean ("Computerinfogatherscheduler"); if ( COUNT&NBSP;==&NBSP;2) { SYSTEM.OUT.PRINTLN ("Computer information collection task paused! "); try { //gets the name of this scheduler in Crontrigger ( The name of the Crontriggerfactorybean in the configuration file) Trigger object CronTrigger cronTrigger = (Crontrigger) scheduler.gettrigger ("Crontrigger", scheduler.default_group); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Set Computer information Acquisition task cycle: 5 Seconds"); &Nbsp; crontrigger.setcronexpression ("0/5 * * * * ? "); /* * public Date reschedulejob (String triggername, string groupname, trigger newtrigger) throws SchedulerException * triggername: Name of the trigger to be replaced * groupname: Group name of the trigger to be replaced * newtrigger: The trigger object to be newly saved * : Returns NULL if no triggername is found, otherwise returns the time the task was first triggered */ date date = scheduler.reschedulejob ("Crontrigger", Scheduler.default_group, crontrigger); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("=========computer Information acquisition task restarted ========="); if (Date == null) { throw new runtimeexception ("Reschedule task failed after changing task cycle!!!" "); } } catch (schedulerexception e) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Exception occurred while getting Crontrigger object"); e.printstacktrace (); } catch (parseexception e) { system.out.println (" An exception occurred while parsing a cron expression "); e.printstacktrace (); } } if (count == 4) {   SYSTEM.OUT.PRINTLN ("Pause Task Scheduler!! 10 seconds to start Again "); // Pause Schedule Task: All trigger tasks in the scheduler are paused, and if you want to pause the specified trigger, call Pausetrigger () scheduler.standby (); try { thread.sleep (10000); //to determine whether the scheduler is currently in a paused state if (Scheduler.isinstandbymode ()) { scheduler.start (); } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Task Scheduler Restarts again"); } catch (interruptedexception e) { e.printstacktrace (); } catch (schedulerexception e) { e.printstacktrace (); } } csi.list (0, Null, null); } }
Spring and quartz implement recurring tasks