What is Job Schedule System? Job Schedule System is a system responsible for executing or notifying an application component in a predefined time. For example, if you send Email at 9:30 on Monday, I will send Email to inform the latest business situation.
Java.util.Timer and Java.util.Timertask Timer and Timertask are classes that can complete the two JDKs of Job Schedule, but this cannot be called a System. Timer and Timertask are very simple, not directly supporting the planning schedule of persistent tasks, thread pools and similar calendars, and developers to perform a lot of extensions in completing some advanced features.
Quartz's brief introduction Quartz is OpenSymphony organization specializes in the Job Scheduling field, and you can view more information from http://www.opensymphony.com/quartz. Quartz is a lightweight component, and developers only need to load a separate JAR package that can take advantage of QUARTZ powerful schedule. Of course, if you are equipped with the characteristics of the database persistence task for Quartz, Quartz can use this well, so that you can remember your original plan after the machine is restarted.
In Quartz, we have exposed the most interface to make the Scheduler interface, the interface provides planned features, such as the Schedule / Unschedule program, Start / Pause / Stop Scheduler.
Quartz provides some commonly used listner (Joblistener, TriggerListener, SchedulerListener) for full monitoring schedule arrangements and execution.
Start our Quartz Tour
HelloWorld Example:
If you want to see a HelloWorld, you will 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, 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, and execute SayHelloWorldJob to see the console print hello world.
Review Hello World EXAMPLE:
What is JOB? Interface Job is an interface that needs to be implemented at each business that needs to be implemented. This interface has only one way:
Package Org.quartz; Public Interface Job {public void Execute (JobExecutionContext Context) THROWS JOBEXECUTIONEXCEPTION;
The Execute method is also the method of quartz callback after arrival, we make SayeloworldJob implement JOB interface to provide print function.
What is JobDetail? JobDetail describes a specific information, such as name, group name, and more. JobDetail JobDetail = New JobDetail ("SayHelloWorldJob", Scheduler.default_group, SayHelloWorldJob.class; in the above construction method, the first is the name of the task, the second is a group name, the third is actually actually required to execute Callback class.
What is TRIGGER? Trigger As the name is the trigger, Quartz has a good idea to isolate the conditions for the task and task execution. TRIGGER is a class that controls task execution conditions. When Trigger considers the time of performing conditions, TRIGGER will notify the relevant Job to execute. The benefits of separation are: 1. You can associate multiple TRIGGERs for a Job, where any of the conditions can trigger JOB execution, so you can complete some combined advanced trigger conditions 2. When Trigger fails (such as: one forever Can't satisfy the conditions), you don't have to declare a new job, instead, you can associate a new Trigger for Job let Job can continue.
In the current Quartz implementation, there are two Trigger, Simpletrigger and Crontrigger, Simpletrigger to complete tasks such as fixed time, such as 1 minute from now, etc .; Crontrigger (Yes, and UNIX's cron process The same thing is the task used to perform Calendar-Like, such as: 3:00 pm every Friday, monthly, the last day, etc.
Trigger Trigger = New Simpletrigger ("SayhelloWorldjobtrigger", Scheduler.default_Group, New Date (), NULL, 0, 0L); In this constructor, the first is the name of Trigger, the second is the group name of Trigger, the third The task start time, the fourth is the end time, the fifth is the number of repetitions (using the simpletrigger.repeat_indefinitely constant), the last one is a repetition cycle (in milliseconds), then this creates an immediate and only Implement a task. Scheduler.ScheduleJob (JobDetail, Trigger); This statement is to associate Job and Trigger so that when TRIGGER thinks it should be triggered (actually a Scheduler call) Job.execute method.
Scheduler.Start (); Don't forget to add the above statement, this statement notifies the Quartz to enable the plan to take effect.
About the parameter of the Execute method JobExecutionContext JobExecutionContext, like the class function ending with many Context, the context environment of the runtime, JobExecutionContext has a reference to many objects such as Scheduler, JobDetail, Trigger, so that these objects are required inside the Execute method. The convenience is available.
JobDetail and Trigger's Name and Group Scheduler instances correspond to many Job and Trigger instances. For the convenience of distinguishing, Quartz uses Name and Group features, as you want, there is no two identical Name under the same group. JobDetail, Trigger does not have two JobDetail, TRIGGER, JobDetail, Trigger, JobDetail, and Trigger, is: Group Name
More in-depth thinking ...
The example of HelloWorld is not enough to explain some problems, some people may ask this way: What should I do if some extra data is needed in an Execute method? For example, Execute I wish to send an email, but I need to know the sender of the email, the recipient and other information?
There are two solutions: 1.JobDataMap class: Each JobDetail is associated with a JobDataMap instance, JobDataMap is subclass of java.util.map, basically providing data in the form of key-value, and provides some convenience ( Mainly support for Java basic data types, such as PUT (String Key, Int Value), when developers create JobDetail, you can put additional information in JobDataMap, then you can find the required values according to 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 data stock JobDetail (using RAM by default), there is a fatal Weakness, you can't put objects that do not implement Java.io.Serializable into JobDataMap, because Quartz will save (or by configuring the configuration file to close) Objects in JobDataMap. For example, you need a java.sql.connection interface instance in the Execute method, so usually you can't put the connection in JobDataMap, even if you just want to use it in Execute. (Note: Readers can temporarily think that the above words are correct, but can change this behavior by indicating quartz, that belongs to advanced topics)
2. If you need a java.sql.connection, you can do some operations in Execute, then you can put the connection into the Quartz's SchedulerContext, Execute can also be accessed, and Quartz does not persist anything in SchedulerContext. .
Scheduler.getContext (). PUT ("Java.sql.connection", Connection;
EXECUTE Connection Con = (Connection) JobExecutionContext.getscheduler (). getContext (). Get ("java.sql.connection);
Sleal continuation ...