Spring Boot + Spring Batch + Quartz 整合定时批量任务

 ​

 博客主页:     南来_北往

系列专栏:Spring Boot实战


前言

最近一周,被借调到其他部门,赶一个紧急需求,需求内容如下:

PC网页触发一条设备升级记录(下图),后台要定时批量设备更新。这里定时要用到Quartz,批量数据处理要用到SpringBatch,二者结合,可以完成该需求。

由于之前,没有用过SpringBatch,于是上网查了下资料,发现可参考的不是很多,于是只能去慢慢的翻看官方文档。

Spring Batch - Reference Documentation

具体实现

在你的pom.xml文件中添加以下依赖: 

<dependencies>
    <!-- Spring Boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <!-- Quartz -->
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-quartz</artifactId>
    </dependency>
</dependencies>

 在application.properties文件中添加以下配置:

spring.quartz.job-store-type=memory
spring.quartz.properties.org.quartz.scheduler.instanceName=MyScheduler
spring.quartz.properties.org.quartz.threadPool.threadCount=5

 创建一个实现Job接口的类,例如MyBatchJob

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class MyBatchJob extends QuartzJobBean {

    @Autowired
    private JobLauncher jobLauncher;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
            jobLauncher.run(myBatchJob(), new JobParametersBuilder().addString("JobID", String.valueOf(System.currentTimeMillis())).toJobParameters());
        } catch (Exception e) {
            throw new JobExecutionException(e);
        }
    }

    private Job myBatchJob() {
        // 返回你的Spring Batch Job实例
    }
}

 在你的配置类中(例如ApplicationConfig),添加一个SchedulerFactoryBean的Bean,用于配置定时任务的触发器:

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class ApplicationConfig {

    @Bean
    public JobDetail myBatchJobDetail() {
        return JobBuilder.newJob(MyBatchJob.class)
                .withIdentity("myBatchJob")
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger myBatchJobTrigger() {
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(60) // 设置任务执行间隔,例如每60秒执行一次
                .repeatForever(); // 设置任务重复执行

        return TriggerBuilder.newTrigger()
                .forJob(myBatchJobDetail())
                .withIdentity("myBatchJobTrigger")
                .withSchedule(scheduleBuilder)
                .build();
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setJobDetails(myBatchJobDetail());
        schedulerFactoryBean.setTriggers(myBatchJobTrigger());
        return schedulerFactoryBean;
    }
}

现在,你已经成功地整合了Spring Boot、Spring Batch和Quartz,实现了定时批量任务。每隔指定的时间间隔(例如60秒),MyBatchJob将会被执行一次。

  • 19
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值