学习在 Spring boot应用程序中使用 Java 配置创建 Spring batch作业(包含多个步骤)。该示例使用 H2 数据库进行持久性处理。
1. Maven 依赖项
我们需要包括spring-boot-starter-batch依赖关系。Spring batch依赖于作业存储库,该存储库是一个持久的数据存储。所以我们也需要一个数据库。我们正在使用 H2(内存数据库),它与 Spring batch很好地集成。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
2. 创建批处理作业
在此项目中,我们将创建一个包含 2 步任务的简单作业,并执行该作业以观察日志。作业执行流程将是 –
- 启动作业
- 执行任务一
- 执行任务二
- 完成作业
2.1. 添加任务集
第一步是创建一些任务,这些任务将按顺序运行以形成作业。在 Spring batch中,它们要实现Tasklet.
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
public class MyTaskOne implements Tasklet {
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
{
System.out.println("MyTaskOne start..");
// ... your code
System.out.println("MyTaskOne done..");
return RepeatStatus.FINISHED;
}
}
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
public class MyTaskTwo implements Tasklet {
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
{
System.out.println("MyTaskTwo start..");
// ... your code
System.out.println("MyTaskTwo done..");
return RepeatStatus.FINISHED;
}
}
2.2. 批量作业配置
这是定义所有与作业相关的配置及其执行逻辑的主要步骤。
@EnableBatchProcessing注释会自动配置许多有用的 bean,否则我们需要创建这些 bean。它还配置 JobBuilderFactory 和 StepBuilderFactory bean。
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.howtodoinjava.demo.tasks.MyTaskOne;
import com.howtodoinjava.demo.tasks.MyTaskTwo;
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
public Step stepOne(){
return steps.get("stepOne")
.tasklet(new MyTaskOne())
.build();
}
@Bean
public Step stepTwo(){
return steps.get("stepTwo")
.tasklet(new MyTaskTwo())
.build();
}
@Bean
public Job demoJob(){
return jobs.get("demoJob")
.incrementer(new RunIdIncrementer())
.start(stepOne())
.next(stepTwo())
.build();
}
}
3. 演示
现在,我们的简单作业'demoJob'已配置完毕,可以执行了。我们使用CommandLineRunner接口在应用程序完全启动时JobLauncher自动执行作业。
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App implements CommandLineRunner
{
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
public static void main(String[] args)
{
SpringApplication.run(App.class, args);
}
@Override
public void run(String... args) throws Exception
{
JobParameters params = new JobParametersBuilder()
.addString("JobID", String.valueOf(System.currentTimeMillis()))
.toJobParameters();
jobLauncher.run(job, params);
}
}
控制台输出日志:
o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=demoJob]] launched with
the following parameters: [{JobID=1530697766768}]
o.s.batch.core.job.SimpleStepHandler : Executing step: [stepOne]
MyTaskOne start..
MyTaskOne done..
o.s.batch.core.job.SimpleStepHandler : Executing step: [stepTwo]
MyTaskTwo start..
MyTaskTwo done..
o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=demoJob]] completed with
the following parameters: [{JobID=1530697766768}] and the following status: [COMPLETED]
4. 禁用作业的自动运行
Spring 还会自动运行配置的批处理作业。要禁用作业的自动运行,我们需要在 application.properties 文件中使用spring.batch.job.enabled属性。
spring.batch.job.enabled=false