Spring Boot整合Quartz

1 Quartz简介

Quartz 是一个完全由Java 编写的开源任务调度的框架,通过触发器设置作业定时运行规 则,控制作业的运行时间。Quartz 定时器作用很多,比如,定时发送信息和定时生成报表等。 Quartz 框架主要核心组件包括调度器、触发器和作业。调度器作为作业的总指挥,触发器 作为作业的操作者,作业为应用的功能模块。其关系如图:

2 Spring Boot整合Quartz步骤

2.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.yiidian</groupId>
    <artifactId>ch03_11_springboot_quartz</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 导入springboot父工程. 注意:任何的SpringBoot工程都必须有的!!! -->
    <!-- 父工程的作用:锁定起步的依赖的版本号,并没有真正到依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.11.RELEASE</version>
    </parent>

    <dependencies>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--springboot 集成 junit 起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.1.6.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <!-- sping对schedule的支持 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <!--Quartz运行必须依赖到spring-tx包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <!-- Quartz支持 -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

</project>

2.2 编写Job任务类

package com.yiidian.jobs;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Quartz的任务类
 * 一点教程网 - www.yiidian.com
 */
public class MyJob {

    /**
     * 任务方法
     */
    public void task(){
        System.out.println("任务被触发,当前时间为:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}

2.3 编写Quartz配置类

package com.yiidian;

import com.yiidian.jobs.MyJob;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * Quartz配置类
 * 一点教程网 - www.yiidian.com
 */
@Configuration
public class QuartzConfig {

    @Bean
    public MyJob createJob(){
        return new MyJob();
    }

    /**
     * 创建任务
     */
    @Bean
    public MethodInvokingJobDetailFactoryBean jobDetailFactoryBean(MyJob job){
        MethodInvokingJobDetailFactoryBean factoryBean = new MethodInvokingJobDetailFactoryBean();
        //设置任务对象
        factoryBean.setTargetObject(job);
        //设置任务方法
        factoryBean.setTargetMethod("task");
        return factoryBean;
    }

    /**
     * 触发器
     */
    @Bean
    public CronTriggerFactoryBean createTrigger(MethodInvokingJobDetailFactoryBean jobDetailFactoryBean){
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        triggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        triggerFactoryBean.setCronExpression("0/5 * * * * ? *");
        return triggerFactoryBean;
    }

    /**
     * 创建Schduler
     */
    @Bean
    public SchedulerFactoryBean getSchedulerFactoryBean(CronTriggerFactoryBean triggerFactoryBean){
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        //关联trigger
        factory.setTriggers(triggerFactoryBean.getObject());
        return factory;
    }
}

2.4 编写引导类

package com.yiidian;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;

/**
 * Spring Boot引导类
 * 一点教程网 - www.yiidian.com
 */
@SpringBootApplication
public class MyBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyBootApplication.class,args);
    }

}

2.5 运行测试

启动引导类,定时任务自动运行。效果是每隔5秒种向控制台输出内容:

 

源码下载:https://pan.baidu.com/s/1bAW3DCJduKurJ8-89JowFw

热门文章

优秀文章