1. 程式人生 > >Spring Boot筆記之定時任務(Quartz)

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支援資料庫儲存還不知帶怎麼搞,以後再寫。