Developing enterprise-level task scheduling applications based on Quartz (1)

Source: Internet
Author: User

Introduction:Quartz is an open-source project of OpenSymphony open-source organization in the task scheduling field. It is fully implemented based on Java. As an excellent open-source scheduling framework, Quartz features powerful functions, flexible applications, and easy integration. This article analyzes the basic implementation principles inside the Quartz framework, and describes the basic methods for developing applications using Quartz through some specific examples, common Problems and Solutions in enterprise applications are discussed.

Basic concepts and principles of Quartz

Quartz Scheduler open-source framework

Quartz is an open-source project of OpenSymphony open-source organization in the task scheduling field. It is fully implemented based on Java. This project was acquired by Terracotta in 2009 and is currently a project under Terracotta. You can download the Quartz release and source code at I used version 1.8.4 in product development. Therefore, this article is based on this version. This article not only describes how to use Quartz for development, but also explains its internal implementation principles.

As an excellent open-source scheduling framework, Quartz has the following features:

Powerful scheduling functions, such as supporting a wide variety of scheduling methods, can meet various general and special needs;

Flexible application methods, such as multiple combinations of tasks and scheduling, and multiple storage methods for scheduling data;

Distributed and cluster capabilities. After Terracotta was acquired, it further improved its functionality. This article does not discuss this part for the moment.

In addition, as the default scheduling framework of Spring, Quartz can easily integrate with Spring to implement flexible and configurable scheduling functions.

The following are some special words used in this article:

Scheduler:Task SchedulerTrigger:Trigger, used to define task scheduling time rulesJob:A task is a scheduled task.Misfire:Missed task scheduling that is supposed to be executed but not actually executed

Basic implementation principle of Quartz Task Scheduling

Core elements

The core elements of Quartz task scheduling are schedgger, trigger, and job. trigger and job are the metadata of task scheduling, and scheduler is the Controller that actually executes the scheduling.

In Quartz, trigger is an element used to define the scheduling time, that is, the time rule to execute the task. Quartz mainly provides four types of triggers: SimpleTrigger, CronTirgger, DateIntervalTrigger, and nth1_deddaytrigger. These four triggers can meet the vast majority of enterprise applications. We will further discuss the functions of four triggers in the Enterprise Application Section.

In Quartz, a job is used to represent a scheduled task. There are two main types of jobs: stateless) and stateful ). For the same trigger, a stateful job cannot be executed in parallel. The next trigger can be triggered only after the last trigger task is executed. A Job has two main attributes: volatility and durability. volatility indicates whether the task is persisted to the database storage, and durability indicates whether the task is retained when no trigger is associated. Both are persistent or retained when the value is true. A job can be associated with multiple triggers, but one trigger can be associated with only one job.

In Quartz, schedfactory is created by the scheduler Factory: DirectSchedulerFactory or StdSchedulerFactory. The second type of factory StdSchedulerFactory is widely used, because DirectSchedulerFactory is not convenient enough to use, and many detailed manual encoding settings are required. There are three types of Scheduler: RemoteMBeanScheduler, RemoteScheduler and StdScheduler. This article uses the most common StdScheduler as an example. This is also the scheduler class used by the author in the project.

Shows the relationship between the core elements of Quartz:

Figure 1. Quartz core element Relationship Diagram


Thread View

In Quartz, there are two types of threads: Scheduler scheduling thread and task execution thread. The task execution thread usually uses a thread pool to maintain a group of threads.

Figure 2. Quartz thread View


Scheduler scheduling threads include the threads that execute regular scheduling and the threads that execute misfired trigger. The regular scheduling thread polls all the stored triggers. If a trigger needs to be triggered, that is, the next trigger time is reached, an idle thread is obtained from the task execution thread pool, executes the task associated with the trigger. The Misfire thread scans all the triggers to check whether there are any misfired triggers. If yes, it will be handled separately according to the misfire policy. Describes the basic processes of these two threads:

Figure 3. Quartz scheduling thread Flowchart


We will further describe misfired trigger in the Enterprise Application Section.

Related Article

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