Quartz springboot

SpringBoot整合任务调度框架Quartz及持久化配置

小亿
224
2024-01-08 17:00:45
栏目: 编程语言

Spring Boot提供了对Quartz任务调度框架的整合支持。Quartz是一个功能强大的任务调度框架,可以用于定时执行任务、计划任务等。

要整合Quartz,首先需要添加Quartz的依赖。在pom.xml文件中添加以下依赖:

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

接下来,创建一个Quartz的配置类,用于配置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;
    }
}

在上面的配置类中,我们创建了两个JobDetail和两个CronTrigger,并将它们添加到SchedulerFactoryBean中。这样,当应用启动时,Quartz会根据配置的触发器和任务来执行定时任务。

最后,创建两个Job类,用于处理具体的任务逻辑。

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 {
        // 任务逻辑
    }
}

这样,就完成了Spring Boot整合Quartz任务调度框架的配置。

如果需要将任务调度的配置进行持久化,可以使用Quartz提供的JobStore来实现。常用的JobStore有以下几种:

要使用JDBCJobStore进行持久化配置,可以添加以下依赖:

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

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

接着,在Quartz的配置类中添加以下代码:

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

@Bean
public DataSource dataSource()

0
看了该问题的人还看了