springboot集成quartz

导入依赖

<!--添加quartz的依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

application.yml配置文件

spring:
  quartz:
    #相关属性配置
    properties:
      org:
        quartz:
          scheduler:
            instanceName: clusteredScheduler
            instanceId: AUTO
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            tablePrefix: QRTZ_
            isClustered: true
            clusterCheckinInterval: 10000
            useProperties: false
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            threadCount: 10
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true
    #数据库方式
    job-store-type: jdbc
    #初始化表结构.初次使用的时候会在库中生成表格,后期注释掉即可.或者初始使用值为always,然后将值改为never
    #jdbc:
      #initialize-schema: always

quartz配置类

package com.theeternity.common.quartz;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: ApiBoot
 * @description: quartz配置类
 * @author: TheEternity Zhang
 * @create: 2019-03-11 11:01
 */
@Configuration
public class QuartzConfig {
    @Bean
    public JobDetail uploadTaskDetail() {
        return JobBuilder.newJob(TestQuartz.class).withIdentity("testQuartz").storeDurably().build();
    }

    @Bean
    public Trigger uploadTaskTrigger() {
        //设置执行频率
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("*/5 * * * * ?");
        return TriggerBuilder.newTrigger().forJob(uploadTaskDetail())
                .withIdentity("testQuartz")
                .withSchedule(scheduleBuilder)
                .build();
    }
}

quartz具体执行类

package com.theeternity.common.quartz;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.UUID;

/**
 * @program: ApiBoot
 * @description: 测试quartz
 * @author: TheEternity Zhang
 * @create: 2019-03-11 11:01
 * @DisallowConcurrentExecution 注解得作用是:是否并发执行,系统默认为true,即第一个任务还未执行完整,第二个任务如果到了执行时间,则会立马开启新线程执行任务,这样如果我们是从数据库读取信息,两次重复读取可能出现重复执行任务的情况,所以我们需要将这个值设置为false,这样第二个任务会往后推迟,只有在第一个任务执行完成后才会执行第二个任务。
 */
@DisallowConcurrentExecution
public class TestQuartz extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String random= UUID.randomUUID().toString();
        System.out.println("任务开始:"+random);
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("任务结束:"+random);
    }
}

参考文档:

https://www.jianshu.com/p/056281e057b3 (流程参考)
https://docs.spring.io/spring-boot/docs/2.0.x-SNAPSHOT/reference/htmlsingle/#boot-features-quartz (官方文档)
https://blog.csdn.net/tushuping/article/details/79636207 (具体代码)

posted @ 2020-01-30 17:07  未月廿三  阅读(865)  评论(0编辑  收藏  举报