Original link | Translation Links | Translation: Nkcoder | Proofreading: Fang Fei
This series of tutorials by quartz-2.2.x Official document translation, collation, and hope to the same interested in Quartz friends some reference and help, there is any improper or wrong, welcome to correct, interested in research source of the classmate, can refer to my comments on the Quartz-core source code (in progress).
Quartz API
The Quartz API core interfaces are:
- scheduler– the main API to interact with Scheduler;
- job– you perform tasks through scheduler, your task class needs to implement the interface;
- jobdetail– defines an instance of the job;
- trigger– triggers the execution of the job;
- jobbuilder– defines and creates interfaces for Jobdetail instances;
- triggerbuilder– defines and creates interfaces for trigger instances;
The lifetime of the scheduler, starting with Schedulerfactory when it was created, ends when scheduler calls the shutdown () method, and scheduler is created to add, delete, and enumerate jobs and trigger, and perform other scheduling-related operations, such as pausing trigger. However, scheduler will really trigger trigger (that is, execute job) only after calling the start () method, see tutorial one.
The "Builder" class provided by Quartz can be considered a domain-specific language (Dsl,domain specific Language). There are examples in tutorial one, here is the code snippet: (Proofing Note: This cascade API is very user-friendly, you can also use this way to write external interfaces later)
// define the job and tie it to our HelloJob class JobDetail job = newJob(HelloJob.class) .withIdentity("myJob", "group1") // name "myJob", group "group1" .build(); // Trigger the job to run now, and then every 40 seconds Trigger trigger = newTrigger() .withIdentity("myTrigger", "group1") .startNow() .withSchedule(simpleSchedule() .withIntervalInSeconds(40) .repeatForever()) .build(); // Tell quartz to schedule the job using our trigger sched.scheduleJob(job, trigger);
The code that defines the job uses methods that are statically imported from Jobbuilder. Similarly, the code that defines trigger uses methods that are statically imported from Triggerbuilder. In addition, the static method of the Simpleschedulerbuilder class is also introduced;
The statements that are statically imported from the DSL are as follows:
import static org.quartz.JobBuilder.*; import static org.quartz.SimpleScheduleBuilder.*; import static org.quartz.CronScheduleBuilder.*; import static org.quartz.CalendarIntervalScheduleBuilder.*; import static org.quartz.TriggerBuilder.*; import static org.quartz.DateBuilder.*;
Schedulerbuilder interface of various implementation classes, you can define different types of scheduling plan (schedule);
The Datebuilder class contains a number of methods that make it easy to construct a Java.util.Date instance that represents a different point in time, such as a point in time that defines an even number of one hours, and if the current time is 9:43:27, the time defined is 10:00:00.
Job and Trigger
A job is a class that implements the job interface, which has only one method:
Job Interface:
package org.quartz; public interface Job { public void execute(JobExecutionContext context) throws JobExecutionException; }
When a trigger of the job is triggered (later), the Execute () method is called by a worker thread scheduler, and some of the information that is passed to the Execute () method is stored in the Jobexecutioncontext object that the job runs. , perform a reference to the job's scheduler, trigger the job's trigger reference, Jobdetail object reference, and some other information.
The Jobdetail object is created by the client program (your program) when the job is joined to scheduler. It contains various property settings for the job, as well as jobdatamap for storing job instance state information. This section is a brief introduction to the job instance, and more details will be covered in the next section.
The trigger is used to trigger job execution. When you are ready to dispatch a job, you create an instance of trigger, and then set the scheduling-related properties. Trigger also has an associated jobdatamap that is used to pass some trigger-related parameters to the job. Quartz comes with a variety of different types of trigger, the most commonly used mainly are Simpletrigger and Crontrigger.
Simpletrigger is primarily used for a one-time job (only once at a specific point-in), or when the job executes at a specific point in time, repeated n times, and each time the interval is performed in t units. Crontrigger is useful in calendar-based scheduling, such as "noon per Friday", or "10:15 on the day of the month".
Why both job and trigger? Many task schedulers do not differentiate between job and trigger. Some schedulers simply define a job with a single execution time and some job identifiers, and some other schedulers combine the quartz job with the trigger object. When developing quartz, we think it is reasonable to separate the dispatch from the task to be dispatched. In our view, this can bring many benefits.
For example, after a job is created, it can be saved in scheduler, is independent from trigger, and can have multiple trigger for the same job; another benefit of this loose coupling is that when trigger associated with a job in scheduler expires, You can configure the job to be re-dispatched later without redefining the job, and you can modify or replace the trigger without redefining the job associated with it.
Key
When you register jobs and trigger with scheduler, you can set key for them to configure their identity properties. The job and trigger keys (Jobkey and Triggerkey) can be used to put jobs and trigger into different groupings, and then operate on groups. The name of the job or trigger under the same grouping must be unique, that is, a job or trigger key consists of a name and a grouping (group).
For jobs and trigger, you now have a general understanding of this, in more detail, see Tutorial Three and tutorial four.
original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Quartz Tutorial II: api,job and Trigger
Quartz Tutorial II: api,job and Trigger