Java task Scheduling Class library Quartz preliminary __java

Source: Internet
Author: User

This article is based on the Quartz 2.2 version, the official address is as follows:

http://www.quartz-scheduler.org/

Now the online article about quartz, mostly 1.x version, but after the 2.x version, Quartz syntax has been very different.

The main point is that quartz has discarded many of the class's construction methods and instead has initialized them with the constructor classes (Builder) of these classes.

For quartz, there are three most basic components: Scheduler, job, and trigger. Where: Scheduler is the quartz of the execution object, through which to load the job and trigger, set the scheduling parameters, as well as start, stop, pause, restore task scheduling execution process; Job is a task scheduling task, it is divided into two modules: one is to implement a self-job interface class, It is responsible for completing various operational actions in the task; the other is the Jobdetail class, which contains all the information about the task, including the data required for the task and the behavior (that is, jobdetail not only load the task data, but also load the behavior classes that implement the self-job interface); trigger is the trigger It sets information such as when the task will start/end, the execution interval, the number of executions, and so on.

As mentioned earlier, in the 1.x version, the initialization of the above functional class is done through its own construction method. However, in the 2.x version, most of the functional class generation needs to be implemented through a dedicated builder. The following code is an example:

Import Org.quartz.CalendarIntervalScheduleBuilder;
Import Org.quartz.CronScheduleBuilder;
Import Org.quartz.JobBuilder;
Import Org.quartz.JobDetail;
Import Org.quartz.Scheduler;
Import org.quartz.SchedulerException;
Import Org.quartz.SimpleScheduleBuilder;
Import Org.quartz.Trigger;
Import Org.quartz.TriggerBuilder;

Import Org.quartz.impl.StdSchedulerFactory; public class Quartztest {public static void main (string[] args) throws Schedulerexception {Scheduler s = new Stdsch
		Edulerfactory (). Getscheduler ();

		S.start ();
		Jobdetail job = Jobbuilder.newjob (). OfType (Testjob.class). Withidentity ("Job1", "group1"). Build (); Trigger Trigger = Triggerbuilder. Newtrigger (). Withidentity ("Trigger1", "group1"). Startnow ()//. Withsche Dule (Simpleschedulebuilder.repeatsecondlyfortotalcount (2))//. Withschedule (Cronschedulebuilder.cronschedule (
				"0 30 9 * *")) . Withschedule (Calendarintervalschedulebuilder.calendarintervalschedule (). Withintervalinhours (2)). bUild ();

S.schedulejob (Job, trigger);
	S.shutdown (TRUE); }

}

As you can see from the example code, Jobdetail and trigger are generated by builder except scheduler.

In the current version, simply, there are two ways to generate scheduler, which are not very different:

Scheduler s = new Stdschedulerfactory (). Getscheduler ();

Scheduler s = Stdschedulerfactory.getdefaultscheduler ();

Task scheduling does not begin until the scheduler Start method is invoked. However, the call to start can be done before jobdetail and trigger are loaded or later.

Jobdetail is generated through Jobbuilder, and Jobbuilder creates an instance of Jobbuilder through its static method Newjob. For a Jobbuilder object, OfType is the method that must be invoked to load the class that implements the job interface (the Testjob class in the example). The Withindentify method will generate a private member of the Jobkey, which is the unique flag of the Jobdetail object.

Trigger is generated by Triggerbuilder, Triggerbuilder creates an instance of Triggerbuilder through its static method Newtrigger. As with Jobdetail, Triggerbuilder also creates a unique flag for trigger objects through the Withindetify method. In addition, there are startnow,startat,endat and other methods in Triggerbuilder to set the time when the task triggers and ends.

If you have used the 1.x version of the quartz, you should know that triggers are divided into Simpletrigger, Crontrigger, etc. But in the 2.x version, these specific trigger classes are discarded and replaced by the Withschedule method in Triggerbuilder. The method needs to pass in a Sechdulebuilder object that implements the logic of the trigger.

As shown in the example, there are three types of Schedulebuilder in version 2.2, divided into Simpleschedulebuilder,cronschedulebuilder and Calendarintervalschedulebuilder: Simpleschedulebuilder is a simple invocation trigger that can only specify the interval and number of executions that are triggered; Cronschedulebuilder is a trigger similar to a Linux cron. It specifies the trigger rule through a rule called cronexpression, usually the specific time of each trigger; (for Cronexpression, see: Official, Chinese web) Calendarintervalschedulebuilder is a supplement to Cronschedulebuilder, which can be specified to be triggered every once in a while.

It should be noted that withschedule can be invoked at most once by the same Triggerbuilder object. If not invoked, the task executes immediately and executes only once.

In addition, both jobdetail and trigger have usingjobdata to load specific data.

The Schedulejob method is one of the scheduler objects that can load jobdetail and trigger objects in many ways. In addition, there are addjob and so on. It is important to note, however, that jobdetail and trigger are not allowed in scheduler, in which case the scheduler will run empty (that is, a dead loop), but once the jobdetail has been loaded, at least one trigger must be loaded and vice versa.

When the shutdown method is invoked, the scheduler ends the loop and ends all of its child threads. The shutdown method has two prototypes:

void shutdown ();

void Shutdown (Boolean);

Shutdown () is equivalent to shutdown (false). Its Boolean parameter represents whether to wait for the task to execute. False to terminate the schedule unconditionally immediately and, if true, to allow the task that is currently waiting or being executed to complete before it ends.

For more information on quartz, see the Quartz Development Guide and API documentation.

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.