Quartz Learning Notes 1:quartz Overview

Source: Internet
Author: User

Quartz is the leader in the open Source task scheduling framework, which provides a powerful task scheduling mechanism. Quartz allows developers to flexibly define the scheduling schedule for triggers and to associate mappings with triggers and tasks. In addition, Quartz provides the persistence mechanism of the dispatching operation Environment, can save and restore the dispatch scene, even if the system is shut down, the task dispatch field data will not be lost. In addition, Quartz also provides component-type listeners, various plug-ins, thread pools and other functions.first, quartz basic structureQuartz the domain problem of task scheduling is highly abstract, and presents the 3 core concepts of scheduler, task and trigger, and describes it through the core concepts of interface and class in Org.quartz.1) Job:is an interface with only one method void execute (jobexecutioncontext context), where the developer implements the interface to define the tasks that need to be performed, and the Jobexecutioncontext class provides various information about the scheduling contexts. The information in the job runtime is saved in the Jobdatamap instance.2) Jobdetail:Quartz creates a job instance every time the job is executed, so instead of accepting an instance of a job directly, it receives a job implementation class so that the runtime instantiates the job through the mechanism invoked by the newinstance () reflection. Therefore, a class is needed to describe the implementation class of the job and other related static information, such as job name, description, associated listener, etc., Jobdetail assumed this role. The constructor of the class can be more specific about its function: Jobdetail (java.lang.String name,java.lang.string group,java.lang.class jobclass), This constructor requires the implementation class for the specified job, and the group name and job name of the task in scheduler.3) Trigger:is a class that describes the time trigger rule that triggers a job execution. There are mainly two subclasses of Simpletrigger and Crontrigger. Simplegtrigger is the most appropriate choice when it is only necessary to trigger once or periodically at a fixed interval, while Crontrigger can define a variety of complex scheduling scenarios through a cron expression: 9:00 daily, every Monday, Wednesday, Friday 5:00.4) Calendar:Unlike Org.quartz.Calendar and Java.util.Calendar, it is a collection of some calendar-specific points of time ( You can simply view Org.quartz.Calendar as a Java.util.Calendar collection------Java.util.Calendar represents a calendar point in time, no special description of the following calendar means Org.quartz.Calendar ). A trigger can be associated with multiple calendar to exclude or contain certain points in time. Suppose, we schedule a weekly Monday 10:00 to perform a task, but if we encounter a statutory holiday, the task is not executed, then we need to use calendar for the trigger trigger mechanism based on fixed point exclusion. For different time period types, Quartz provides several calendar implementation classes under the Org.quartz.impl.calendar package, such as Annualcalendar, Monthlycalendar, Weekleycalendar are defined for each year, month, and week respectively.5) Scheduler:Represents a quartz independent running container, trigger and Jobdetail can be registered into scheduler, both of which have their own groups and names in scheduler. Groups and names are the basis for finding an object in a scheduler container, the combination of trigger groups and names must be unique, and the combination of jobdetail groups and names must be unique (but can be the same as the trigger group and name because they are different types and are in different sets )。 Scheduler defines multiple interface methods that allow external access to and control of trigger and jobdetail in a container through groups and names. Scheduler can bind trigger to a jobdetail so that when the trigger is triggered, the corresponding job is executed. A job can correspond to multiple trigger, but a trigger can only correspond to one job. You can create a scheduler instance from Schedulerfactory. Scheduler has a schedulercontext that preserves scheduler contextual information and can understand schedulercontext against the familiar ServletContext. Both job and trigger can access information within the Schedulercontext. 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 fetching data. The corresponding Schedulercontext instance can be obtained by Scheduler#getcontext ().6) ThreadPool:Scheduler uses a thread pool as the infrastructure for the task to run, and the task increases the efficiency of the operation through threads in the shared thread pool.
second, stateful tasks and stateless tasksThe job has a statefuljob sub-interface that represents a stateful task, which is a label interface with no methods, and its purpose is to let quartz know the type of task in order to take different execution scenarios. Stateless tasks have their own jobdatamap replication at execution time, and changes to Jobdatamap do not affect the next execution.         While stateful tasks share a Jobdatamap instance, changes to Jobdatamap are saved each time the task is executed, and subsequent execution can see the change, which affects subsequent execution after each execution of the task. For this reason, stateless jobs can be executed concurrently, and 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 have more factors to consider than stateless tasks, and programs tend to be more complex, so you should avoid using stateful jobs unless necessary.         If Quartz uses database persistence task scheduling information, stateless Jobdatamap will only be saved once when the task is scheduler registered, and the jobdatamap of the stateful task will be saved each time the task is executed. Trigger itself can also have a jobdatamap, the jobdatamap of the period associated can be obtained by Jobexecutioncontext#gettrigger (). Getjobdatamap. Changes made to trigger's Jobdatamao during task execution are not persisted, regardless of status or stateless tasks, that do not affect the execution of the next task.
Quartz has a perfect event and monitoring system, most of the components have events, such as the task before the event, task execution events, triggers before trigger events, triggered events, scheduler start events, shutdown events, etc., you can register the appropriate listener to handle the event of interest.                                                             The following figure describes the internal component structure of the Scheduler, Schedulercontext provides scheduler globally visible context information, each task corresponds to a jobdatamap, and the dotted line expresses a jobdatamap that indicates a task of due status: A scheduler can have multiple trigger and multiple jobdetail, which can be divided into different groups. When registering trigger and Jobdetail, if the specified group is not displayed, scheduler is placed in the default group, and the group name for the default group is Scheduler.default_group. The group name and name form the full name of the object. The full name of the same type object (that is, job or trigger) cannot be the same. The scheduler itself is a container that maintains the various components of the quartz and implements the rules of dispatch. Scheduler also has a thread pool, which provides execution threads for tasks-----This is more efficient than simply creating a new thread when performing a task, while reducing resource consumption through a shared mechanism. With the support of thread pool component, quartz can provide good scalability for task scheduling with high busy and heavy pressure.
Note: The Quartz full download package has more than 10 instances in the examples directory, which is a good example of quickly mastering quartz applications.



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.