Hello Quartz/This article uses a simple example to help you quickly get started with quartz and learn some key objects in quartz, such
Schedexecu, job, trigger, jobexecutioncontextAnd other objects
- Import the two jar packages required by quartz <quartz-2.2.1.jarr, quartz-jobs-2.2.1.jar>
- Create our own job class hellojob for simple output
Package Org. quartz. examples. examples01; import Java. util. date; import Org. slf4j. logger; import Org. slf4j. loggerfactory; import Org. quartz. job; import Org. quartz. jobexecutioncontext; import Org. quartz. jobexecutionexception;/*** Hello World quartz * @ author weeks **/public class hellojob implements job {Private Static logger _ log = loggerfactory. getlogger (hellojob. class);/*** job, job requires a public constructor; otherwise Ctory cannot build */Public hellojob () {}/*** to implement the execute Method */Public void execute (jobexecutioncontext context) throws jobexecutionexception {_ log.info ("Hello world! -"+ New date ());}}
- Create an example of running our job and execute our own job in the next minute
Package Org. quartz. examples. examples01; import static Org. quartz. datebuilder. evenminutedate; import static Org. quartz. jobbuilder. newjob; import static Org. quartz. triggerbuilder. newtrigger; import Org. quartz. jobdetail; import Org. quartz. scheduler; import Org. quartz. schedulerfactory; import Org. quartz. trigger; import Org. quartz. impl. stdschedulerfactory; import Org. slf4j. logger; import Org. slf4j. loggerfactory; import Java. util. date;/*** this demo shows how to start and disable the quartz scheduler and how to run the * @ author weeks **/public class simpleexample {public void run () throws exception {logger log = loggerfactory. getlogger (simpleexample. class); log.info ("------- initializing --------------------"); // 1. Create a scheduler factory in the factory mode. STD is the default factory of quartz, and developers can implement their own factory; schedulerfactory Sf = new stdschedulerfactory (), a component such as job and trigger; // 2. Obtain the scheduler object scheduler sched = SF through schedulerfactory. getscheduler (); log.info ("------- initialization complete -----------"); // 3, org. quartz. datebuilder. evenminutedate <next minute> -- Build date runtime = evenminutedate (new date () through datebuilder; log.info ("------- scheduling job -----------------"); // 4, org. quartz. jobbuilder. newjob -- construct job jobdetail job = newjob (hellojob. class ). withidentity ("job1", "group1 "). build (); // 5. Use triggerbuilder to build trigger = newtrigger (). withidentity ("trigger1", "group1 "). startat (runtime ). build (); // 6. In the factory mode, assemble each component <job, trigger> sched. schedulejob (job, trigger); // [group1.job1] will run at: log.info (job. getkey () + "will run at:" + runtime); // 7. Start sched. start (); log.info ("------- started scheduler ---------------"); log.info ("------- waiting 65 seconds... ------------- "); try {// wait 65 seconds to show job thread. sleep (65l * 1000l); // executing ...} catch (exception e) {//} // shut down the scheduler log.info ("------- shutting down -----------------------"); // 8. Destroy the built-in triggers and job sched through scheduler. shutdown (true); log.info ("------- shutdown complete -----------------");} public static void main (string [] ARGs) throws exception {simpleexample example = new simpleexample (); example. run ();}}
- Let's see what the above Code has done:
- Create a quartz job class, which must be implementedOrg. Quartz. JobThis interface has only one method you want to implement. The execute method is defined as follows:
- Void execute (jobexecutioncontext context)ThrowsJobexecutionexception;
- When the quartz scheduler reaches the agreed time, it will generate a job instance. Therefore, you must provide a public function to implement the job interface. Otherwise, an exception will be thrown and the execute method will be called. the Scheduler only executes the task and does not care about the result unless a jobexecutionexception is thrown.
- JobexecutioncontextContains all the information required for running quartz. See the following code snippet.
- TheSchedgger, job, triggerThree key objects (these three objects will be introduced later)
- WhereSchedulerThe scheduler object. It has methods such as start () and Shutdown () to manage the entire scheduling job.
- JobIt is related to several objects.Job, jobdetail, jobdatamap
- View the relationship between them through the class diagram
- The class diagram clearly shows that every sub-component is assembled by jobexecutioncontext. Let's look at the source code of jobexecutioncontextimpl, which stores all context information.
private transient Scheduler scheduler ; private Trigger trigger; private JobDetail jobDetail; private JobDataMap jobDataMap; private transient Job job; private Calendar calendar; private boolean recovering = false; private int numRefires = 0; private Date fireTime; private Date scheduledFireTime; private Date prevFireTime; private Date nextFireTime; private long jobRunTime = -1; private Object result; private HashMap<Object, Object> data=new HashMap<Object, Object>();
- AboutJobdetail
- Jobdetail does not store a specific instance, but it allows you to define an instance. jobdetail points to jobdatamap again.
- Jobdetail holds detailed information about a job, such as its group and name.
- About jobdatamap
- Jobdatamap stores the objects of task instances and maintains their state information. It is the implementation of the map interface, that is, you can put and get the information you want to store and obtain.
- AboutTrigger
- Triggers according to specific conventions, such as simpletrigger and crontrigger.