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

摘要

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支持数据库存储还不知带怎么搞,以后再写。

猜你喜欢

转载自blog.csdn.net/yimcarson/article/details/84241457