Recently, the quartz framework was used to schedule the function of timed push data (note: In Java you can use the Java.util.Timer and the classes that implement the Java.util.TimerTask interface to do the timed task function).
This paper mainly introduces the basic use of quartz and the quartz to provide the user with the extension point Joblistener and other listening interfaces from the large framework.
Usually for job scheduling we pay attention to these three aspects of content: job, scheduling time, by whom dispatch. For example: I will go to Beijing tomorrow. This is good, will "go to Beijing" as a job, "I" is the dispatcher of this job, and "Tomorrow" is naturally the time of dispatch (also can be regarded as the trigger time of the job to Beijing).
From the small plot above, can you see what the framework can do for us? Separating a generic, business-independent part of the organization becomes the framework for a template code that solves a problem or specific problem. Exactly, quartz is the solution framework for job scheduling or domain. To see the "Go to Beijing" keyword, you can find that to Beijing for the actual business is only a descriptive statement, and how to (the gray machine, the BBC, or green car) as if these did not reflect, and this does not reflect the benefits of the framework and the business is fully decoupled. Here will introduce a few job scheduling aspects of professional terminology, such as Job,trigger, Scheduler. We are so organized these three nouns, for example: a schedule carries a trigger, this trigger has a play called job, occasionally take out to play twice. As for the impact of the job,job can have no concern for trigger, but this seems to make the job and trigger have a coupling, because the job belongs to the specific business. For a complete job scheduling solution quartz is no longer a problem, Quartz uses jobdetail to represent job or express job, so that the job with a specific business and the entire framework is separated.
The following is a macro introduction to the use of quartz2.x through the Quartz class diagram.
1.Job and Jobdetail
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/8B/wKiom1V_iE2CY3YSAABlY2RCSeE692.jpg "title=" Quartz-job.png "alt=" Wkiom1v_ie2cy3ysaably2rcsee692.jpg "/>
The specific job service realizes the job interface, specifically using jobdetail in job scheduling, using Jobbuilder constructor to construct concrete jobdetail. Two interfaces + a constructor-mode Jobdetail constructor completes the jobdetail separation in the specific business job and actual job scheduling. In practical application, as long as the jobdetail and job two interface programming, hide the specific implementation such as: Jobdetailimpl class.
2.Triiger (Job Trigger)
For the job trigger is mostly related to time, date, schedule, interval time, schedule delay and so on, quartz has classified the job trigger and provided triggerbuilder.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6E/8C/wKiom1V_ilWzlX-kAAEpU5lHdXw055.jpg "title=" Quartz-trigger.png "width=" 680 "height=" 305 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:680px;height:305px; "alt=" Wkiom1v_ilwzlx-kaaepu5lhdxw055.jpg "/>
From the trigger interface can be seen there are different trigger types, such as daily interval trigger (DAILYTIMEINTERVALTRIGGERIMPL), calendar-based trigger, cron start, simple trigger. The same triggerbuilder provides an easy way to build trigger, where users can set up different property information to build concrete instances of trigger implementations.
3. Job Scheduler Scheduler
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/8C/wKiom1V_i1vhypo0AACajASQALk167.jpg "title=" Quartz-schedule.png "width=" 680 "height=" 217 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:680PX;HEIGHT:217PX; "alt= "Wkiom1v_i1vhypo0aacajasqalk167.jpg"/>
Quartz provides different types of scheduler implementations, generally we use Stdscheduler to meet the needs, such as the distribution or remote method calls can choose the other two suitable implementations.
Listener of extensions provided by 4.Quartz
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/88/wKioL1V_jW-iU1iOAAC3Zbho8cc541.jpg "title=" Quartz-listener.png "width=" 680 "height=" 136 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:680px;height:136px; "alt= "Wkiol1v_jw-iu1ioaac3zbho8cc541.jpg"/>
Quartz provides a variety of listener interfaces for user observation jobs, triggers, and job scheduler execution procedures to provide extensibility points.
The Scheduler object has Listenermanager properties, Listenermanager objects are used to manage the implementation of the Triggerlistener,joblistener,schedulerlistener, Listen to the relationship (for example: Specify the global job listener, specify a specific job listener, etc.), see below for specific use.
Here is a sample code for a set of quartz used:
package secondriver.springsubway.demo.job;import org.junit.afterclass;import org.junit.beforeclass;import org.junit.test;import org.quartz.*;import org.quartz.impl.stdschedulerfactory;import org.quartz.impl.matchers.groupmatcher;public class testjoblistener { public static scheduler scheduler; @BeforeClass public static void setbeforeclass () throws SchedulerException { StdSchedulerFactory Schedulerfactory = new stdschedulerfactory (); Scheduler = schedulerfactory.getscheduler (); Scheduler.start (); } @AfterClass public static void setafterclass () throws schedulerexception { if (scheduler.isstarted ()) { scheduler.shutdown (True); } } //Prepared for Test /** * building Jobdetail instances using builder mode * @return */ public static jobdetail getjobdetail () { return jobbuilder.newjob ( Myjob.class). Withdescription ("Myjobdetail") .withidentity ("MyJob", "Myjobgroup") .build (); } /** * using builder mode to build trigger instances * @return */ public static trigger gettrigger () { return triggerbuilder.newtrigger (). Withdescription ("MyTrigger"). Withidentity ("Mytrigger", "Mytriggergroup") .withschedule (Simpleschedulebuilder.repeatsecondlyforever (3)) .startnow (). Build (); } @Test public void testone () throws schedulerexception { jobdetail jobdetail = Getjobdetail (); &nBsp; trigger trigger = gettrigger (); scheduler.getlistenermanager (). Addjoblistener (New myjoblistener ()); scheduler.getlistenermanager (). Addtriggerlistener (New mytriggerlistener ()); scheduler.schedulejob (Jobdetail, trigger); } @Test public void testtwo () throws schedulerexception { jobdetail jobdetail = Getjobdetail (); trigger trigger = gettrigger (); /** * Add joblistener */ for the specified Jobgrup &nbsP;scheduler.getlistenermanager (). Addjoblistener (New myjoblistener (), groupmatcher.jobgroupequals (" Myjobgroup ")); /** * add triggerlistener */for the specified triggergroup scheduler.getlistenermanager (). AddTriggerListener (new Mytriggerlistener (), groupmatcher.triggergroupequals ("Mytriggergroup"); scheduler.schedulejob (Jobdetail, trigger); } @Test public void testthree () throws SchedulerException, interruptedexception { jobdetail jobdetail = getjobdetail (); // Non-persistent Job No association trigger added to scheduler need to use addjob third parameter storenondurablewhileawaitingscheduling for true scheduler.addjob (jobdetail, false, true); //JobDetail 1<->* Trigger trigger trigger1 = triggerbuilder.newtrigger (). Forjob (Jobdetail) .startnow (). WithSchedule ( Simpleschedulebuilder.repeatsecondlyforever ()). build (); trigger trigger2 = triggerbuilder.newtrigger (). Forjob (Jobdetail) .startnow (). Withschedule ( Simpleschedulebuilder.repeatsecondlyforever (5)). Build (); Scheduler.schedulejob (trigger1); &nbsP; scheduler.schedulejob (Trigger2); thread.sleep (10000); } /** * specific business implementation classes to implement the job interface Execute method can be */ public static class myjob implements job { @ Override public void execute (JobExecutionContext context) throws JobExecutionException { system.out.println ("myjob " + this.getclass (). GetCanonicalName ()); } } /** * joblistener Implementation * <p/> The * joblistener method performs a smoothOrder * <p/> * If Triigerlistener's vetojobexecution returns true * triggerfired -> vetojobexecution ->jobexecutionvetoed * <p/> * if Tiggerlistener's vetojobexecution returns false * triggerfired -> vetoJobExecution ->jobToBeExecuted -> [Job execute] -> jobWasExecuted * ->[triggerMisfired|triggerComplete] */ public static class myjoblistener implements JobListener { @Override public string getname () { return "Myjoblistener"; } @Override public void jobtobeexecuted (JobExecutionContext Context) { system.out.println (" jobtobeexecuted: " + context.getjobdetail (). GetDescription ()); } @Override public void jobexecutionvetoed (Jobexecutioncontext context) { system.out.println ("JobExecutionVetoed:" + context.getjobdetail (). GetDescription ()); } @Override public void jobwasexecuted (JobexecutionconText context, jobexecutionexception jobexception) { system.out.println ("jobwasexecuted:" + context.getjobdetail (). GetDescription ()); } } /** * triggerlistener Implementation */ public static class mytriggerlistener implements triggerlistener { @Override Public string getname () { return "Mytriggerlistener"; } @Override public void Triggerfired (trigger trigger,  Jobexecutioncontext context) { system.out.println ("triggerfired:" + trigger.getdescription ()); } @Override public boolean vetojobexecution (Trigger trigger, jobexecutioncontext context) { System.out.println ("Vetojobexecution:" + trigger.getdescription ()); return false; /** Return:false triggerfired: mytrigger vetojobexecution:mytrigger jobToBeExecuted:MyJobDetail MyJob secondriver.quartz.TestJobListener.MyJob jobWasExecuted:MyJobDetail triggerComplete:MyTrigger */ /** return:true triggerFired:MyTrigger vetoJobExecution:MyTrigger jobexecutionvetoed:myjobdetail */ } @Override public void Triggermisfired (Trigger trigger) { system.out.println ("triggermisfired:" + trigger.getdescription ()); } @Override public void triggercomplete (Trigger trigger, jobexecutioncontext context, trigger.completedexecutioninstruction triggerinstructioncode) { system.out.println ("TriggerComplete:" + Trigger.getdescription ()); &NBSP;&NBSP;&NBsp; } }}
The above code relies on the MAVEN configuration of the quartz framework:
<dependency> <groupId>org.quartz-scheduler</groupId> <version>2.2.1</version> < Artifactid>quartz</artifactid></dependency>
Quartz provides a very complete implementation of Java application job scheduling, this article through the three job scheduling keywords and quartz user extension listener A brief introduction of the use of quartz, more content can view Quartz user manual or source code.
This article is from the "Red Horse Red" blog, please be sure to keep this source http://aiilive.blog.51cto.com/1925756/1662253
Viewing quartz2.x job scheduling framework through Job,trigger,scheduler