Quartz Quick start of scheduling framework

Source: Internet
Author: User

Quartz is a very powerful task scheduling framework, with a small example to illustrate how to use it. <?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

First go to quartz website Download http://www.opensymphony.com/quartz/, the latest version of 1.6.2, this article is used in 1.6.1. Unzip the package to the local, which contains the quartz itself, documentation, source code, and dependent class packs.

Create a new Java project in Eclipse, add the Quartz jar file Quartz-1.6.1.jar to the build path, and quartz run at least two additional classes: Commons-collections-3.2.jar and Commons-logging-1.1.jar, these two files can be found in the Lib/core directory.

Here we can start writing the powerful features of the class experience quartz. The following is an example of a simple, timed output task, which is a simple task, which is to output a sentence every once in a while.

First, write class Printoutjob to implement a simple output task. The task that runs in the quartz framework is called job, and this class needs to implement the job interface,

Public class Printoutjob implements Job {

@Override

Public void Execute (jobexecutioncontext ctx) throws jobexecutionexception{

SYSTEM.OUT.PRINTLN ("Do task ...");

}

}

The task is now available, and a scheduler-scheduler is needed to run it. Add a new class Jobrunner:

Public class Jobrunner {

Public Static void Main (string[] args) {

Scheduler Scheduler = null;

Schedulerfactory schfactory = new stdschedulerfactory ();

Try {

Scheduler = Schfactory.getscheduler ();

Jobdetail initjob = new jobdetail ("PrintJob", "Jobgroup", Printoutjob. Class);

Simpletrigger trig = new simpletrigger ("Trigger1", "Jobgroup");

Date RunTime = Triggerutils.getevenseconddate (new date ());

Trig.setstarttime (RunTime);

Trig.setrepeatinterval (3000);

Trig.setrepeatcount (10);

Scheduler.schedulejob (Initjob, trig);

Scheduler.start ();

Catch (Schedulerexception e) {

E.printstacktrace ();

}

}

}

To get Scheduler to schedule your job according to your intent, you need to tell Schduler some details about the job, and the job details are saved in a Jobdetail instance.

New Jobdetail ("PrintJob", "Jobgroup", Printoutjob. Class);

This job is named "PrintJob", which belongs to the "Jobgroup" thread group, and the implementation class of the job is printoutjob. With the job details, but also tell the Scheduler scheduler how to do the scheduling, that is, at what time, according to what frequency to perform the job, providing this information is a trigger, we first create a simple type of trigger-simpletrigger, The following sets the properties for trigger:

Trig.setstarttime (RunTime)--Set the time when the job started running, here is the start immediately,

Trig.setrepeatinterval (3000)--set the run interval to be 3000 milliseconds or 3 seconds,

Trig.setrepeatcount (10)--Set up a job to execute 10 times altogether,

Then register the Jobdetail and Simpletrigger we created with the Shcduler:

Scheduler.schedulejob (Initjob, Trig)

Invoke the Start () method to start the Schduler, and the previously written tasks are executed according to our settings.

Simpletrigger can perform some simple scheduling functions for us, but we sometimes need more complex schedules, such as the Day of the month, or the number of runs a week, and so on. Quartz also provides a very powerful trigger-crontrigger,crontrigger to complete such a complex schedule, with a time expression that can accomplish many complex schedules, and its expression is the same as that of a cron under Linux. Use Crontrigger:

Crontrigger Jobtrigger = new crontrigger ("Onlinetrigger", "Triggergroup");

Cronexpression cexp = new cronexpression ("0/5 * * * *");

Jobtrigger.setcronexpression (CEXP);

It is important to note that, unlike using thread to implement timed scheduling, a quartz job requires neither inheritance thread nor implementation of the Runnable interface, and each execution of the job is a new instance.

The above example code shows that the job is run by the Scheduler dispatch, if you need to pass parameters to the job, it seems that there is no way to achieve, in fact, Quartz gives us a way to pass parameters to the job, through the Jobdetail Datamap, In the main class put the data to be passed into the datamap, for example we want to pass a parameter class config to the job:

Initjob.getjobdatamap (). Put ("config", config);

Job gets the data that is passed:

Config cfg = (config) ctx.getjobdetail (). Getjobdatamap (). Get ("config");

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.