Quartz, enterprise plan/schedule (Job Schedule) system (1)-Introduction

Source: Internet
Author: User

What is Job Schedule System?
The Job Schedule system is a system that executes or notifies an application component at a predefined time. For example, send an email to notify the customer of the latest business situation at every Monday morning.

Java. util. timer and Java. util. timertask
Timer and timertask are two classes provided by JDK that can complete job schedule. However, they cannot be called a system. Timer and timertask are simple. They do not directly support persistence tasks, thread pools, and calendar-like schedules, developers need to perform a lot of expansion to complete some advanced functions.

Quartz Introduction
Quartz is another open-source tool in the field of job scheduling by the opensymphony organization. You can go to http://www.opensymphony.com/quartz#for detailed information. Quartz is a lightweight component. Developers only need to load a separate jar package to use the powerful schedule function of quartz. Of course, if you have configured the database persistence task feature for quartz, quartz can also make good use of this feature, so that you can remember your original plan after the machine restarts.

In quartz, we have the most contact with the scheduler interface, which provides the scheduling function, such as schedule/unschedule plan, start/pause/Stop scheduler.

Quartz provides some commonly used listener (joblistener, triggerlistener, schedulerlistener) for full monitoring of plan arrangement and execution.

Start our quartz journey

  • Helloworld example:

I'm sure you 'd like to take a look at the helloworld example, so we should start with hellowworld.

Import java. util. date;
Import org. Quartz. job;
Import org. Quartz. jobdetail;
Import org. Quartz. jobexecutioncontext;
Import org. Quartz. jobexecutionexception;
Import org. Quartz. scheduler;
Import org. Quartz. schedulerfactory;
Import org. Quartz. Trigger;
Import org. Quartz. impl. stdschedulerfactory;
/**
* @ Author snowway
* @ Version $ ID $
*/
Public class sayhelloworldjob implements job {
/*
* (Non-javadoc)
*
* @ See org. Quartz. Job # execute (Org. Quartz. jobexecutioncontext)
*/
Public void execute (jobexecutioncontext context) throws jobexecutionexception {
System. Out. println ("Hello world! ");
}

Public static void main (string [] ARGs) throws exception {
Schedulerfactory factory = new stdschedulerfactory ();
Scheduler scheduler = factory. getscheduler ();

Jobdetail = new jobdetail ("sayhelloworldjob ",
Scheduler. default_group,
Sayhelloworldjob. Class );

Trigger trigger = new simpletrigger ("sayhelloworldjobtrigger ",
Scheduler. default_group,
New Date (),
Null,
0,
0l );
Scheduler. schedulejob (jobdetail, trigger );
Scheduler. Start ();
}
}

For the sake of simplicity, I wrote the main method in sayhelloworldjob. Execute sayhelloworldjob and you can see that the console prints Hello world.

  • Review Hello world example:

What is a job?
An interface job is the interface that needs to be implemented for tasks to be executed on each business. This interface has only one method:

Package org. quartz;

Public interface job {

Public void execute (jobexecutioncontext context)
Throws jobexecutionexception;
}

The execute method is the quartz callback method after the time is reached. We enable sayhelloworldjob to implement the job interface to provide the printing function.

 What is jobdetail?
Jobdetail describes the specific information of a task, such as the name and group name.
Jobdetail = new jobdetail ("sayhelloworldjob ",
Scheduler. default_group,
Sayhelloworldjob. Class );
In the preceding constructor, the first is the task name, the second is the group name, and the third is the actual callback class to be executed by the task.

What is Trigger?
Trigger, as its name implies, is a trigger. Quartz has a good idea of separating the task and the task execution conditions. A trigger is a class that controls the execution conditions of a task. When a trigger determines that the execution conditions are met, the trigger notifies the relevant job to execute the task. The advantages of separation are:
1. You can associate multiple triggers for a job. If any of the conditions is met, job execution can be triggered. In this way, advanced Triggering Conditions of some combinations can be completed.
2. when a trigger fails (for example, a condition that cannot be met), you do not have to declare a new job, instead, you can associate a new trigger for the job so that the job can continue to run.

In the current quartz implementation, there are two types of triggers: simpletrigger and crontrigger. simpletrigger is used to complete some tasks that are executed at a fixed time, for example, one minute from now on; the crontrigger (yes, the same as the Unix cron process) is used to execute the calendar-like task, for example, every Friday, the last day of each month, and so on.

Trigger trigger = new simpletrigger ("sayhelloworldjobtrigger ",
Scheduler. default_group,
New Date (),
Null,
0,
0l );
In this constructor, the first is the trigger name, the second is the trigger group name, the third is the task start time, the fourth is the end time, and the fifth is the repetition.
Number of times (expressed as an infinite number using simpletrigger. repeat_indefinitely constant), the last is the recurrence (unit: milliseconds), then create
Immediately and only once.

Scheduler. schedulejob (jobdetail, trigger );
The route cute method is used.

Scheduler. Start ();
Do not forget to add the preceding statement to notify quartz to make the schedule take effect.

About the parameter jobexecutioncontext of the execute Method
Jobexecutioncontext is similar to many class functions at the end of context. It provides a runtime context in which jobexecutioncontext includes
Schedgger, jobdetail, trigger, and many other objects are referenced, So that you need the convenience of these objects within the execute method.

 Name and group of jobdetail and trigger
The schedgger instance corresponds to many job and trigger instances. For convenience, quartz uses the name and group features, just as you would like,
The same group cannot have two jobdetail with the same name. The same applies to trigger.
The same Scheduler cannot have two jobdetail instances of the same group.
The full name of jobdetail and trigger is: group + name

  • More in-depth thinking...

The helloworld example is not enough to explain some problems. Some people may ask: What if the execute method requires some additional data? For example, execute
To send an email, but do I need to know the sender and receiver of the email?

There are two solutions:
1. jobdatamap class:
Every jobdetail is associated with a jobdatamap instance, and jobdatamap is Java. util. map subclasses provide data in the key-value format and provide some convenient methods (mainly for the support of basic java data types, such as put (string key, int value). When a developer creates jobdetail, the additional information can be put into jobdatamap. Then, the required value can be found based on the key in the execute method.
Jobdetail job = new jobdetail ....
Job. getjobdatamap (). Put ("from", "snowway@vip.sina.com ");
...

In Execute
String from = jobexecutioncontext. getjobdetail (). getjobdatamap (). getstring ("from ");
....

However, when you use the database to store jobdetail (by default, Ram is used), there is a fatal weakness here, and you cannot implement Java. io. the serializable object is placed in jobdatamap, because quartz will save the objects in the serialized jobdatamap using the Blob field (which can also be closed through the configuration file. For example, you need a java. SQL. connection interface instance, which is also common, so you generally cannot put the connection into jobdatamap, even if you only want to use it in execute. (Note: The reader may temporarily think that the above paragraph is correct. However, it is a high-level topic to instruct quartz to change such behavior)

2. assume that you need a java. SQL. connection is used to complete some operations in execute. You can put the connection into the schedulercontext of quartz. Execute can also be accessed, and quartz won't persist anything in schedulercontext.

Schedcontext. getcontext (). Put ("Java. SQL. Connection", connection );

Execute
Connection con = (connection) jobexecutioncontext. getscheduler (). getcontext (). Get ("Java. SQL. Connection ");

To be continued...

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.