A,QuartzWhat was it?!

A lot of too professional words I don’t say first! Why! Because it’s so professional, I can’t see myself! Too professional words also need to copy from the Internet, why! I will write down as much as I can in my own understanding. Speaking of this framework, it’s designed to perform timed tasks anyway. What timed tasks? Such as:

You want to go to the canteen every day at 12:00 noon

The above statement can be broken down into three parts:

  • 1. 12:00 noon every day(Trigger)
  • 2, have a meal(Job)
  • 3. Your brain makes you do it(Scheduler)

The three points above illustrate the three most critical components of the Quartz framework: triggers, timed tasks, and schedulers. It’s easy to understand. A trigger tells you when a timed task should be executed! The scheduler is used to coordinate the execution of each scheduled task from a macro perspective!

Quartz uses the builder design pattern in many places, such as Trigger has a TriggerBuilder and JobDetail has a JobBuilder. Knowing that the Builder pattern is used, we can create objects like this:

JobDetail job = JobBuilder.newJob(yourJobClass).withIdentity(jobName, jobGroupName).build();
Copy the code

In Quartz, each task has its own task name and group name, each task name and group name are unique, if specified not unique, error!

The factory method design pattern is used to create Scheduler in Quartz, which shows how common design patterns are in frameworks.

Properties is used as a configuration file in the classpath by default. However, you can also create a file with the same name in your classpath to override it. For example, I created a file with the same name in the Resource directory of the Spring Boot project to override it.

File attribute configuration can refer to the official website of the document, said very detailed! Click quartz configuration for details

Quartz supports the Date type and cron expression for Trigger times. Spring also provides cron expressions for timed tasks.

Spring Boot integration Quartz

Here’s how Quartz works in the Spring Boot app, which may differ from many tutorials on the web.

1. First, we generally require that only one Scheduler instance be configured globally, and we want to call the start() method of the Scheduler instance when Spring Boot is started, so we only need to dynamically drop tasks into the instance or remove tasks from the instance. Create a class StartSchedualer somewhere in the project:

@component // Public class StartSchedualer implements CommandLineRunner {// Implements the interface @override public void run(String... args) throws Exception { try { GetSchedual.getSchedual().start(); } catch (SchedulerException e) {}}}Copy the code

How do I dynamically join tasks?

        @RequestMapping("/add")
	public static void addJob() throws SchedulerException {
		JobDetail job = JobBuilder.newJob(SimpleJob.class).withIdentity("job1"."group1").setJobData(null).build();
		CronTrigger trigger = newTrigger().withIdentity("trigger1"."group1")
				.withSchedule(cronSchedule("0/2 * * * *?")).build(); // A task is executed every 2 seconds Date ft = GetSchedual.getscheDual ().scheduleJob(job, trigger); // Register tasks with Scheduler}Copy the code

How do I delete a task?

        @RequestMapping("/remove"Public void remove(@requestParam String triggerName, @requestParam String triggerGroupName, @requestParam String triggerGroupName, @RequestParam String jobName, @RequestParam String jobGroupName) { try { Schedulersched= GetSchedual.getSchedual(); // Get the only object (singleton!) TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); // Delete sched.pauseTrigger(triggerKey); // Stop trigger sched.unscheduleJob(triggerKey); Jobkey.deletejob (jobkey.jobkey (jobName, jobGroupName)); } catch (Exception e) {throw new RuntimeException(e); }}Copy the code

GetSchedual.java

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class GetSchedual {

	private static SchedulerFactory factory = new StdSchedulerFactory();

	public static Scheduler getSchedual() throws SchedulerException {
		Scheduler schedual = factory.getScheduler();
		returnschedual; }}Copy the code

pom.xml

<! Scheduler </groupId> <artifactId>quartz</artifactId> The < version > 2.2.1 < / version > < / dependency >Copy the code

Finally, Quartz also supports clustering. For details, read the documentation on the official website. You can download the source code from the official website. Although it is not difficult to read the source code, but xiaobian also have to learn in depth!

The above is my Quartz and Spring Boot integration chestnut, probably not done well! Welcome to point out any problem, exchange and progress together!