Basic steps for using the scheduler
Preparatory work:
1. Get a scheduler factory: Schedulerfactory
2. Get a scheduler: Get through Schedulerfactory.getschduler ()
3. Add some information to the scheduler that you want to write: Scheduler.getcontext (). Put ("Ypdao", scan_trans_task_quartz_period);
To create a task:
1. Create an Jobdetail instance, bind the job implementation class (for example, Quartjob.class implements the Job's Class):
Jobdetail job = Jobbuilder.newjob (Quartzjob.class). Withidentity ("job132121", "JGROU12412P1"). Build ();//define Schedule trigger rules
To create a trigger:
1. Create Trigger:trigger Trigger = Triggerbuilder.newtrigger (). Withidentity ("simpleTrigger2132123", "Triggergr423oup "). Withschedule (Simpleschedulebuilder.repeatsecondlyforever (5)). Startnow (). build ();
Registering jobs and triggers in a task schedule
Scheduler.schedulejob (Job, trigger);
Start scheduling
Scheduler.start ();
Attention:
1. A scheduler can have more than one scheduled task (a scheduled task = Trigger plus Task)
2. A trigger can trigger only one task (the relationship between the trigger and the task is one by one).
Code:
Classes that implement the scheduling function ():
Package Com.tudy.springscheduler.task;import Javax.annotation.postconstruct;import Org.apache.log4j.Logger; Import Org.quartz.jobbuilder;import Org.quartz.jobdetail;import Org.quartz.scheduler;import Org.quartz.schedulerexception;import Org.quartz.schedulerfactory;import Org.quartz.simpleschedulebuilder;import Org.quartz.trigger;import Org.quartz.triggerbuilder;import Org.quartz.impl.stdschedulerfactory;import Org.springframework.beans.factory.annotation.value;import org.springframework.stereotype.Component; @Component Public classProcessquartz {Private StaticFinal Logger Logger =Logger. GetLogger (Processmsgnotpushedwithquartz.class); //This annotation can only read a configuration file named Application.properties (only this name) under the project root directory .//10 in "Scan.trans.task.quartz.period:10" represents the default value@Value ("${scan.trans.task.quartz.period:10}") PrivateInteger Scan_trans_task_quartz_period; @PostConstruct Public voidTriggerrunner () throws Schedulerexception, interruptedexception {//get a scheduler from SchedulerfactorySchedulerfactory schedulerfactory =Newstdschedulerfactory (); Scheduler Scheduler=NULL; Try { //get a scheduler from SchedulerfactoryScheduler =Schedulerfactory.getscheduler (); Scheduler.getcontext (). Put ("Ypdao", Scan_trans_task_quartz_period); //Create a Jobdetail instance, bind the job implementation class//indicates the name of the job, the name of the group, and the binding job classJobdetail Job= Jobbuilder.newjob (quartzjob.class). Withidentity ("job132121","JGROU12412P1"). build (); //defining a schedule trigger rule//using Simpletrigger rulesTrigger Trigger =Triggerbuilder.newtrigger (). Withidentity ("simpleTrigger2132123","Triggergr423oup"). Withschedule (Simpleschedulebuilder.repeatsecondlyforever (5) . Startnow (). build (); //registering jobs and triggers in a task schedulescheduler.schedulejob (Job, trigger); //Start SchedulingScheduler.start (); } Catch(Exception e) {logger.error (E, E); } }}
Task Class (Quartz.class)
Package Com.iflytek.study.springscheduler.task;import Org.apache.log4j.logger;import Org.quartz.disallowconcurrentexecution;import Org.quartz.job;import Org.quartz.jobexecutioncontext;import Org.quartz.jobexecutionexception;import org.quartz.SchedulerException; @DisallowConcurrentExecution Public classQuartzjob implements Job {Private StaticFinal Logger Logger = Logger.getlogger (quartzjob.class); PrivateFinalStatic intNot_send =0; PrivateFinalStatic intSENT =1; Public voidExecute (Jobexecutioncontext context) throws Jobexecutionexception {Try{System. out. println ("This :"+ This); intYpdao = (Integer) Context.getscheduler (). GetContext ().Get("Ypdao"); System. out. println ("Ypdao:"+Ypdao); } Catch(schedulerexception e) {e.printstacktrace (); } } }
Spring Scheduler (Scheduler)