Spring Boot entry (IX): Integrated Quartz regular tasks Spring Boot entry (VIII): Getting basic integration of RabbitMQ message queue using the Quartz (version 2.3.0)

The film article continued, " the Spring Getting the Boot (VIII): Integrated RabbitMQ message queue " on Quartz regular tasks refer to " Getting Started with the basic use of the Quartz (2.3.0 version) "

spring boot achieve regular tasks, in addition to integrated Quartz, the annotation scheduler may be used directly. Using a simple annotation can be done, why it is more complex integration Quartz? Here, I simply answered difference in the way these 2 under, which is why I choose Quartz project in this way.

1.scheduler annotation mode, once the timer task abnormal, then the timer task will not start again, which means that timing task becomes ineffective, and Quartz will not.

2.scheduler annotation mode, the current face of regular tasks not completed when the regular tasks can not be opened again, indicating that the scheduler annotation mode is single-threaded, multi-threaded and Quartz, the same timing tasks can be processed concurrently.

3.scheduler annotation mode, the timing of the format rarely, only simple configuration in the comment, many complex timing could not complete the task, and the format of Quartz very rich, you can configure a variety of timed tasks.

For these reasons, the timing task should be selected Quartz.

 

1. increased reliance

 1 <!--quartz-->
 2         <dependency>
 3             <groupId>org.quartz-scheduler</groupId>
 4             <artifactId>quartz</artifactId>
 5             <version>2.2.1</version>
 6         </dependency>
 7         <!-- 该依赖必加,里面有sping对schedule的支持 -->
 8         <dependency>
 9             <groupId>org.springframework</groupId>
10             <artifactId>spring-context-support</artifactId>
11         </dependency>

2. Increase conf

. 1  Package 2  
. 3  Import org.quartz *. ;
 . 4  Import org.springframework.beans.factory.annotation.Autowired;
 . 5  Import org.springframework.stereotype.Service;
 . 6  
. 7  
. 8  / ** 
. 9  * @program: 
 10  * @description : Timing task creation job, task operates by injecting Scheduler
 . 11  * @author : the DZ
 12 is  * @Create: 2019-10-19 18:28
 13 is  * * / 
14  @Service
 15  public  class QuartzConf {
 16      Private  static  FinalJOB_GROUP = String "job_group" ;
 . 17      Private  static  Final String TRIGGER_GROUP = "trigger_group" ;
 18 is      @Autowired
 . 19      Private Scheduler Scheduler;
 20 is  
21 is      / ** 
22 is       * create a scheduled task
 23       * corresponding task definition (JobDetial) and trigger (Trigger ), and added to an execution schedule (Scheduler) and starts the agenda by the listener.
24       * @param jobDetailName
 25       * @param cronExpression
 26 is       * @param jobClass
 27       * @throws SchedulerException
 28      */
29     public void createScheduleJob(String jobDetailName, String cronExpression, Class<? extends Job> jobClass) throws SchedulerException {
30         JobDetail jobDetail = JobBuilder.newJob(jobClass)
31                 .withIdentity("task_" + jobDetailName, JOB_GROUP).storeDurably().requestRecovery().build();
32         CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
33         Trigger trigger = TriggerBuilder.newTrigger().withIdentity("task_" + jobDetailName, TRIGGER_GROUP).withSchedule(scheduleBuilder).build();
34         scheduler.scheduleJob(jobDetail, trigger);
35     }
36 }

3. Add filter

 6 import lombok.extern.slf4j.Slf4j;
 7 import org.quartz.SchedulerException;
 8 import org.slf4j.MDC;
 9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.context.ApplicationListener;
11 import org.springframework.context.annotation.Configuration;
12 import org.springframework.context.event.ContextRefreshedEvent;
13 
14 import java.util.UUID;
15 
16 /**
17  * @program: 
18  * @description: 启动监听去初始化Quartz
. 19  * @author : the DZ
 20 is  * @Create: 2019-10-19 18:32
 21 is  * * / 
22 is  @ SLF4J
 23 is  @Configuration
 24  public  class ApplicationStartQuartzJobListener the implements the ApplicationListener <ContextRefreshedEvent> {
 25      @Autowired
 26 is      Private QuartzConf quartzConf;
 27  
28     / * time format:
 29      * * * * * * *
 30      [sec] [min] [h] [day] [month] [weeks] [Y] * / 
31 is  
32      / ** 
33       * initial start-quartz
34 is       * / 
35      @Override
 36      public  void onApplicationEvent (ContextRefreshedEvent ContextRefreshedEvent) {
 37 [          the try {
 38 is              log.info ( "Task started ..." );
 39            40              quartzConf.createScheduleJob ( "TestTask", "* / 30 * * * ? *. ", TestTask class );
 41 is              log.info (" task has started ... " );
 42 is          } the catch (SchedulerException E) {
 43 is              log.error (" timer task failed to start " , E);
 44 is          }
 45      }
 46 }

If the entire project is a timed task, in fact, you do not need to filter directly to the class name is written in the cong regular tasks can be. If there are a plurality of timing tasks, defining a plurality of trigger and the job may be. Such relatively high redundancy codes

The effect of this is that the listener: When the project started, the listener will register all the regular tasks to schedule, the equivalent of opening timing tasks. So you do only need to define a trigger job and you can write multiple timing tasks.

4. Write a scheduled task

 1  2 
 3  4 import lombok.extern.slf4j.Slf4j;
 5 import org.quartz.Job;
 6 import org.quartz.JobExecutionContext;
 7 import org.quartz.JobExecutionException;
 8 import org.quartz.JobKey;
 9 import org.slf4j.MDC;
10 
11 import java.util.UUID;
12 
13 /**
14  * @program:
15  * @description: 测试定时任务
16  * @author: DZ
17  * @create: 2019-10-19 18:49
18  **/
19 @Slf4j
20 public class TestTask implements Job {
21     @Override
22     public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
23        25         JobKey key = jobExecutionContext.getJobDetail().getKey();
26         // todo 业务逻辑
27         log.info("----------" + key + "任务执行中,currentTime:" + DateUtil.getCurrentDate());
28     }
29 }

 In addition to achieving this location Job interface, you can also inherit QuartzJobBean class, in fact QuartzJobBean is a subclass of Job, but some parameters initialized

Guess you like

Origin www.cnblogs.com/dz-boss/p/11729505.html