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

一、引言

定时任务调度是Java开发中不可或缺的重要部分,但是Java自带的Time等任务调度类在实际项目中不好用。所以Quartz和Spring Task就成了我们项目开发技术选型最多的,在这里我们着重探讨一下Quartz在Spring Boot 2.X版本中的使用。

二、Quartz

1. 介绍

Quartz是OpenSymphony开源组织在Job scheduling领域的开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的日程序表。Jobs可以做成标准的Java组件或 EJBs。
Quartz是一个任务日程管理系统,一个在预先确定(被纳入日程)的时间到达时,负责执行(或者通知)其他软件组件的系统。
Quartz用一个小Java库发布文件(.jar文件),这个库文件包含了所有Quartz核心功能。这些功能的主要接口(API)是Scheduler接口。它提供了简单的操作,例如:将任务纳入日程或者从日程中取消,开始/停止/暂停日程进度。

2.安装配置

官方网站:官网

3.设计架构

  1. 介绍
    • Scheduler – 核心调度器
    • Job – 任务
    • JobDetail – 任务描述
    • Trigger – 触发器
  2. 图示
    在这里插入图片描述

多个定时的图示如下:
在这里插入图片描述
对比下配置xml中的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 
        Spring整合Quartz进行配置遵循下面的步骤:
        1:定义工作任务的Job
        2:定义触发器Trigger,并将触发器与工作任务绑定
        3:定义调度器,并将Trigger注册到Scheduler
     -->
    <!-- 1:定义任务的bean ,这里使用JobDetailFactoryBean,也可以使用MethodInvokingJobDetailFactoryBean ,配置类似-->
    <bean name="hwJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <!-- 指定job的名称 -->
        <property name="name" value="hw_job"/>
        <!-- 指定job的分组 -->
        <property name="group" value="hw_group"/>
        <!-- 指定具体的job类 -->
        <property name="jobClass" value="com.dufy.spring.quartz.chapter01.job.HelloWorldJob"/>
        <!-- 必须设置为true,如果为false,当没有活动的触发器与之关联时会在调度器中会删除该任务  -->
        <property name="durability" value="true"/>
        <!-- 指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的 -->
        <property name="applicationContextJobDataKey" value="applicationContext"/>
    </bean>
    <!-- 2.1:定义触发器的bean,定义一个Simple的Trigger,一个触发器只能和一个任务进行绑定 -->
    <!-- <bean name="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        指定Trigger的名称
        <property name="name" value="hw_trigger"/>
        指定Trigger的名称
        <property name="group" value="hw_trigger_group"/>
        指定Tirgger绑定的Job
        <property name="jobDetail" ref="hwJob"/>
        指定Trigger的延迟时间 1s后运行
        <property name="startDelay" value="1000"/>
        指定Trigger的重复间隔  5s
        <property name="repeatInterval" value="5000"/>
        指定Trigger的重复次数
        <property name="repeatCount" value="5"/>
    </bean> -->

    <!-- 2.2:定义触发器的bean,定义一个Cron的Trigger,一个触发器只能和一个任务进行绑定 -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- 指定Trigger的名称 -->
        <property name="name" value="hw_trigger"/>
        <!-- 指定Trigger的名称 -->
        <property name="group" value="hw_trigger_group"/>
        <!-- 指定Tirgger绑定的Job -->
        <property name="jobDetail" ref="hwJob"/>
        <!-- 指定Cron 的表达式 ,当前是每隔1s运行一次 -->
        <property name="cronExpression" value="0/1 * * * * ?" />
    </bean>


    <!-- 3.定义调度器,并将Trigger注册到调度器中 -->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <!-- <ref bean="simpleTrigger"/> -->
                <ref bean="cronTrigger"/>
            </list>
        </property>
        <!-- <property name="autoStartup" value="true" /> -->
    </bean>

</beans>

三、整合

1.项目基础

项目是基于Spring Boot2.x版本的。
在这里插入图片描述

2.添加依赖

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

3.yml配置

在这里插入图片描述
application-quartz.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
    #初始化表结构
    #jdbc:
      #initialize-schema: never

4.创建任务测试类

  • 简单任务
    在这里插入图片描述
    代码如下:
public class MyJob extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("start My Job:" + LocalDateTime.now());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end  My Job:" + LocalDateTime.now());

    }
}
  • CRON任务
    在这里插入图片描述
public class MyCronJob extends QuartzJobBean {

    @Autowired
    IndexController indexController;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("任务执行了" + new Date());
        // indexController.testMail();
    }
}

5.Java配置(QuartzConfiguration)

@Configuration
public class QuartzConfiguration {

	// 使用jobDetail包装job
    @Bean
    public JobDetail myJobDetail() {
        return JobBuilder.newJob(MyJob.class).withIdentity("myJob").storeDurably().build();
    }

	// 把jobDetail注册到trigger上去
    @Bean
    public Trigger myJobTrigger() {
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(15).repeatForever();

        return TriggerBuilder.newTrigger()
                .forJob(myJobDetail())
                .withIdentity("myJobTrigger")
                .withSchedule(scheduleBuilder)
                .build();
    }

	// 使用jobDetail包装job
    @Bean
    public JobDetail myCronJobDetail() {
        return JobBuilder.newJob(MyCronJob.class).withIdentity("myCronJob").storeDurably().build();
    }

	// 把jobDetail注册到Cron表达式的trigger上去
    @Bean
    public Trigger CronJobTrigger() {
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");

        return TriggerBuilder.newTrigger()
                .forJob(myCronJobDetail())
                .withIdentity("myCronJobTrigger")
                .withSchedule(cronScheduleBuilder)
                .build();
    }
}

其实上面的配置就等价于在传统的xml中配置bean是一样的。

6.启动测试

在这里插入图片描述
至此,SpringBoot集成Quartz的完毕。

四、总结

  • Spring Boot集成quartz还是比较简单的。
  • 其实还有更高级的用法,就是前台动态创建和控制定时任务,后面有时间再完善。大家先把这种最简单的基本用法熟练掌握。
    在这里插入图片描述

在这里插入图片描述

Github地址:源码

### 整合Spring BootQuartz实现数据库持久化的作业调度 #### 配置依赖项 为了使Spring Boot应用程序能够支持Quartz并利用`quartz-jobs`库来完成数据库中的作业存储,需先调整项目的构建文件。对于Maven项目,在`pom.xml`中加入如下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> <!-- Quartz Jobs --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>${quartz.version}</version> </dependency> ``` 上述配置引入了必要的组件用于启动和管理定时任务以及通过JDBC连接到关系型数据库保存job详情[^1]。 #### 数据源设置 接着定义数据源属性以便于Quartz可以访问外部数据库执行持久操作。编辑`application.properties`或`application.yml`: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/quartz_db?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=password spring.jpa.hibernate.ddl-auto=update ``` 这里指定了MySQL作为后台存储介质;当然也可以选择其他类型的RDBMS只要相应修改URL即可满足需求[^4]。 #### 初始化表结构 为了让Quartz能在指定的数据源上创建所需的表格,应该下载官方提供的SQL脚本并将之应用于目标schema内。通常这些DDL语句位于Quartz发行版下的`docs/dbTables`目录下找到适合版本的建表语句应用至实际环境中[^2]。 #### 自定义Job类 编写具体的业务逻辑单元即所谓的Jobs, 它们代表待被执行的任务体。下面给出一个简单的例子展示怎样继承自`Job`接口从而形成可被调度器识别的对象形式: ```java import org.quartz.Job; import org.quartz.JobExecutionContext; public class SampleJob implements Job { @Override public void execute(JobExecutionContext context){ System.out.println("Executing sample job at " + new Date()); } } ``` 此段代码实现了基本的日志记录功能每当触发条件达成时就会打印当前时间戳信息给控制台输出流[^3]。 #### 调度策略设定 最后一步就是安排何时何地运行已注册好的jobs了。这可以通过编程方式动态添加trigger实例或者静态声明的方式预先确定好计划参数。以下是采用Java Config风格的例子说明如何做到这一点: ```java @Configuration @EnableScheduling public class SchedulerConfig { @Autowired private ApplicationContext applicationContext; @Bean public JobDetailFactoryBean jobDetail(){ JobDetailFactoryBean factory = new JobDetailFactoryBean(); factory.setJobClass(SampleJob.class); factory.setDescription("Invoke SampleJob"); return factory; } @Bean public CronTriggerFactoryBean cronTrigger() throws ParseException{ CronTriggerFactoryBean trigger = new CronTriggerFactoryBean(); trigger.setCronExpression("*/5 * * * * ?"); //每五秒一次 trigger.setJobDetail(jobDetail().getObject()); return trigger; } } ``` 这段配置片段设置了每隔五分钟就激活一次之前提到过的SampleJob示例程序。
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值