Integration of SpringBoot with task scheduling framework Quartz and persistent configuration.

Spring Boot offers integration support for the Quartz job scheduling framework. Quartz is a powerful job scheduling framework that can be used for tasks such as executing jobs on a schedule.

To integrate Quartz, you need to first add the dependency for Quartz. Add the following dependency in the pom.xml file:

<!-- Quartz -->
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
</dependency>

Next, create a Quartz configuration class to configure the properties and task scheduling of Quartz.

@Configuration
public class QuartzConfig {

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        
        // 设置触发器
        schedulerFactoryBean.setTriggers(trigger1().getObject(), trigger2().getObject());
        
        // 设置任务
        schedulerFactoryBean.setJobDetails(jobDetail1().getObject(), jobDetail2().getObject());
        
        // 设置自动启动
        schedulerFactoryBean.setAutoStartup(true);
        
        return schedulerFactoryBean;
    }
    
    @Bean
    public JobDetailFactoryBean jobDetail1() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(MyJob1.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }
    
    @Bean
    public JobDetailFactoryBean jobDetail2() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(MyJob2.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }
    
    @Bean
    public CronTriggerFactoryBean trigger1() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(jobDetail1().getObject());
        cronTriggerFactoryBean.setCronExpression("0/5 * * * * ?");
        return cronTriggerFactoryBean;
    }
    
    @Bean
    public CronTriggerFactoryBean trigger2() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(jobDetail2().getObject());
        cronTriggerFactoryBean.setCronExpression("0/10 * * * * ?");
        return cronTriggerFactoryBean;
    }
}

In the configuration class above, we have created two JobDetails and two CronTriggers, and added them to the SchedulerFactoryBean. This way, when the application starts, Quartz will execute scheduled tasks based on the configured triggers and jobs.

Finally, create two Job classes to handle the specific task logic.

public class MyJob1 implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // 任务逻辑
    }
}

public class MyJob2 implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // 任务逻辑
    }
}

This completes the configuration of integrating the Quartz task scheduling framework with Spring Boot.

To persist the configuration of task scheduling, you can utilize JobStore provided by Quartz. Some commonly used JobStore options include:

  1. RAMJobStore: Save the configuration of job scheduling in memory, data will be lost after the application is closed.
  2. JDBCJobStore: Saves the configuration of job scheduling in a database, ensuring the data persists even after the application is closed.
  3. JobStoreCMT: Saving task scheduling configuration in the database and supporting container-managed transactions.

To configure persistence using JDBCJobStore, you can add the following dependency.

<!-- Quartz JDBC -->
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz-jdbc</artifactId>
</dependency>

<!-- H2 Database -->
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>

Next, add the following code in the Quartz configuration class:

@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
    // ...
    
    // 设置持久化配置
    schedulerFactoryBean.setDataSource(dataSource());
    schedulerFactoryBean.setJobFactory(jobFactory());
    
    return schedulerFactoryBean;
}

@Bean
public DataSource dataSource()
Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds