(1) What is quartz?
(2) Characteristics of quartz;
(3) Quartz special Vocabulary description;
(4) The basic realization principle of quartz task scheduling;
Next look at the specific content:
(1) What is quartz?
Quartz is an open-source job scheduling framework written entirely by Java, providing a simple yet powerful mechanism for job scheduling in Java applications. Quartz allows developers to schedule jobs based on time intervals. It implements a many-to-many relationship between jobs and triggers, and can also associate multiple jobs with different triggers. Simply create a Java class with a Org.quarz.Job interface.
(2) Characteristics of quartz;
As an excellent open-source scheduling framework, Quartz has the following features:
① powerful scheduling functions, such as supporting a wide range 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 storage methods;
③ distributed and cluster capabilities, Terracotta acquisition after the original function on the basis of a further improvement.
The ④quartz is easy to integrate with Spring to enable flexible and configurable scheduling capabilities.
(3) Quartz special Vocabulary description;
Here are some of the special terms used in this article, which declares:
Scheduler :
Task Scheduler
Trigger :
triggers, which define task scheduling time rules
Job :
Tasks, which are scheduled tasks
Misfire :
A task scheduled to be executed but not actually executed.
(4) The basic realization principle of quartz task scheduling;
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 used to define the scheduling time, which is the time rule to perform the task. There are four types of Trigger:simpletrigger,crontirgger,dateintervaltrigger, and Nthincludeddaytrigger, which are mainly available in Quartz. These four types of trigger can meet most of the needs of enterprise applications.
In Quartz, the job is used to represent the task being dispatched. There are two main types of job: stateless (stateless) and stateful (stateful). For the same trigger, a stateful job cannot be executed in parallel, and the next execution can be triggered only after the last triggered task has been executed. There are two main properties of the job: volatility and durability, where volatility indicates whether the task is persisted to the database store, and durability indicates whether the task is persisted without a 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 factory stdschedulerfactory used more, because the directschedulerfactory is not easy to use, need to make a lot of detailed manual coding settings. There are three main types of Scheduler: Remotembeanscheduler, Remotescheduler and Stdscheduler. This article takes the most commonly used stdscheduler as an example to explain. This is also the scheduler class used by the author in the project.
In this article, we follow the previous article on how quartz is used in Java project projects, where we use MAVEN to build projects. First look at the outline of this chapter:
(1) New project Quartz-java;
(2) configuration Pom.xml file;
(3) Coding instructions;
(4) Writing job classes;
(5) Writing the startup class for code testing;
(6) Quartz.properties configuration file description;
Take a look at the specific content:
(1) New project Quartz-java;
Create a new Java project named Quartz-java.
(2) configuration Pom.xml file;
Add the quartz dependency in the Pom.xml file:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
(3) Coding instructions;
(a) First we need to define a task class, such as Hellojob, which needs to inherit the job class and then add the Execute (Jobexecutioncontext context) method, where our specific task is executed.
(b) Where is the definition of "when to perform what tasks?" ": Then we need scheduler, the way this class was created using the factory class Stdschedulerfactory.getdefaultscheduler () provided by Quartz.
(c) How to trigger: Scheduler.schedulejob (Jobdetail,trigger), to trigger a timed task, where two parameters are required. Jobdetail can be created through Jobbuilder.newjob, where a job class needs to be developed, that is, the Hellojob;trigger class that we created in the first step, can be created by Triggerbuilder.newtrigger.
(4) Writing job classes;
To write the Hellojob task class:
Package com.kfit.job;
import java.util.Date;
import Org.quartz.Job;
import Org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* Task class.
* @author Angel--Guardian Angel
* @version v.0.1
* @date April 21, 2017
*/
Public class Hellojob implements job{
Public void Execute (jobexecutioncontext context) throws jobexecutionexception {
The task that executes the response.
System. out. println ("Hellojob.execute," +new Date ());
}
}
(5) Writing the startup class for code testing;
To test the code in the Main method:
Package com.kfit;
import java.util.concurrent.TimeUnit;
import Org.quartz.JobBuilder;
import Org.quartz.JobDetail;
import Org.quartz.Scheduler;
import org.quartz.SchedulerException;
import Org.quartz.SimpleScheduleBuilder;
import Org.quartz.Trigger;
import Org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import Com.kfit.job.HelloJob;
/**
* Start the test directly in the main method.
* @author Angel--Guardian Angel
* @version v.0.1
* @date April 21, 2017
*/
Public class App {
Public Static void Main (string[] args) throws Schedulerexception, interruptedexception {
/*
* In Quartz, Scheduler is created by the Scheduler Factory: Directschedulerfactory or Stdschedulerfactory. The second kind of factory stdschedulerfactory use more,
* Because Directschedulerfactory is not easy to use, many detailed manual coding settings are required.
*/
Get Scheduler Instance
Scheduler Scheduler = stdschedulerfactory. Getdefaultscheduler ();
Scheduler.start ();
System. out. println ("Scheduler.start");
Specific tasks.
Jobdetail Jobdetail = Jobbuilder. Newjob (Hellojob. class). Withidentity ("Job1", "group1"). Build ();
Trigger point in time. (Executes 1 times every 5 seconds.)
Simpleschedulebuilder Simpleschedulebuilder = Simpleschedulebuilder. Simpleschedule (). Withintervalinseconds (5). RepeatForever ();
Trigger Trigger = Triggerbuilder. Newtrigger (). Withidentity ("Trigger1", "group1"). Startnow (). Withschedule (Simpleschedulebuilder). build ();
Sent to scheduler to arrange the trigger
Scheduler.schedulejob (Jobdetail,trigger);
Sleep for 20 seconds.
Timeunit. SECONDS. Sleep (20);
Scheduler.shutdown ();//Close timed Task Scheduler.
System. out. println ("Scheduler.shutdown");
}
}
To execute code to view the print information for the console:
-----------------------------------------------------------
Scheduler.start
Hellojob.execute,fri APR 19:50:01 CST 2017
Hellojob.execute,fri APR 19:50:06 CST 2017
Hellojob.execute,fri APR 19:50:11 CST 2017
Hellojob.execute,fri APR 19:50:16 CST 2017
Hellojob.execute,fri APR 19:50:21 CST 2017
Scheduler.shutdown
Springboot Simple example of integrating quartz timed tasks 2