SpringBoot整合Spring Batch示例代碼
文檔
Spring Batch - Reference Documentation
基礎(chǔ)概念
- JobLauncher:作業(yè)啟動(dòng)器,啟動(dòng)作業(yè)的入口。對(duì)應(yīng)的實(shí)現(xiàn)類為SimpleJobLauncher。
- Job:作業(yè),用于配置作業(yè)的相關(guān)配置,一個(gè)作業(yè)可以配置多個(gè)步驟,步驟之間是有序的。
- Step:步驟,作業(yè)具體執(zhí)行的業(yè)務(wù)邏輯,一個(gè)Job可以配置多個(gè)Step。步驟有兩種實(shí)現(xiàn)方式:
- Tasklet方式:所有作業(yè)邏輯寫在一個(gè)方法中。
- Chunk方式:將一個(gè)完整的作業(yè)邏輯根據(jù)作用拆分到三個(gè)方法中
- ItemReader:負(fù)責(zé)從數(shù)據(jù)源中讀數(shù)據(jù)(如從文件、數(shù)據(jù)庫等)。
- ItemProcessor :負(fù)責(zé)對(duì)讀出來的數(shù)據(jù)進(jìn)行非法校驗(yàn)和對(duì)數(shù)據(jù)進(jìn)行加工。
- ItemWriter:將數(shù)據(jù)寫到某個(gè)目標(biāo)中(如文件、數(shù)據(jù)庫等)。
- JobBuilderFactory:作業(yè)構(gòu)建起工廠,用于構(gòu)建作業(yè)Job對(duì)象。
- get(String name):設(shè)置作業(yè)名稱。
- start(Step step):設(shè)置作業(yè)啟動(dòng)的第一個(gè)步驟。
- build():構(gòu)建Job對(duì)象。
- StepBuilderFactory:作業(yè)構(gòu)建器工廠,用于構(gòu)造步驟Step對(duì)象。
- get(String name):設(shè)置步驟名稱。
- tasklet(Tasklet tasklet):設(shè)置Tasklet。
- build():構(gòu)建Step對(duì)象。
- JobRepository:作業(yè)持久化,在執(zhí)行作業(yè)的過程中用于操作spring batch相關(guān)的表,記錄作業(yè)的相關(guān)狀態(tài)等。
Tasklet方式示例
運(yùn)行環(huán)境
$ java -version java version "1.8.0_361" Java(TM) SE Runtime Environment (build 1.8.0_361-b09) Java HotSpot(TM) 64-Bit Server VM (build 25.361-b09, mixed mode)
spring-batch依賴
<!-- spring-batch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-test</artifactId> <scope>test</scope> </dependency> <!-- h2、mysql... --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency>
備注:第一次照著網(wǎng)上的文章寫代碼運(yùn)行,直接報(bào)錯(cuò),原因是缺少引入的依賴;需要引入數(shù)據(jù)庫驅(qū)動(dòng),如H2或mysql
項(xiàng)目結(jié)構(gòu)
$ tree . ├── pom.xml └── src └── main ├── java │ └── com │ └── example │ └── demo │ ├── HelloWorldApplication.java │ ├── config │ │ └── BatchConfig.java │ └── task │ └── HelloWorldTasklet.java └── resources
完整依賴pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <mybatis-plus.version>3.5.2</mybatis-plus.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- spring-batch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <!-- test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
編寫業(yè)務(wù)邏輯
package com.example.demo.task; 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; import org.springframework.stereotype.Component; /** * 任務(wù) */ @Component public class HelloWorldTasklet implements Tasklet { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { System.out.println("Hello, World!"); return RepeatStatus.FINISHED; } }
配置作業(yè)步驟
package com.example.demo.config; import com.example.demo.task.HelloWorldTasklet; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Spring Batch配置 */ @Configuration public class BatchConfig { @Autowired private JobBuilderFactory jobBuilderFactory; @Autowired private StepBuilderFactory stepBuilderFactory; @Autowired private HelloWorldTasklet helloWorldTasklet; @Bean public Step step() { return this.stepBuilderFactory.get("step") .tasklet(helloWorldTasklet) .build(); } @Bean public Job job(Step step) { return this.jobBuilderFactory.get("job") .start(step) .build(); } }
啟動(dòng)類增加注解:@EnableBatchProcessing
package com.example.demo; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableBatchProcessing @SpringBootApplication public class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); } }
Chunk方式示例
項(xiàng)目結(jié)構(gòu)
$ tree . ├── pom.xml └── src └── main ├── java │ └── com │ └── example │ └── demo │ ├── Application.java │ ├── config │ │ └── BatchConfig.java │ ├── dto │ │ └── Person.java │ └── processor │ └── PersonItemProcessor.java └── resources └── persons.csv
依賴pom.xml 同上
package com.example.demo.dto; import lombok.Data; @Data public class Person { private String name; private Integer age; }
package com.example.demo.config; import com.example.demo.dto.Person; import com.example.demo.processor.PersonItemProcessor; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.item.file.FlatFileItemReader; import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; import org.springframework.batch.item.json.JacksonJsonObjectMarshaller; import org.springframework.batch.item.json.JsonFileItemWriter; import org.springframework.batch.item.json.builder.JsonFileItemWriterBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; /** * Spring Batch配置 */ @Configuration public class BatchConfig { @Autowired private JobBuilderFactory jobBuilderFactory; @Autowired private StepBuilderFactory stepBuilderFactory; @Bean public Step step() { return this.stepBuilderFactory.get("step") .<Person, Person>chunk(10) .reader(reader()) .processor(processor()) .writer(writer()) .build(); } @Bean public Job job(Step step) { return this.jobBuilderFactory.get("job") .start(step) .build(); } /** * 讀取csv文件 * @return */ @Bean public FlatFileItemReader<Person> reader() { return new FlatFileItemReaderBuilder<Person>() .name("personItemReader") .resource(new ClassPathResource("persons.csv")) .delimited() .names(new String[] {"name", "age"}) .targetType(Person.class) .build(); } /** * 處理器 * @return */ @Bean public PersonItemProcessor processor() { return new PersonItemProcessor(); } /** * 寫到j(luò)son文件 * @return */ @Bean public JsonFileItemWriter<Person> writer() { return new JsonFileItemWriterBuilder<Person>() .jsonObjectMarshaller(new JacksonJsonObjectMarshaller<>()) .resource(new FileSystemResource("persons.json")) .name("personItemWriter") .build(); } }
package com.example.demo.processor; import com.example.demo.dto.Person; import org.springframework.batch.item.ItemProcessor; public class PersonItemProcessor implements ItemProcessor<Person, Person> { @Override public Person process(Person person) throws Exception { // 為每個(gè)對(duì)象年齡+1 person.setAge(person.getAge() + 1); return person; } }
啟動(dòng)入口
package com.example.demo; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableBatchProcessing @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
啟動(dòng)SpringBoot即可運(yùn)行任務(wù)
讀取的persons.csv
文件
Tom,18 Jack,20
生成的persons.json
文件
[ {"name":"Tom","age":19}, {"name":"Jack","age":21} ]
到此這篇關(guān)于SpringBoot整合Spring Batch示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot整合Spring Batch內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)獲取客戶端真實(shí)IP方法小結(jié)
本文給大家匯總介紹了2種使用java實(shí)現(xiàn)獲取客戶端真實(shí)IP的方法,主要用于獲取使用了代理訪問的來訪者的IP,有需要的小伙伴可以參考下。2016-03-03Spring Cloud Gateway重試機(jī)制的實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud Gateway重試機(jī)制的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03eclipse創(chuàng)建springboot項(xiàng)目的三種方式總結(jié)
這篇文章主要介紹了eclipse創(chuàng)建springboot項(xiàng)目的三種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07