A dynamic configurable open source project based on spring boot integrated quartz

Recently, I learned quartz, so I integrated quartz under spring boot to realize configurable, log recording and other functions. It's powerful enough to dynamically specify a method to a class.
springquartz
This article begins with Orange Silence Blog Please add this label for reprinting.

What is quartz?

Quartz is an open source job scheduling framework written entirely by Java, which provides a simple but powerful mechanism for job scheduling in Java applications.

Quatz's First Experience

  1. First you need to use his jar package to use quartz.
<!--quartz rely on-->
       <dependency>
           <groupId>org.quartz-scheduler</groupId>
           <artifactId>quartz</artifactId>
           <version>2.2.1</version>
       </dependency>
  1. Create Scheduler objects

This object is created by SchedulerFactory

SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();

  Scheduler sched = schedFact.getScheduler();
  1. Creating Job Objects

execute is a specific method of task scheduling

public class HelloJob implements Job {

   public HelloJob() {
   }

   public void execute(JobExecutionContext context)
     throws JobExecutionException
   {
     System.err.println("Hello!  HelloJob is executing.");
   }
 }
  1. Create triggers
//40s execute once
rigger trigger = newTrigger()
      .withIdentity("myTrigger", "group1")
      .startNow()
      .withSchedule(simpleSchedule()
          .withIntervalInSeconds(40)
          .repeatForever())            
      .build();

  1. test
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();

  Scheduler sched = schedFact.getScheduler();

  sched.start();

  // Classes that specify work
  JobDetail job = newJob(HelloJob.class)
      .withIdentity("myJob", "group1")
      .build();

  // Specify trigger 40s to execute once
  Trigger trigger = newTrigger()
      .withIdentity("myTrigger", "group1")
      .startNow()
      .withSchedule(simpleSchedule()
          .withIntervalInSeconds(40)
          .repeatForever())
      .build();

  // Tell the timer what the task is and what the trigger is.
  sched.scheduleJob(job, trigger);

  //start
  sched.start();

The main components of quratz

We need to understand some of Quartz's core concepts so that understanding Quartz's principles becomes easier.

  1. Job represents the specific content of a job to be performed. There is only one method in this interface, as follows:

void execute(JobExecutionContext context)

  1. JobDetail represents a specific executable scheduler. Job is the content of the executable scheduler. JobDetail also includes the schemes and Strategies of the task scheduling.

  2. Trigger represents the configuration of a scheduling parameter and when to adjust it.

  3. Scheduler represents a scheduling container in which multiple JobDetails and Triggers can be registered. When Trigger is combined with JobDetail, it can be scheduled by the Scheduler container.

What did springquartz do?

The use of quartz is very simple, but in our usual work, we need to be able to dynamically configure quartz for scheduling. So I integrated it and made it have the following functions. (In this demo I just integrated quartz very concise and clear)

  1. Dynamic Task Assignment (Classes and Methods Specific to Use)

Create a job class without a task. I can specify any class or method.

  1. Supporting concurrency and non-concurrency

Concurrent execution and non-concurrent execution are specified by configuration.

  1. Logging

Log the scheduled schedule.

Tags: Java Spring

Posted on Sat, 07 Sep 2019 04:58:21 -0700 by scanreg