【jeecg-boot项目开发crm】:平台技术点——day05【Java定时任务解决方案:十、springboot整合quartz】:图灵课堂

十、springboot整合quartz

  • 前面的是原生的quartz,为了让了解原理。
  • quartz整合到springboot上就可以轻量级(就不用new很多的调度器实例)

1 准备相应的集群和一些配置文件

  • springboot中的节点是不通信的,是通过数据库来记录集群的信息,不同的集群来访问同一个数据库。

1.1 quartz官方提供了一张表,导入即可

  • 资源在博主中心

1.2 设置springboot的配置文件

application.properties

server.context-path=/quartz-cluster2
#\u7AEF\u53E3\u53F7
server.port=8082
# \u6570\u636E\u6E90\u914D\u7F6E
spring.datasource.url=jdbc:mysql://localhost:3306/tuling?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-active=1000
spring.datasource.max-idle=20
spring.datasource.min-idle=5
spring.datasource.initial-size=10

1.3 设置quartz本身的配置文件

spring-quartz.properties

#============================================================================
# 配置JobStore
#============================================================================
# JobDataMaps是否都为String类型,默认false
org.quartz.jobStore.useProperties=false

# 表的前缀,默认QRTZ_
org.quartz.jobStore.tablePrefix = QRTZ_

# 是否加入集群
org.quartz.jobStore.isClustered = true

# 调度实例失效的检查时间间隔 ms
org.quartz.jobStore.clusterCheckinInterval = 5000

# 当设置为“true”时,此属性告诉Quartz 在非托管JDBC连接上调用setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED)。
org.quartz.jobStore.txIsolationLevelReadCommitted = true

# 数据保存方式为数据库持久化
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX

# 数据库代理类,一般org.quartz.impl.jdbcjobstore.StdJDBCDelegate可以满足大部分数据库
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate

#============================================================================
# Scheduler 调度器属性配置
#============================================================================
# 调度标识名 集群中每一个实例都必须使用相同的名称
org.quartz.scheduler.instanceName = ClusterQuartz
# ID设置为自动获取 每一个必须不同
org.quartz.scheduler.instanceId= AUTO

#============================================================================
# 配置ThreadPool
#============================================================================
# 线程池的实现类(一般使用SimpleThreadPool即可满足几乎所有用户的需求)
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool

# 指定线程数,一般设置为1-100直接的整数,根据系统资源配置
org.quartz.threadPool.threadCount = 5

# 设置线程的优先级(可以是Thread.MIN_PRIORITY(即1)和Thread.MAX_PRIORITY(这是10)之间的任何int 。默认值为Thread.NORM_PRIORITY(5)。)
org.quartz.threadPool.threadPriority = 5

2 创建任务类

2.1 创建QuartzJob类

  1. 继承springboot封装好的QuartzJobBean或者实现Job接口
  • 这里我们用【继承springboot封装好的QuartzJobBean】
package com.tuling.bootquartz;

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

import java.util.Date;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class QuartzJob extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
            Thread.sleep(2000);
            System.out.println(context.getScheduler().getSchedulerInstanceId());

            System.out.println("taskname="+context.getJobDetail().getKey().getName());
            System.out.println("执行时间="+new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3 定义调度器实例

  • 由于调度器每次用的都是一个,所以通过IOC,将调度器配置成一个Bean

3.1 创建SchedulerConfig配置类

  1. 加@configuration注解
    • @configuration用于定义配置类
  2. 创建出Scheduler对象,用来配置,这里配置了一个名字

在这里插入图片描述
3. 注入数据源(因为调度器之间的节点不通信),通过数据库来保持这些节点的通信。

在这里插入图片描述
4. 读配置文件,设置线程池

在这里插入图片描述

  • 通过一个方法来读取properties来返回一个properties
    • 通过PropertiesFactoryBean来获取
      • setLocation:把配置文件的路径给过去
      • 调取afterPropertiesSet方法:才能读取文件

在这里插入图片描述
5. 线程池配置
在这里插入图片描述

  • 也是通过定义一个@Bean的方式,将线程池定义出来
    在这里插入图片描述
  1. 设置调度器延迟执行还是立即执行
    在这里插入图片描述
  2. 就可以调用schedulerFactoryBean()这个方法来获取Scheduler实例了。
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂野小白兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值