Spring Boot笔记之定时任务(Quartz)

本文介绍如何在SpringBoot2.x项目中使用spring-boot-starter-quartz进行定时任务的配置与执行。通过示例代码展示了JobBean的创建、配置触发器以及cron表达式的应用。
摘要由CSDN通过智能技术生成

摘要

Spring Boot2.x支持spring-boot-starter-quartz,本文介绍spring-boot-starter-quartz配置及使用。

pom引入

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

定时任务业务(JobBean)

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class FooJob extends QuartzJobBean {
    private static Logger logger = LoggerFactory.getLogger(FooJob.class);

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("==> fooJob star");
        try {
            Thread.sleep(1500L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("<== fooJob end");
    }
}

给JobBean配置触发器

import com.my.demo.quartz.job.AbrJob;
import com.my.demo.quartz.job.FooJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail fooJobDetail() {
        return JobBuilder.newJob(FooJob.class).withIdentity("fooJob").storeDurably().build();
    }

    @Bean
    public Trigger fooJobTrigger() {
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("*/1 * * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(fooJobDetail())
                .withIdentity("fooJob")
                .withSchedule(scheduleBuilder)
                .build();
    }

    @Bean
    public JobDetail abrJobDetail() {
        return JobBuilder.newJob(AbrJob.class).withIdentity("abrJob").storeDurably().build();
    }

    @Bean
    public Trigger abrJobTrigger() {
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("*/1 * * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(abrJobDetail())
                .withIdentity("abrJob")
                .withSchedule(scheduleBuilder)
                .build();
    }
}

一些废话

一共写了两个JobBean,fooJobabrJob用来演示,Quartz支持线程池,项目启动日志:

  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

日志显示Quartz使用了SimpleThreadPool,线程池长度为10。

cron表达式都是*/1 * * * * ?,每秒触发一次,job内用Thread.sleep(1500L)模拟业务代码执行消耗的1500ms,所以job下次执行前本次还未结束

控制台录屏

从控制台可以看出两个job执行时不会因为触发器的频繁出发而排队,理论上如果只有一个job的话,这个job允许的执行时间为cron表达式推算的时间间距乘以线程池长度

线程池长度使用spring.quartz.properties.org.quartz.threadPool.threadCount在application.properties中配置

spring.quartz.properties.org.quartz.threadPool.threadCount=11

完整的配置:

spring.quartz.properties.org.quartz.scheduler.instanceName
spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO
spring.quartz.properties.org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
spring.quartz.properties.org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
spring.quartz.properties.org.quartz.jobStore.tablePrefix=config_quartz_
spring.quartz.properties.org.quartz.jobStore.isClustered=true
spring.quartz.properties.org.quartz.jobStore.clusterCheckinInterval=10000
spring.quartz.properties.org.quartz.jobStore.useProperties=false
spring.quartz.properties.org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
spring.quartz.properties.org.quartz.threadPool.threadCount=10
spring.quartz.properties.org.quartz.threadPool.threadPriority=5
spring.quartz.properties.org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
spring.quartz.job-store-type=jdbc

job-store-type支持数据库存储还不知带怎么搞,以后再写。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值