Spring boot 整合 Quartz (小白专场)

Posted white-123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring boot 整合 Quartz (小白专场)相关的知识,希望对你有一定的参考价值。

0.项目结构(含主函数):
技术图片

1.依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xinyu.test</groupId>
    <artifactId>Test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
            <version>RELEASE</version>
            <exclusions>
               <exclusion>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-logging</artifactId>
               </exclusion>
            </exclusions>

        </dependency>

    </dependencies>

</project> 

2.定时器配置代码:

package com.quartz;


import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;



@Configuration
@Component
public class QuartzConfig {


    @Bean(name = "quartzTaskBean")
    public MethodInvokingJobDetailFactoryBean chargingFeedBackBean(QuartzTask task){
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        // 是否并发
        jobDetail.setConcurrent(false);
        // 任务的名字
        jobDetail.setName("quartzTask");
        // 任务的分组
        jobDetail.setGroup("feedBack");
        // 被执行的对象
        jobDetail.setTargetObject(task);
        // 被执行的方法
        jobDetail.setTargetMethod("doTask");
        return jobDetail;
    }


    /**
     //     * 表达式触发器工厂Bean
     //     */
    @Bean(name = "quartzTaskTrigger")
    public CronTriggerFactoryBean jobTestTaskTrigger(@Qualifier("quartzTaskBean") MethodInvokingJobDetailFactoryBean quartzTaskBean) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(quartzTaskBean.getObject());
        // 什么是否触发,Spring Scheduler Cron表达式,每秒一次
        trigger.setCronExpression("0/1 * * * * ?");
        trigger.setName("quartzTaskTrigger");
        return trigger;
    }






    /**
     //     * 调度器工厂Bean
     //     */
    @Bean(name = "schedulerFactory")
//    public SchedulerFactoryBean schedulerFactory(@Qualifier("agvErrorReportTrigger") Trigger agvErrorReportTrigger,
//                                                 @Qualifier("agvChargingReportTrigger")Trigger agvChargingReportTrigger) {
    public SchedulerFactoryBean schedulerFactory(@Qualifier("quartzTaskTrigger") Trigger quartzTaskTrigger) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        // 覆盖已存在的任务
        bean.setOverwriteExistingJobs(false);
        //项目启动完成后,等待5秒后开始执行调度器初始化
        bean.setOverwriteExistingJobs(true);
        // 延时启动定时任务,避免系统未完全启动却开始执行定时任务的情况
        bean.setStartupDelay(10);
        // 判断定时任务是否开器
        List<Trigger> triggers = new ArrayList<>();

        triggers.add(quartzTaskTrigger);

        Trigger[] ts = triggers.toArray(new Trigger[0]);
        // 注册触发器
        bean.setTriggers(ts);
        return bean;
    }

}

3.业务代码:

package com.quartz;

import org.springframework.stereotype.Component;

@Component
public class QuartzTask {



    public void doTask() {
        System.out.println("成功调用");
    }
}

4.执行结果:

技术图片

以上是关于Spring boot 整合 Quartz (小白专场)的主要内容,如果未能解决你的问题,请参考以下文章

spring boot整合quartz定时任务案例

Spring Boot 整合 Quartz 实现定时任务

Spring Boot 整合Quartz定时器

SpringBoot之整合Quartz调度框架-基于Spring Boot2.0.2版本

SpringBoot之整合Quartz调度框架-基于Spring Boot2.0.2版本

Spring Boot整合Scheduled定时任务器整合Quartz定时任务框架