The principle of "turn" Spring quartz

Source: Internet
Author: User

Quartz is a famous Java version of the open-source timing scheduler, powerful, easy to use.

First, the core concept

Quartz's principle is not very complex, just understand a few concepts, and then know how to start and close a scheduler can.

1. Job

Represents the specific content of a job to be performed. There is only one method in this interface

void execute (Jobexecutioncontext context)

2, Jobdetail

Jobdetail represents a specific executable scheduler, the job is what the executable scheduler to do, and Jobdetail also contains the task scheduling scheme and policy.

3, trigger represents a scheduling parameter configuration, when to tune.

4, Scheduler represents a dispatch container, a dispatch container can register multiple jobdetail and trigger. When Trigger and Jobdetail are combined, they can be dispatched by the Scheduler container.

Second The simplest example of getting started

 PackageTest.quartz;Importjava.util.Date;ImportOrg.quartz.Job;ImportOrg.quartz.JobExecutionContext;Importorg.quartz.JobExecutionException; Public classMyJobImplementsJob { Public voidExecute (jobexecutioncontext jobexecutioncontext)throwsjobexecutionexception {System.out.println (NewDate () + ": Doing something ..."); }}
 PackageTest.quartz;Importjava.util.Date;ImportOrg.quartz.JobDetail;ImportOrg.quartz.Scheduler;Importorg.quartz.SchedulerException;Importorg.quartz.SchedulerFactory;ImportOrg.quartz.SimpleTrigger;Importorg.quartz.impl.StdSchedulerFactory; Public classTestquartz { Public Static voidMain (string[] args) {//1. Create a Jobdetial objectJobdetail Jobdetail =NewJobdetail (); //set up work ItemsJobdetail.setjobclass (MyJob.class); Jobdetail.setname ("Myjob_1"); Jobdetail.setgroup ("Jobgroup_1"); //2. Create a Trigger objectSimpletrigger Strigger =NewSimpletrigger (); Strigger.setname ("Trigger_1"); Strigger.setgroup ("Trigger_group_1"); Strigger.setstarttime (NewDate ()); //set the repeat stop time and destroy the trigger objectJava.util.Calendar C =java.util.Calendar.getInstance (); C.settimeinmillis (System.currenttimemillis ()+ 10000 * 1L);        Strigger.setendtime (C.gettime ()); Strigger.setfireinstanceid ("Trigger_1_id_001"); //Set repeat interval timeStrigger.setrepeatinterval (1L); //set number of repeat executionsStrigger.setrepeatcount (3); //3. Create Scheduler objects and configure Jobdetail and trigger objectsSchedulerfactory SF =Newstdschedulerfactory (); Scheduler Scheduler=NULL; Try{Scheduler=Sf.getscheduler ();            Scheduler.schedulejob (Jobdetail, Strigger); //4, and do the start, shutdown and other operationsScheduler.start (); //Scheduler.shutdown (true);        } Catch(schedulerexception e) {e.printstacktrace (); }//try {//            //Shutdown Scheduler//Scheduler.shutdown (true);//} catch (Schedulerexception e) {//e.printstacktrace ();//        }    }}

Execution Result:

When the end time is changed to:

Set the repeat stop time and destroy the trigger object
Java.util.Calendar C = java.util.Calendar.getInstance ();
C.settimeinmillis (System.currenttimemillis () + 1L);
Strigger.setendtime (C.gettime ());

Execution Result:

When you add a statement that closes the scheduler:

4, and do the start, shutdown and other operations
Scheduler.start ();
Scheduler.shutdown (TRUE);

Program execution Results:

Thu Jul 10:11:50 CST 2009:doing something ...

Process finished with exit code 0

Executes only once, this time can be executed, because Scheduler.shutdown (true) is set, and True indicates that it waits for the task to finish executing.

As can be seen from here, Scheduler is a container, scheduler control Jobdetail execution, the control strategy is through the trigger.

When the scheduler container is started, the jobdetail can be executed according to the associated trigger policy. When the scheduler container is closed, all jobdetail stop executing.

Third, through the example to see the principle

By studying the source code of quartz, and this example, I finally realized how quartz works.

1, Scheduler is a Planning scheduler container (Headquarters), the container can be filled with a large number of jobdetail and trigger, when the container starts, each jobdetail in the trigger according to the step-up automatic execution.

2, Jobdetail is an executable work, which itself may be stateful.

3, trigger represents a scheduling parameter configuration, when to tune.

4, when Jobdetail and trigger in the Scheduler container registration, the formation of a good assembly of the work (Jobdetail and trigger a pair of children), can accompany the container start and dispatch execution.

5, Scheduler is a container, there is a thread pool in the container, used to schedule each job in parallel, which can improve the efficiency of the container.

6, the above structure is represented by a graph, as follows:

Iv. Summary

1. Make clear the principle and process of the operation of the quartz container, and the way the work is formed, and the method of registering the job with the container. I know the core principle of quartz.

2, quartz although very large, but everything revolves around this core, in order to configure a powerful time scheduling strategy, you can study specialized Crontrigger. To flexibly configure job and container properties, you can do so through the Quartz property file or XML.

3, in order to dispatch more persistent, structured operations, you can read the job through the database, and then put into the container to execute.

4, everything revolves around this core principle, to understand this, and then to study more advanced usage is much easier.

5, quartz and spring integration is also very simple, Spring provides a set of beans to support: Methodinvokingjobdetailfactorybean, Simpletriggerbean, Schedulerfactorybean, see what you need to inject into the property to understand. Spring launches the Quartz container when the spring container is started.

6, quartz container closed way is also very simple, if it is spring integration, there are two methods, one is to close the spring container, one is to get to the Schedulerfactorybean instance, and then call a shutdown to get it done. If quartz is used independently, call Scheduler.shutdown (true) directly;

7, quartz jobdetail, trigger can be reset at runtime, and at the next call to play a role. This provides a basis for the implementation of dynamic jobs. You can put the scheduling time policy into the database, and then set the trigger through the database data, so that the dynamic scheduling can be generated.

Transferred from: http://my.oschina.net/laigous/blog/260816

Original: http://lavasoft.blog.51cto.com/62575/181907/

Associated JAR Packages:

Log4j-1.2.12.jar

Quartz-1.8.4.jar

Slf4j-api-1.7.2.jar

Slf4j-log4j12-1.6.1.jar

The principle of "turn" Spring quartz

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.