springboot整合quartz入门(二)带数据库

  • Post author:
  • Post category:其他


在(一)中,是不需要数据库实现的quartz定时任务,如果想获取任务的一些执行消息,可以使用jdbcjobstore,quartz会自动把任务执行时相关组件的信息插入到数据库中。

1、引入maven坐标

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

2、修改application.yml中配置

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/external?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
  #定时任务配置
  quartz:
    auto-startup: true
    overwrite-existing-jobs: true
    job-store-type: jdbc
    properties: 
      org: 
        quartz: 
          #ID设置为自动获取 每一个必须不同 (所有调度器实例中是唯一的)
          scheduler: 
            instanceId: AUTO
            instanceName: clusteredScheduler
          #ThreadPool实现的类名
          threadPool: 
            class: org.quartz.simpl.SimpleThreadPool
            #ThreadPool配置线程守护进程
            makeThreadsDaemons: true
            #线程数量
            threadCount: 20
            #线程优先级
            threadPriority: 5
          #数据保存方式为持久化
          jobStore: 
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            #StdJDBCDelegate说明支持集群
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            #quartz内部表的前缀
            tablePrefix: QRTZ_
            #是否加入集群
            isClustered: true 
            #不使用配置文件
            useProperties: false 
            clusterCheckinInterval: 10000

3、创建配置类

import org.quartz.spi.JobFactory;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

@Configuration
public class SchedledConfiguration {

	//配置JobFactory
    @Bean
    public JobFactory jobFactory(ApplicationContext applicationContext) {
        AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
        jobFactory.setApplicationContext(applicationContext);
        return jobFactory;
    }
	//配置JobFactory,为quartz作业添加自动连接支持
    public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements
            ApplicationContextAware {
        private transient AutowireCapableBeanFactory beanFactory;
        @Override
        public void setApplicationContext(final ApplicationContext context) {
            beanFactory = context.getAutowireCapableBeanFactory();
        }
        @Override
        protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
            final Object job = super.createJobInstance(bundle);
            beanFactory.autowireBean(job);
            return job;
        }
    }
}

4、导入SQL文件

参考:

https://gitee.com/i_have/testQuartz1.git

,在resources目录中的quartz.sql文件

5、运行测试

启动项目,执行定时任务时,会在quartz对应表中插入对应记录



版权声明:本文为plmzaqokn11原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。