Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

1. Introduction of Quartz

1.1 Basic Introduction

Quartz is a Java-based Job Scheduling framework that allows you to define timed, delayed, and periodic tasks as needed.

1.2 Official Documents

Quartz official website

You can download Quartz, view the documentation, and code for the Quartz open source project.

When you unzip the downloaded Quartz zip file, you will find the Quartz documentation, sample files, and related JAR packages in it, making it very easy to learn.

2. Procedure

There are three steps to using Quartz:

  1. Defining a task Job
  2. Define a time policy Trigger
  3. Define Scheduler Scheduler to implement scheduled task execution

2.1 Importing Dependencies

Quartz related dependency information

<! Quartz core pack -->
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.3.2</version>
</dependency>
<! - tools - >
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz-jobs</artifactId>
    <version>2.3.2</version>
</dependency>

<! --SpringBoot -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
Copy the code

2.3 Defining a task class

Job comes from the org.Quartz.Job package, and you need to override execute(…) in the Job interface. Method, which contains the task content to be executed.

public class DoTaskByQuartz implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("Task execution, current time:"+ System.currentTimeMillis()); }}Copy the code

After the definition of the task class is completed, the task class needs to be passed as a parameter to generate the JobDetail object for actual use

JobDetail = jobBuilder.newJob (DoTaskByQuartz. Class).withidentity ("jobName").build();Copy the code

2.3 Defining time Policies (Triggers)

Trigger objects are used as task triggers in the Quartz framework to configure time policies for scheduled tasks by creating Trigger objects.

Trigger objects can be created in two ways:

  • SimpleTrigger, created using SimpleScheduleBuilder, sets the cycle time
  • CronTrigger, created using CronScheduleBuilder, sets time rules through cron expressions
// Set the trigger name. ② Setting the trigger takes effect immediately; ③ Set the task trigger period; ④ Create Trigger objects
Trigger trigger = TriggerBuilder.newTrigger()
    .withIdentity("triggerName")
    .startNow()             .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(2))
    .build();
Copy the code

2.4 Defining the task scheduler

The factory class is used when the scheduler is defined to create the default scheduler object and complete the assembly of scheduled tasks and triggers.

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    scheduler.scheduleJob(jobDetail,trigger);
    scheduler.start();
Copy the code

2.5 Complete use process

//main starts the class. The task class has been defined
public static void main(String[] args) throws SchedulerException {
    JobDetail jobDetail = JobBuilder.newJob(DoTaskByQuartz.class)
        .withIdentity("jobName")
        .build();

    Trigger trigger = TriggerBuilder.newTrigger()
        .withIdentity("triggerName")
        .startNow()
        .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(2))
        .build();

    Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    scheduler.scheduleJob(jobDetail,trigger);
    scheduler.start();
}
Copy the code

3. Process summary

  1. Define the task class implementationJobInterface, and rewrite theexecute()Method that defines the task content
  2. createJobDetailClass, and bind the task class that needs to be executed to get the task contentJob
  3. createTriggerClass object, setSimpleTriggerorCronTriggerType of trigger that is responsible for notifying the trigger at a specified timeSchedulerTo execute the specifiedJob
  4. SchedulerType object creation, bindingJobDetailThe objects andTriggerObject, throughTriggerObject to executeJobDetailCorresponding in objectJobTask.