Quartz is a opensymphony open source organization in the Task scheduling field of an open source project, completely based on Java implementation. The project was acquired by Terracotta in 2009 and is currently a Terracotta project. Readers can download the quartz release and its source code to the http://www.quartz-scheduler.org/site. The author uses the version 1.8.4 in the product development, therefore this article content is based on this version. This paper not only introduces how to use Quartz to develop, but also explains its internal realization principle.
As an excellent open source scheduling framework, Quartz has the following characteristics:
Powerful scheduling functions, such as supporting a rich variety of scheduling methods, can meet a variety of conventional and special needs, flexible application methods, such as support tasks and scheduling of a variety of combinations, support scheduling data for a variety of storage methods; distributed and cluster capabilities, Terracotta After the acquisition of the original function on the basis of further improvement. This article does not discuss this part of the content
In addition, as the spring default scheduling framework, Quartz is easy to implement with spring integration with flexible configurable scheduling capabilities.
Here are some of the special words used in this article, which are stated here:
Scheduler: Task Scheduler Trigger: triggers, which define task scheduling time rule jobs: tasks, which are scheduled tasks misfire: missing, referring to tasks that should have been performed but not actually executed
Basic realization principle of Quartz task scheduling
Core elements
The core element of Quartz task scheduling is scheduler, trigger and job, where trigger and job are the metadata of task scheduling, scheduler is the controller that actually executes the dispatch.
In Quartz, trigger is the element that defines the scheduling time, that is, what time rules are used to perform tasks. The Quartz mainly provides four types of Trigger:simpletrigger,crontirgger,dateintervaltrigger, and Nthincludeddaytrigger. These four kinds of trigger can meet the most needs of enterprise application. We will further discuss the capabilities of the four trigger in the Enterprise Application section.
In Quartz, a job is used to represent a scheduled task. There are two main types of jobs: stateless (stateless) and stateful (stateful). For the same trigger, a stateful job cannot be executed in parallel, and the next execution can only be triggered after the last triggered task has been executed. The job has two main properties: volatility and durability, where volatility indicates whether the task is persisted to the database store, and durability indicates whether the task is retained when there is no trigger association. Both are persisted or persisted when the value is true. A job can be associated with multiple trigger, but a trigger can only associate one job.
In Quartz, Scheduler is created by the Scheduler Factory: Directschedulerfactory or Stdschedulerfactory. The second plant is stdschedulerfactory, because the directschedulerfactory is not easy to use and requires many detailed manual coding settings. There are mainly three kinds of Scheduler: Remotembeanscheduler, Remotescheduler and Stdscheduler. This article takes the most commonly used stdscheduler as an example to explain. This is also the author in the project uses the Scheduler class.
The relationship between the Quartz core elements is shown in the following illustration:
Figure 1. Quartz core element diagram
Threads View
In Quartz, there are two types of threads, Scheduler scheduling threads and task execution threads, where task execution threads typically maintain a set of threads using a thread pool.
Figure 2. Quartz Threads View