Spring boot and achieve the task timed reminder Quartz

Our product is a code-free business processes to build tools (product name light flow), our customers often put forward such a demand:

"We want to make a schedule to control, to review each node plus the time limit, because the task on time if a node does not handle, then a timeout it!"

"Time is a limited task more important things, you can not do a light stream function, when approaching time-limited task, remind the person in charge of this task?"

......

Visibility, timeliness task is a very important thing, how we should achieve "a certain time to go node to remind people," this goal?

We know, Spring Boot comes with a scheduling function can be achieved with regular tasks @Schedule notes, but this method can only achieve a fixed time schedule. The user need is custom timing task start time. Importantly, if the system is restarted, then the original memory timing task will be released! This is a serious problem.

This time, Quartz This tool comes in handy.

Quartz is a good open source scheduling framework (thanks to the contribution of the open-source organization), it has many advantages, such as:

(1) powerful support rich scheduling method, and supports thousands of timed tasks simultaneously scheduling;

(2) support the mission continues, do not worry about the system after a reboot, "forget" before scheduling tasks;

(3) support distributed, you can set up multiple executor to perform the task scheduling, load balancing and improve reliability (Linkin open source scheduling tool Azkaban is implemented based on quartz).

Quartz Core Concepts

Quartz has several core concepts are:

Scheduler: Task Scheduler

Trigger: Trigger, which defines the rules of the scheduled time

Job: task scheduler schedules tasks i.e.

Key: When including the name (name) and group (group), we will trigger job and registered with the scheduler, you need to assign them the key.

Quartz core concept of the relationship as shown:

Spring boot and achieve the task timed reminder Quartz

(Source: IBM)

Work process components:

(1) Scheduler instance is created using SchedulerFactory

SchedulerFactory factory = new SchedulerFactory();
Scheduler scheduler = factory.getScheduler();
(2)创建一个任务

Job = JobBuilder.newJob the JobDetail (CustomJob.class) .withIndentity ( "myjob", "myGroup") Build ();.
CustomJob timed task class is a custom.

(3) create a trigger, specify at what time the task execution

The Trigger = TriggerBuilder.newTrigger.withIndentity the Trigger ( "myTrigger", "myGroup")
.startAt (targerDate) .build ()
where, TargetDate is a java.util.Date type, is used to specify at what time to perform the task.

(4) Sign up to the task scheduler

scheduler.scheduleJob (job, trigger)
after the task scheduling up, the task will be executed at a specified time friends.

In Spring Boot in

Earlier we just introduced the Quartz basic workflow, then how to use Spring Boot in it? How do the regular tasks of sustained?

In Spring Boot version 2.0, has been integrated Quartz 2.3.0 version, Spring Boot has been able to automate the configuration of Quartz, eliminating a lot of manual operations, reduce development pressures programmers

(1) Gradle file in an existing project in the Spring Boot introduced quartz-starter dependent

the compile ( 'org.springframework.boot: Spring-Boot-Starter-quartz')
(2) specified in the quartz sql statement corresponding to the item in the data source mysql, required to create a table (see end of text codes)

(3) adding quartz is disposed in application.properties

= of the type-Store-spring.quartz.job jdbc
spring.quartz.jdbc.initialize-Schema = Never
addition, we do not write other configurations, Spring Boot automatically configured for us.

After this configuration, all quartz tasks are automatically persisted to the mysql in, will not be lost.

(4) task scheduling code

// where we needed to use direct injection Scheduler Scheduler, Spring Boot has been configured

Scheduler instance

@Autowire
Private Scheduler Scheduler;
// task scheduling
String JobKey = UUID.randomUUID () toString ();.
String triggerKey = UUID.randomUUID () toString ();.
JobDetail jobDetail = JobBuilder.newJob (CustomNotifyTask.class)
.withIdentity ( JobKey, CustomTask.class.getName ())
.build ();
the Trigger Trigger = TriggerBuilder.newTrigger ()
.withIdentity (triggerKey, CustomTask.class.getName ())
.startAt (targetdate) .build ();
// start scheduling
scheduler.scheduleJob (jobDetail, Trigger);
the CustomTask task is a class that inherits its executeInternal QuartzJobBean and implements the interface.

the extends the CustomTask QuartzJobBean {class public br /> @ Override
protected void executeInternal (the JobExecutionContext context) {
System.out.println ( "Hello World");
}
}
Thus, we demonstrate the use of a single continuous Quartz node scheduler.

Bonus: pass parameters to the regular tasks
in general, need to receive regular task parameters, so as to achieve rich functionality. For example, we want to send a message to the mailbox custom timing task, then the e-mail address is passed in the parameter.

In the framework of Quartz, passing parameters to the timer task is very convenient.

When scheduling tasks, passing parameters myNumber.

JobDetail = JobBuilder.newJob the JobDetail (CustomNotifyTask.class)
.withIdentity (JobKey, CustomTask.class.getName ())
.usingJobData ( "myNum", the myNumber)
.usingJobData ( "In Email", In Email)
.build ();
performing tasks when obtaining parameters myNumber.

@Override
protected void executeInternal(JobExecutionContext context) {
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
Integer applyId = dataMap.getInt("mynum");
String email = dataMap.getInt("email");
// .......
}

Extended: Quartz cluster

Although individual Quartz scheduler instance (standalone version) allows us to be very good for task scheduling, but can not meet the increasingly complex requirements of enterprise applications, such as reliability, availability and scalability. If we multi-task scheduling, Quartz cluster is then to be considered. When a crash Quartz instance, another instance of the cluster can quickly take over its work to ensure the implementation of Job.

Quartz cluster configuration is more or less complicated, if interested in it, may be the venue of more features such as https://medium.com/@Hronom/spring-boot-quartz-scheduler-in-cluster-mode-457f4535104d,Quartz for you to explore!

Guess you like

Origin blog.51cto.com/14502444/2429860