Quartz Timing Framework Primer

Source: Internet
Author: User

The quartz framework is a Java open-source scheduled Task Scheduler that has the following core concepts in the quartz framework:

1. Job

In the task interface, only the method void execute (Jobexecutioncontext context) is declared in the interface, and the declaration of the interface is as follows:

Public interface Job {    void execute (Jobexecutioncontext context) throws jobexecutionexception;}

  

2. Jobdetail

Quartz The job is performed, the new job instance needs to be recreated, so quartz cannot accept the job instance directly, instead it receives a job implementation class to instantiate the job through the Java reflection mechanism at run time. Therefore, a class is required to describe the implementation class of the job and other related static information, such as job name, description, Association Listener and other information, Jobdetail encapsulates the information.

3. Trigger

Quartz the trigger that the framework task executes, there are four types of task triggers available in the quartz framework: Simpletrigger, Crontrigger, Dailytimeintervaltrigger, Calendarintervaltrigger

In the Qunar framework, Jobdetail objects are built through Jobbuilder, trigger objects are built by Triggerbuilder, and a simple example is shown below:

 Public classSchedulerdemo {Private Static FinalSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Private Staticschedulerfactory schedulerfactory; Static {        Try{Properties props=NewProperties (); Props.load (Schedulerdemo.class. getResourceAsStream ("Quartz.properties")); Schedulerfactory=Newstdschedulerfactory (props); } Catch(Exception e) {System.out.println ("[Stdschedulerfactory] init error"); }    }         Public Static classHelloquartzjobImplementsJob {@Override Public voidExecute (Jobexecutioncontext context)throwsjobexecutionexception {System.out.println ("Hello quartz! -Executing its Job at ["+ Sdf.format (NewDate ()) + "] by ["
+ Context.gettrigger (). GetDescription () + "]"); } } /*** The construction of the Jobdetail is built by Jobbuilder, Jobbuilder calls the Jobdetail implementation class Jobdetailimpl **/ Public voidRun (Trigger Trigger)throwsschedulerexception {Scheduler Scheduler=Schedulerfactory.getscheduler (); Jobdetail Jobdetail= Jobbuilder.newjob (helloquartzjob.class). Withidentity ("Helloquartz", "Demo"). build (); Scheduler.schedulejob (Jobdetail, Trigger); Scheduler.start (); } /*** Trigger's construction is created by Triggerbuilder **/ PublicTrigger Getsimpletrigger () {Simpletrigger Simpletrigger=(Simpletrigger) Triggerbuilder.newtrigger (). Withi Dentity ("Simpletrigger", "Demo"). Withdescription ("Simpletrigger"). Startnow () . Withschedule (Simpleschedulebuilder.simpleschedule () . Withintervalinseconds (10). Withrepeatcou NT (5) . Build (); returnSimpletrigger; } /*** Cornexpress allowed characters * seconds 0-59,-*/* minute 0-59,- */* Hours 0-23,-*/* Date 1-31,-*? /L W C * Month 1-12 or JAN-DEC,-*/* Week 1-7 or SUN-SAT,-*? /L C # * YEAR (optional) leave blank, 1970-2099,-*/* special character meaning * * denotes all values *? Represents a value that is not described, that is, does not care why the value *-Represents a specified range *, which represents the start time before attaching a possible value */symbol. The value of each increment is indicated by the symbol; * Example: * "0 0 12 * *?" Trigger at 12 o'clock Noon * "0 0/5 14,18 * *?" Trigger every 5 minutes from 2 o'clock in the afternoon to 2:55 and 6 o'clock in the afternoon to 6:55 * "0 0-5 14 * *?" Trigger every 1 minutes from 2 o'clock in the afternoon to 2:05 every day **/ PublicTrigger Getcrontrigger () {Crontrigger Trigger=(Crontrigger) Triggerbuilder.newtrigger (). Withidentity ("Crontrigger", "Demo"). Withdescription ("Crontrigger"). Startnow () . Withschedule (Cronschedulebuilder.cronschedule ("0 17 17 * *?" *") . Build (); returnTrigger; } PublicTrigger Getdailytimeintervaltrigger () {Dailytimeintervaltrigger Trigger=(Dailytimeintervaltrigger) Triggerbuilder.newtrigger () . withidentity ("Dailytimeintervaltrigger", "Demo"). Withdescription ("Dailytimeintervaltrigger"). Startnow () . Withschedule (Dailytimeintervalschedulebuilder.dailytimeintervalschedule () . Oneveryday () . Startingdailyat (NewTimeOfDay (17,32,10). Withinter Val (10, Intervalunit.second) . Withrepeatcount (1) . Build (); returnTrigger; } PublicTrigger Getcalendarintervaltrigger () {Calendarintervaltrigger Trigger=Triggerbuilder.newtrigger (). Withidentity ("Calendarintervaltrigger", "Demo"). Withdescription ("Calendarintervaltrigger"). Startnow () . Withschedule (Calendarintervalschedulebuilder.calendarintervalschedule () . Withinterval (10, Intervalunit.second)) . build (); returnTrigger; } Public Static voidMain (String []args)throwsschedulerexception {Schedulerdemo Schedulerdemo=NewSchedulerdemo ();//Schedulerdemo.run (Schedulerdemo.getsimpletrigger ());//Schedulerdemo.run (Schedulerdemo.getcrontrigger ());//Schedulerdemo.run (Schedulerdemo.getdailytimeintervaltrigger ());Schedulerdemo.run (Schedulerdemo.getcalendarintervaltrigger ()); } }

  

Quartz Timing Framework Primer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.