Quartz integration of spring task scheduling

Source: Internet
Author: User

Recommend a blog: http://blog.csdn.net/column/details/14251.html

Basic concepts

Job : is an interface with only one method void execute (Jobexecutioncontext context), the developer implements the interface definition to run the task, and the Jobexecutioncontext class provides various information about the scheduling context. The job runtime information is stored in the Jobdatamap instance;

Jobdetail : Quartz re-creates a job instance each time the job is executed, so it does not directly accept an instance of the job, instead it receives a job implementation class so that the job is instantiated by the reflection mechanism of newinstance () at runtime. Therefore, a class is needed to describe the job implementation class and other related static information, such as job name, description, Association Listener and other information, jobdetail assume this role.

The constructor of this class gives you a more specific understanding of its function: Jobdetail (java.lang.String name, java.lang.String Group, Java.lang.Class Jobclass), This constructor requires specifying the implementation class for the job, as well as the group name and job name of the task in scheduler;

Trigger : is a class that describes the time-triggered rules that trigger job execution. There are mainly two subclasses of Simpletrigger and Crontrigger. Simpletrigger is the most suitable choice when only one or a fixed time interval is required, while Crontrigger can define scheduling schemes for various complex time rules through cron expressions: 5:0 per 9:00, Monday, Wednesday, Friday pm 0 implementation, etc.;

Calendar : Unlike Org.quartz.Calendar and Java.util.Calendar, it is a collection of some calendar-specific points of time (you can simply consider Org.quartz.Calendar as a collection of Java.util.Calendar- Java.util.Calendar represents a calendar point in time, without special instructions the calendar that follows refers to Org.quartz.Calendar). A trigger can be associated with more than one calendar to exclude or include certain points in time.

Let's say we schedule a task for Monday 10:00 every week, but if you encounter a legal holiday and the task does not execute, then you need to use the calendar for point exclusion on the basis of the trigger trigger mechanism. For different time-period types, Quartz provides several calendar implementation classes under the Org.quartz.impl.calendar package, such as Annualcalendar, Monthlycalendar, Weeklycalendar are defined for each year, month and week respectively;

Scheduler : represents a quartz independent runtime container, trigger and Jobdetail can be registered to scheduler, and both have their own group and name in scheduler. Groups and names are the basis for locating an object in a scheduler container, the group and name of the trigger must be unique, and the Jobdetail group and name must be unique (but can be the same as the trigger group and name, because they are of different types). Scheduler defines multiple interface methods that allow external access and control of trigger and jobdetail in the container through groups and names.

Scheduler can bind trigger to a jobdetail so that the corresponding job is executed when the trigger is triggered. A job can correspond to multiple trigger, but one trigger can only correspond to one job. You can create a scheduler instance from Schedulerfactory. Scheduler has a schedulercontext that is similar to ServletContext, holds scheduler contextual information, and the job and trigger can access information within Schedulercontext. The Schedulercontext internally uses a map to maintain these contextual data in a key-value pair, Schedulercontext provides multiple put () and getxxx () methods for saving and retrieving data. The corresponding Schedulercontext instance can be obtained by scheduler# GetContext ();

ThreadPool : Scheduler uses a thread pool as the infrastructure for the task to run, and the task improves efficiency by sharing threads in the thread pool.

The job has a statefuljob subinterface that represents a stateful task, which is a tabbed interface with no methods, and is designed to let quartz know the type of the task in order to take different execution scenarios. Stateless tasks have their own jobdatamap copies at execution time, and changes to Jobdatamap do not affect the next execution. While stateful task sharing shares the same Jobdatamap instance, the changes made to jobdatamap each time the task is executed are saved, and subsequent executions can see the change, which affects subsequent executions after each execution of the task.

For this reason, stateless jobs can execute concurrently, while stateful Statefuljob cannot be executed concurrently, which means that if the previous statefuljob has not been completed, the next task will block the wait until the previous task has finished executing. Stateful tasks require more considerations than stateless tasks, and programs tend to have a higher degree of complexity, so you should try to use stateless jobs unless necessary.

If quartz uses the database persistence task scheduling information, the stateless Jobdatamap will only be maintained once scheduler the task is registered, and the jobdatamap corresponding to the status task will be saved after each execution of the task.

The trigger itself can also have a jobdatamap, and its associated job can get trigger in Jobdatamap by Jobexecutioncontext#gettrigger (). Getjobdatamap (). Regardless of the stateful or stateless task, changes made to the trigger jobdatamap during the execution of the task are not persisted and will not have an impact on the next execution.

Quartz has a sophisticated event and monitoring system, and most of the components have events, such as pre-task events, post-task events, pre-trigger events, post-trigger events, scheduler start events, shutdown events, and so on, to register the appropriate listener to handle events of interest.

Quartz and Spring Integration dependencies:
<!--Quartz related Dependencies -<Dependency>    <groupId>Org.quartz-scheduler</groupId>    <Artifactid>Quartz</Artifactid>    <version>2.2.1</version></Dependency><Dependency>    <groupId>Org.quartz-scheduler</groupId>    <Artifactid>Quartz-jobs</Artifactid>    <version>2.2.1</version></Dependency><!--Spring related dependencies -<Dependency>    <groupId>Org.springframework</groupId>    <Artifactid>Spring-context</Artifactid>    <version>4.0.5.RELEASE</version></Dependency><Dependency>    <groupId>Org.springframework</groupId>    <Artifactid>Spring-context-support</Artifactid>    <version>4.0.5.RELEASE</version></Dependency><Dependency>    <groupId>Org.springframework</groupId>    <Artifactid>Spring-tx</Artifactid>    <version>4.0.5.RELEASE</version></Dependency>
Task class: Need to implement the job interface
 Public class Implements Job {    publicvoidthrows  jobexecutionexception {        simpledateformat ft =new simpledateformat ("Yyyy-mm-dd HH:mm:ss");        System.out.println ("Current time is:" +ft.format (new  Date ()));}    }
Execution class:
 Public class demotest {    publicstaticvoid  main (String [] args) {        ApplicationContext app=new classpathxmlapplicationcontext ("Classpath:/app-context.xml");}    }

Quartz integration of spring task 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.