1膀跌、概要
Spring Batch是一個(gè)開源的批量處理框架遭商,Spring Batch提供了類和API來讀寫資源,管理事務(wù)捅伤,作業(yè)處理統(tǒng)計(jì)劫流、重啟、以及分區(qū)技術(shù)處理大量數(shù)據(jù)丛忆。在Spring Batch中祠汇,一個(gè)作業(yè)任務(wù)可以由多個(gè)step組成,每個(gè)任務(wù)又都可以分為Read-Process-Write或者是tasklet
- 對(duì)于“Read-Process-Write”過程熄诡,它是指從資源(csv可很、xml或數(shù)據(jù)庫(kù))中“讀取”數(shù)據(jù),“處理”它并“寫入”它到其他資源(csv凰浮、xml和數(shù)據(jù)庫(kù))我抠。例如,步驟可以從CSV文件中讀取數(shù)據(jù)袜茧,對(duì)其進(jìn)行處理并將其寫入數(shù)據(jù)庫(kù)菜拓。Spring Batch提供了許多定制類來讀/寫CSV、XML和數(shù)據(jù)庫(kù)笛厦。
- 對(duì)于“單個(gè)”操作任務(wù)(tasklet)尘惧,它意味著只執(zhí)行單個(gè)任務(wù),比如在步驟啟動(dòng)或完成之后或之前清理資源递递。
- 這些步驟可以鏈接在一起作為作業(yè)運(yùn)行喷橙。
2、項(xiàng)目依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
3登舞、編寫一個(gè)簡(jiǎn)單的Tasklet
public class MessageTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
String message = (String) chunkContext.getStepContext().getJobParameters().get("message");
ExecutionContext jobContext = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext();
jobContext.put("message", message);
//打印傳入的參數(shù)
System.out.println(message);
return RepeatStatus.FINISHED;
}
}
4贰逾、Job配置
@Configuration
public class TaskletJobConfiguration {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
public Job taskletJob() {
return this.jobs.get("taskletJob")
.start(step())
.build();
}
@Bean
protected Step step() {
return steps
.get("step")
.tasklet(messageTasklet())
.build();
}
@Bean
public MessageTasklet messageTasklet() {
MessageTasklet tasklet = new MessageTasklet();
return tasklet;
}
}
5、參數(shù)配置
#初始化Spring Batch 數(shù)據(jù)表
spring.batch.initialize-schema=always
#工程啟動(dòng)時(shí)不執(zhí)行任務(wù)
spring.batch.job.enabled=false
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.pool-name=HikariPool
#最大連接數(shù)菠秒,小于等于0會(huì)被重置為默認(rèn)值10疙剑;大于零小于1會(huì)被重置為minimum-idle的值
spring.datasource.hikari.maximum-pool-size=10
#連接超時(shí)時(shí)間:毫秒,小于250毫秒践叠,否則被重置為默認(rèn)值30秒
spring.datasource.hikari.connection-timeout=60000
#最小空閑連接言缤,默認(rèn)值10,小于0或大于maximum-pool-size禁灼,都會(huì)重置為maximum-pool-size
spring.datasource.hikari.minimum-idle=10
#空閑連接超時(shí)時(shí)間管挟,默認(rèn)值600000(10分鐘),大于等于max-lifetime且max-lifetime>0弄捕,會(huì)被重置為0僻孝;不等于0且小于10秒导帝,會(huì)被重置為10秒。
# 只有空閑連接數(shù)大于最大連接數(shù)且空閑時(shí)間超過該值穿铆,才會(huì)被釋放
spring.datasource.hikari.idle-timeout=500000
#連接最大存活時(shí)間.不等于0且小于30秒蟋软,會(huì)被重置為默認(rèn)值30分鐘.設(shè)置應(yīng)該比mysql設(shè)置的超時(shí)時(shí)間短
spring.datasource.hikari.max-lifetime=540000
#連接測(cè)試查詢
spring.datasource.hikari.connection-test-query=SELECT 1
6褂萧、接口測(cè)試
@RestController
@Slf4j
public class JobLauncherController {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
@RequestMapping("/launchjob")
public String handle() throws Exception {
String parameter = UUID.randomUUID().toString();
try {
//接口每次都重新生成一個(gè)UUID惨撇,如果參數(shù)完全相同创坞,日志會(huì)提示任務(wù)已經(jīng)執(zhí)行成功,不能重復(fù)執(zhí)行
JobParameters jobParameters = new JobParametersBuilder().addString("message", "Welcome To Spring Batch World!" + parameter)
.toJobParameters();
jobLauncher.run(job, jobParameters);
} catch (Exception e) {
log.error("", e);
}
return parameter;
}
}
最后不要忘記在啟動(dòng)類上加上注解@EnableBatchProcessing
7凤优、Job攔截器
@Component
public class InterceptingJobExecution implements JobExecutionListener {
@Override
public void beforeJob(JobExecution jobExecution) {
System.out.println("Intercepting Job Execution - Before Job!");
}
@Override
public void afterJob(JobExecution jobExecution) {
System.out.println("Intercepting Job Execution - after Job!");
}
}
實(shí)現(xiàn)JobExecutionListener
接口后在Job配置的地方增加一下listener即可悦陋,如下:
@Autowired
InterceptingJobExecution interceptingJobExecution;
@Bean
public Job taskletJob() {
return this.jobs.get("taskletJob")
.start(step()).listener(interceptingJobExecution)
.build();
}
8、源碼
https://github.com/cattles/fucking-great-springbatch
9别洪、參考文檔
1、[Spring Batch Sample]: https://github.com/spring-projects/spring-batch/tree/master/spring-batch-samples "Spring Batch Sample"
2柳刮、[Spring Boot Reference]: https://docs.spring.io/spring-batch/docs/4.2.x/reference/html/index.html "Spring Boot Reference"
來吧~飛不飛的再說