SpringBatch是什么?
Spring Batch 是一个轻量级的、完善的批处理框架,旨在帮助企业建立健壮、高效的批处理应用。Spring Batch是Spring的一个子项目,使用Java语言并基于Spring框架为基础开发,使得已经使用Spring框架的开发者更容易访问和利用企业服务。
Spring Batch 提供了大量可重复的组件,包括了日志、追踪、事务、任务作业统计、任务重启、跳过、重复、资源管理。对于大数据量和高性能的批处理任务,Spring Batch 同样提供了高级性能和特性来支持,比如分区功能、远程功能。总之,通过Spring Batch能够支持简单的、复杂的和大数据量的批处理作业。
Spring Batch是一个批处理应用框架,不是调度框架,但需要和调度框架合作构建完成的批处理任务。它只关注处理任务的相关问题,如事务、并发、监控等,并不提供相关的调度功能。如果需要使用调度框架,在商业软件和开源软件中已经有很多的企业级的调度框架(如Quartz) 可以使用。
框架主要有以下功能:
Transaction management 事务管理
Chunk based processing 基于块的处理
Declarative I/O 声明式的输入输出
Start/Stop/Restart 启动/停止/重启
Retry/Skip 重试/跳过
框架一共有4个主要角色:
JobLauncher 任务启动器,通过它来启动任务,可以看作是程序的入口。
Job 代表一个具体的任务
Step 代表着一个具体的步骤,一个Job可以包含多个Step。
JobRepository 是存储数据的地方,可以看作是一个数据库的接口,在任务执行的时候需要通过它来记录任务状态等信息。
版本说明
使用的springboot版本2.7.3
新建Springboot模块,不需要选择任何模块(不需要web-starter)
导入依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-batchartifactId>
dependency>
<dependency>
<groupId>org.springframework.batchgroupId>
<artifactId>spring-batch-testartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
启动项目,可以看到启动报错,提示我们可以需要配置一个内嵌数据库(如H2,HSQL和Derby),也可以选择一个外部的数据库。
根据上面的报错,我们引入依赖h2数据库,并配置数据库的信息
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
dependency>
spring:
h2:
console:
path: /h2
enabled: true #线上运行时需要设置为false,避免暴露公网
H2相关内容见:springboot基础(29):内存数据库H2
编写入门程序,我们知道一个Job可以对于n个Step,那么下面我们编写一个demo测试
package com.it2.springbootspringbatch01.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
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.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
@Slf4j
public class JobConfiguration {
//注入任务对象工厂
@Autowired
private JobBuilderFactory jobBuilderFactory;
//任务的执行由Step决定,注入step对象的factory
@Autowired
private StepBuilderFactory stepBuilderFactory;
//创建Job对象
@Bean
public Job jobDemo(){
return jobBuilderFactory.get("jobDemo").start(step1()).build();
}
//创建Step对象
@Bean
public Step step1() {
// return stepBuilderFactory.get("step1").tasklet();//方式一 ,使用tasklet();
// 方式二,stepBuilderFactory.get("step1").chunk()
return stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
log.info("------step1 ok");
return RepeatStatus.FINISHED;//决定step是否结束,否则会重试
}
}).build();
}
}
启动服务器主类,可以看到SpringBatch的Job任务被执行。