Quartz, Enterprise-Class planning/scheduling (Job Schedule) system (1)-Introduction

Source: Internet
Author: User
Tags date execution implement interface

What is job schedule system? The job schedule system is one that is responsible for executing at predefined times or notifying an application component. For example, send an email every morning 9:30 to inform the customer about the latest business situation.

Java.util.Timer and Java.util.TimerTask Timer and TimerTask are the two JDK-provided classes that can complete job schedule, but this cannot be called a system. Timer and TimerTask are simple and do not directly support the scheduling of persistent tasks, thread pools, and similar calendars (calendar-like), and developers have extensive extensions to complete some of the advanced features.

Quartz's brief introduction quartz is opensymphony organization specialized job scheduling domain Another open source weapon, may go to the Http://www.opensymphony.com/quartz to view the detailed information. Quartz is a lightweight component that developers can take advantage of Quartz's powerful scheduling capabilities simply by loading a separate jar package. Of course, if you have the characteristics of a database persistence task for quartz, quartz can make good use of it so that you can remember the plan you originally arranged after the machine was restarted.

The interface that we touch most in quartz makes the Scheduler interface, which provides the function of scheduling, such as Schedule/unschedule plan, Start/pause/stop Scheduler.

Quartz provides a number of commonly used listener (Joblistener,triggerlistener,schedulerlistener) for full monitoring scheduling and execution.

Start our quartz trip.
HelloWorld Example:
Presumably everyone would like to see an example of a HelloWorld, then still 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 jobdetail = new Jobdetail ("Sayhelloworldjob", Scheduler.default_group, Sayhellow Orldjob.class);

Trigger Trigger = new Simpletrigger ("Sayhelloworldjobtrigger", Scheduler.default_group, NE         W Date (), NULL, 0, 0L);         Scheduler.schedulejob (Jobdetail, Trigger);     Scheduler.start (); } }

For the sake of simplicity, I wrote the Main method in Sayhelloworldjob, performing sayhelloworldjob can see the console print Hello world.
Review Hello World Example:
What's the job? An interface job is an interface that needs to be implemented for every task that needs to be performed on the business, and there is only one method for this interface:

Package Org.quartz;

Public interface Job {

public void execute (jobexecutioncontext context) throws jobexecutionexception; }

The Execute method, which is the quartz callback method when time arrives, lets sayhelloworldjob implement the job interface to provide print functionality

What is Jobdetail? Jobdetail describes a task's specific information, such as name, group name, and so on. Jobdetail jobdetail = new Jobdetail ("Sayhelloworldjob", Scheduler.default_group, Sayhellow Orldjob.class); In the above construction method, the first is the name of the task, the second is the group name, and the third is the callback class that actually executes when the task needs to be executed.

What is trigger? Trigger as the name implies is a trigger, quartz has a very good idea is to separate the task and task execution conditions. Trigger is the class that controls the execution conditions of a task, and trigger notifies the relevant job to execute when trigger considers the execution condition to be fulfilled. The benefits of separation are: 1. You can associate multiple trigger for a job, and any one of these conditions can trigger job execution, which completes some combination of advanced trigger conditions 2. When trigger fails (for example, a condition that is never satisfied), You don't have to declare a new job, instead you can associate a new trigger for the job so that the job can continue to execute.

In the current quartz implementation, there are two Trigger,simpletrigger and crontrigger,simpletrigger to accomplish tasks such as fixed-time execution, such as: 1 minutes from now, and so on; Crontrigger ( Yes, as is the case with the UNIX cron process) to perform calendar-like tasks such as: Every Friday 3:00, the last day of the month, and so on.

Trigger Trigger = new Simpletrigger ("Sayhelloworldjobtrigger", Scheduler.default_group, NE W Date (), NULL, 0, 0L); In this construction method, the first is the name of the trigger, the second is the trigger's group name, the third is the task start time, the fourth is the end time, and the fifth is the repetition number (using the simpletrigger.repeat_indefinitely constant to denote the infinite time). , the last one is a recurrence (in milliseconds), so you create a task that is immediately and only performed once.

Scheduler.schedulejob (Jobdetail, Trigger); This statement associates the job with the trigger, so that when the trigger thinks it should be triggered, it calls (actually the scheduler call) Job.execute method.

Scheduler.start (); Do not forget to add the above statement, this statement informs Quartz to make the scheduled plan effective.

About the parameters of the Execute method jobexecutioncontext Jobexecutioncontext, like many of the context-ending class features, provides a contextual environment for the runtime, in Jobexecutioncontext Scheduler,jobdetail,trigger and many other objects are referenced so that when you need these objects within the Execute method the convenience of the moment is provided.

The name and group scheduler instances of Jobdetail and trigger correspond to a number of job and trigger instances, and for convenience of distinction, Quartz uses the attributes of name and group, as you would expect, Cannot have two identical name Jobdetail,trigger under the same group the same same scheduler cannot have two same group Jobdetail,trigger The fully qualified name of Jobdetail and trigger is: Group + name
More in-depth thinking ...
HelloWorld's example is not enough to illustrate some of the questions that some might ask: what if some extra data is needed in the Execute method? For example, if you want to send an e-mail message in execute, I need to know the sender, receiver, etc. of the message.

There are two solutions: 1.JobDataMap class: Each jobdetail is associated with a Jobdatamap instance, Jobdatamap is a subclass of Java.util.Map, essentially providing key-value form of data, and provides some convenient methods (primarily support for Java basic data types, such as put (String key,int value)),   When the developer creates the Jobdetail, the additional information can be placed in the Jobdatamap, then the required value can be found in the Execute method based on the key.   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 (using RAM by default), here is a fatal weakness, You cannot put an object that does not implement java.io.Serializable into jobdatamap because quartz will use the BLOB field to save (or to close through the configuration file) the objects in the Jobdatamap that are serialized. For example, if you need a Java.sql.Connection interface instance in the Execute method, which is also common, then you usually can't put connection in Jobdatamap, even if you only want to use it in execute. (Note: The reader may temporarily think that the above passage is correct, but it can be changed by instructing Quartz, which is a high-level topic)

2. If you need a java.sql.Connection to perform certain operations in execute, you can put Connection into quartz schedulercontext, execute can also access, and quartz will not persist Schedul Any thing in the Ercontext.

Scheduler.getcontext (). Put ("Java.sql.Connection", Connection);

Execute Connection con = (Connection) Jobexecutioncontext.getscheduler (). GetContext (). Get ("java.sql.Connection");

Not 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.