Implementation of quartz timer and quartz timer principle Introduction _java

Source: Internet
Author: User
Tags time interval

First, the core concept

The principle of quartz is not very complicated, as long as you understand a few concepts and then know how to start and close a scheduler.

1. Job
Represents a job, the specific content to be executed. There is only one method in this interface
void execute (Jobexecutioncontext context)

2, Jobdetail
Jobdetail represents a specific executable scheduler in which the job is executed by the executable, and the Jobdetail includes the program and strategy for the Task Scheduler.

3, trigger represents a dispatch 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 scheduled by the scheduler container.

Second The simplest example of getting started

Copy Code code as follows:

Import org.quartz.*;
Import Org.quartz.impl.StdSchedulerFactory;

Import Java.util.Date;

/**
* Quartz timer Test
*
* @author leizhimin 2009-7-23 8:49:01
*/
public class Myjob implements Job {        public void Execute (jobexecutioncontext jobexecutioncontext) throws jobexecutionexception {
                 SYSTEM.OUT.PRINTLN (new Date () + ": doing something ...");
       }
}

Class Test {
public static void Main (string[] args) {
1. Create Jobdetial objects
Jobdetail jobdetail = new Jobdetail ();
Set up work Items
Jobdetail.setjobclass (Myjob.class);
Jobdetail.setname ("myjob_1");
Jobdetail.setgroup ("Jobgroup_1");

2. Create Trigger objects
Simpletrigger Strigger = new Simpletrigger ();
Strigger.setname ("trigger_1");
Strigger.setgroup ("Trigger_group_1");
Strigger.setstarttime (New Date ());
Set the repeat stop time and destroy the trigger object
Java.util.Calendar C = java.util.Calendar.getInstance ();
C.settimeinmillis (System.currenttimemillis () + 1000 * 1L);
Strigger.setendtime (C.gettime ());
Strigger.setfireinstanceid ("trigger_1_id_001");
Set a recurrence time interval
Strigger.setrepeatinterval (1000 * 1L);
Set repeat execution times
Strigger.setrepeatcount (3);

3. Create Scheduler objects and configure Jobdetail and trigger objects
Schedulerfactory SF = new Stdschedulerfactory ();
Scheduler Scheduler = null;
try {
Scheduler = Sf.getscheduler ();
Scheduler.schedulejob (Jobdetail, Strigger);
4, and perform start-up, shutdown and other operations
Scheduler.start ();

catch (Schedulerexception e) {
E.printstacktrace ();
}
try {
Shutdown Scheduler
Scheduler.shutdown (TRUE);
catch (Schedulerexception e) {
E.printstacktrace ();
//                }
}
}

Execution results:


When the end time is changed to:

Copy Code code as follows:

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

Execution results:

When you add a statement that closes the scheduler:
4, and perform start-up, shutdown and other operations
Scheduler.start ();
Scheduler.shutdown (TRUE);


Program execution Results:
Thu June 10:11:50 CST 2009:doing something ...

Process finished with exit code 0
Only once, this time it can be done, because Scheduler.shutdown (true) is set, and True indicates that the task will stop after the execution of this one is completed.

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

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

Three, through the example to see the principle

By studying the source code of quartz, and this example, we finally realized the working principle of quartz.

1, Scheduler is a scheduler container (headquarters), the container can be filled with a large number of jobdetail and trigger, when the container started, the inside of each jobdetail according to trigger step-by-step automatic implementation.

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

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

4, when the Jobdetail and trigger on the Scheduler container Register, formed a good assembly of the operation (Jobdetail and trigger composed of a pair of children), can be accompanied by the launch of the container and scheduled execution.

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

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


Four, summarizing

1, figuring out the principles and procedures for performing jobs on the quartz container, and how the job is formed, and how the job is registered to the container. Know the core principle of quartz.

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

3, to schedule more persistent, structured jobs, you can read jobs through the database and then execute them in the container.

4, everything revolves around this core principle, figuring this out, and it's much easier to study more advanced usage. The integration of

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

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

7, quartz jobdetail, trigger can be reset at run time, and will work at the next call. This provides a basis for the implementation of dynamic jobs. You can put the scheduling time strategy to the database, and then through the database data to set the trigger, so that can generate dynamic scheduling.

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.