├── spring-batch-hello-world ├── src │ ├── main │ │ ├── resources │ │ │ └── csv │ │ │ │ └── persons.csv │ │ └── java │ │ │ └── com │ │ │ └── codenotfound │ │ │ ├── SpringBatchApplication.java │ │ │ ├── batch │ │ │ ├── BatchConfig.java │ │ │ ├── PersonItemProcessor.java │ │ │ └── HelloWorldJobConfig.java │ │ │ └── model │ │ │ └── Person.java │ └── test │ │ ├── resources │ │ └── csv │ │ │ └── persons.csv │ │ └── java │ │ └── com │ │ └── codenotfound │ │ └── SpringBatchApplicationTests.java ├── README.md └── pom.xml ├── spring-batch-tasklet ├── src │ ├── test │ │ ├── resources │ │ │ └── csv │ │ │ │ ├── persons2.csv │ │ │ │ └── persons1.csv │ │ └── java │ │ │ └── com │ │ │ └── codenotfound │ │ │ └── SpringBatchApplicationTests.java │ └── main │ │ └── java │ │ └── com │ │ └── codenotfound │ │ ├── SpringBatchApplication.java │ │ ├── batch │ │ ├── BatchConfig.java │ │ ├── PersonItemProcessor.java │ │ ├── FileDeletingTasklet.java │ │ └── CapitalizeNamesJobConfig.java │ │ └── model │ │ └── Person.java ├── README.md └── pom.xml ├── spring-batch-scheduler ├── src │ ├── main │ │ ├── resources │ │ │ ├── application.yml │ │ │ └── csv │ │ │ │ └── persons.csv │ │ └── java │ │ │ └── com │ │ │ └── codenotfound │ │ │ ├── SpringBatchApplication.java │ │ │ ├── batch │ │ │ ├── BatchConfig.java │ │ │ ├── PersonItemProcessor.java │ │ │ ├── CapitalizeNamesJobController.java │ │ │ ├── CapitalizeNamesJobScheduler.java │ │ │ └── CapitalizeNamesJobConfig.java │ │ │ └── model │ │ │ └── Person.java │ └── test │ │ ├── resources │ │ └── csv │ │ │ └── persons.csv │ │ └── java │ │ └── com │ │ └── codenotfound │ │ └── SpringBatchApplicationTests.java ├── README.md └── pom.xml ├── spring-batch-admin ├── spring-batch-task │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ ├── application.yml │ │ │ │ └── csv │ │ │ │ │ └── persons.csv │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── codenotfound │ │ │ │ ├── SpringBatchApplication.java │ │ │ │ ├── model │ │ │ │ └── Person.java │ │ │ │ └── batch │ │ │ │ ├── PersonItemProcessor.java │ │ │ │ └── CapitalizeNamesJobConfig.java │ │ └── test │ │ │ ├── resources │ │ │ └── csv │ │ │ │ └── persons.csv │ │ │ └── java │ │ │ └── com │ │ │ └── codenotfound │ │ │ └── SpringBatchApplicationTests.java │ ├── README.md │ └── pom.xml └── spring-cloud-data-flow-server │ ├── README.md │ ├── src │ └── main │ │ └── java │ │ └── com │ │ └── codenotfound │ │ └── SpringDataFlowServerApplication.java │ └── pom.xml ├── spring-batch-capitalize-names ├── src │ ├── main │ │ ├── resources │ │ │ └── csv │ │ │ │ └── persons.csv │ │ └── java │ │ │ └── com │ │ │ └── codenotfound │ │ │ ├── SpringBatchApplication.java │ │ │ ├── batch │ │ │ ├── BatchConfig.java │ │ │ ├── PersonItemProcessor.java │ │ │ └── CapitalizeNamesJobConfig.java │ │ │ └── model │ │ │ └── Person.java │ └── test │ │ ├── resources │ │ └── csv │ │ │ └── persons.csv │ │ └── java │ │ └── com │ │ └── codenotfound │ │ └── SpringBatchApplicationTests.java ├── README.md └── pom.xml ├── spring-batch-embedded-database ├── src │ ├── main │ │ ├── resources │ │ │ ├── csv │ │ │ │ └── persons.csv │ │ │ └── application.yml │ │ └── java │ │ │ └── com │ │ │ └── codenotfound │ │ │ ├── batch │ │ │ ├── BatchConfig.java │ │ │ ├── PersonItemProcessor.java │ │ │ └── CapitalizeNamesJobConfig.java │ │ │ ├── SpringBatchApplication.java │ │ │ └── model │ │ │ └── Person.java │ └── test │ │ ├── resources │ │ └── csv │ │ │ └── persons.csv │ │ └── java │ │ └── com │ │ └── codenotfound │ │ └── SpringBatchApplicationTests.java └── pom.xml ├── .gitignore ├── spring-batch-itemreader ├── src │ ├── test │ │ ├── resources │ │ │ └── csv │ │ │ │ └── persons.csv │ │ └── java │ │ │ └── com │ │ │ └── codenotfound │ │ │ └── batch │ │ │ └── SpringBatchApplicationTests.java │ └── main │ │ └── java │ │ └── com │ │ └── codenotfound │ │ ├── batch │ │ ├── SpringBatchApplication.java │ │ └── job │ │ │ ├── PersonNameProcessor.java │ │ │ └── ConvertNamesJobConfig.java │ │ └── model │ │ └── Person.java ├── README.md └── pom.xml ├── spring-batch-itemwriter ├── src │ ├── test │ │ ├── resources │ │ │ └── csv │ │ │ │ └── persons.csv │ │ └── java │ │ │ └── com │ │ │ └── codenotfound │ │ │ └── batch │ │ │ └── SpringBatchApplicationTests.java │ └── main │ │ └── java │ │ └── com │ │ └── codenotfound │ │ ├── batch │ │ ├── SpringBatchApplication.java │ │ └── job │ │ │ ├── PersonNameProcessor.java │ │ │ └── ConvertNamesJobConfig.java │ │ └── model │ │ └── Person.java ├── README.md └── pom.xml ├── README.md ├── .travis.yml └── LICENSE /spring-batch-hello-world/src/main/resources/csv/persons.csv: -------------------------------------------------------------------------------- 1 | John, Doe 2 | Jane, Doe 3 | -------------------------------------------------------------------------------- /spring-batch-hello-world/src/test/resources/csv/persons.csv: -------------------------------------------------------------------------------- 1 | John, Doe 2 | Jane, Doe 3 | -------------------------------------------------------------------------------- /spring-batch-tasklet/src/test/resources/csv/persons2.csv: -------------------------------------------------------------------------------- 1 | Jack, Doe 2 | Jake, Doe 3 | Josh, Doe 4 | Joe, Doe 5 | -------------------------------------------------------------------------------- /spring-batch-scheduler/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | batch: 3 | job: 4 | enabled: false 5 | -------------------------------------------------------------------------------- /spring-batch-tasklet/src/test/resources/csv/persons1.csv: -------------------------------------------------------------------------------- 1 | Jessica, Doe 2 | Julia, Doe 3 | Jasmin, Doe 4 | Jennifer, Doe 5 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-batch-task/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | url: jdbc:h2:tcp://localhost:19092/mem:dataflow 4 | driverClassName: org.h2.Driver 5 | -------------------------------------------------------------------------------- /spring-batch-scheduler/src/main/resources/csv/persons.csv: -------------------------------------------------------------------------------- 1 | Jack, Doe 2 | Jake, Doe 3 | Jane, Doe 4 | Jasmin, Doe 5 | Jennifer, Doe 6 | Jessica, Doe 7 | Joe, Doe 8 | John, Doe 9 | Josh, Doe 10 | Julia, Doe 11 | -------------------------------------------------------------------------------- /spring-batch-scheduler/src/test/resources/csv/persons.csv: -------------------------------------------------------------------------------- 1 | Jack, Doe 2 | Jake, Doe 3 | Jane, Doe 4 | Jasmin, Doe 5 | Jennifer, Doe 6 | Jessica, Doe 7 | Joe, Doe 8 | John, Doe 9 | Josh, Doe 10 | Julia, Doe 11 | -------------------------------------------------------------------------------- /spring-batch-capitalize-names/src/main/resources/csv/persons.csv: -------------------------------------------------------------------------------- 1 | Jack, Doe 2 | Jake, Doe 3 | Jane, Doe 4 | Jasmin, Doe 5 | Jennifer, Doe 6 | Jessica, Doe 7 | Joe, Doe 8 | John, Doe 9 | Josh, Doe 10 | Julia, Doe 11 | -------------------------------------------------------------------------------- /spring-batch-capitalize-names/src/test/resources/csv/persons.csv: -------------------------------------------------------------------------------- 1 | Jack, Doe 2 | Jake, Doe 3 | Jane, Doe 4 | Jasmin, Doe 5 | Jennifer, Doe 6 | Jessica, Doe 7 | Joe, Doe 8 | John, Doe 9 | Josh, Doe 10 | Julia, Doe 11 | -------------------------------------------------------------------------------- /spring-batch-embedded-database/src/main/resources/csv/persons.csv: -------------------------------------------------------------------------------- 1 | Jack, Doe 2 | Jake, Doe 3 | Jane, Doe 4 | Jasmin, Doe 5 | Jennifer, Doe 6 | Jessica, Doe 7 | Joe, Doe 8 | John, Doe 9 | Josh, Doe 10 | Julia, Doe 11 | -------------------------------------------------------------------------------- /spring-batch-embedded-database/src/test/resources/csv/persons.csv: -------------------------------------------------------------------------------- 1 | Jack, Doe 2 | Jake, Doe 3 | Jane, Doe 4 | Jasmin, Doe 5 | Jennifer, Doe 6 | Jessica, Doe 7 | Joe, Doe 8 | John, Doe 9 | Josh, Doe 10 | Julia, Doe 11 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-batch-task/src/main/resources/csv/persons.csv: -------------------------------------------------------------------------------- 1 | Jack, Doe 2 | Jake, Doe 3 | Jane, Doe 4 | Jasmin, Doe 5 | Jennifer, Doe 6 | Jessica, Doe 7 | Joe, Doe 8 | John, Doe 9 | Josh, Doe 10 | Julia, Doe 11 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-batch-task/src/test/resources/csv/persons.csv: -------------------------------------------------------------------------------- 1 | Jack, Doe 2 | Jake, Doe 3 | Jane, Doe 4 | Jasmin, Doe 5 | Jennifer, Doe 6 | Jessica, Doe 7 | Joe, Doe 8 | John, Doe 9 | Josh, Doe 10 | Julia, Doe 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ##Maven## 2 | target/ 3 | pom.xml.tag 4 | pom.xml.releaseBackup 5 | pom.xml.next 6 | release.properties 7 | 8 | ##Eclipse## 9 | .classpath 10 | .project 11 | .settings/ 12 | 13 | ##Logs## 14 | *.log 15 | *.log.* 16 | -------------------------------------------------------------------------------- /spring-batch-itemreader/src/test/resources/csv/persons.csv: -------------------------------------------------------------------------------- 1 | First Name, Last Name 2 | Jack, Doe 3 | Jake, Doe 4 | Jane, Doe 5 | Jasmin, Doe 6 | Jennifer, Doe 7 | Jessica, Doe 8 | Joe, Doe 9 | John, Doe 10 | Josh, Doe 11 | Julia, Doe 12 | -------------------------------------------------------------------------------- /spring-batch-itemwriter/src/test/resources/csv/persons.csv: -------------------------------------------------------------------------------- 1 | First Name, Last Name 2 | Jack, Doe 3 | Jake, Doe 4 | Jane, Doe 5 | Jasmin, Doe 6 | Jennifer, Doe 7 | Jessica, Doe 8 | Joe, Doe 9 | John, Doe 10 | Josh, Doe 11 | Julia, Doe 12 | -------------------------------------------------------------------------------- /spring-batch-embedded-database/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | url: jdbc:h2:mem:testdb 4 | driverClassName: org.h2.Driver 5 | username: sa 6 | password: 7 | h2: 8 | console: 9 | enabled: true 10 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-cloud-data-flow-server/README.md: -------------------------------------------------------------------------------- 1 | # spring-cloud-data-flow-server 2 | 3 | Spring Cloud Data Server on Spring Boot. 4 | 5 | [https://codenotfound.com/spring-batch-admin-example.html](https://codenotfound.com/spring-batch-admin-example.html) 6 | -------------------------------------------------------------------------------- /spring-batch-embedded-database/src/main/java/com/codenotfound/batch/BatchConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | @Configuration 7 | @EnableBatchProcessing 8 | public class BatchConfig { 9 | } 10 | -------------------------------------------------------------------------------- /spring-batch-itemreader/README.md: -------------------------------------------------------------------------------- 1 | # spring-batch-itemreader 2 | 3 | [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=com.codenotfound%3Aspring-batch-itemreader&metric=alert_status)](https://sonarcloud.io/dashboard?id=com.codenotfound%3Aspring-batch-itemreader) 4 | 5 | [https://www.codenotfound.com/spring-batch](https://www.codenotfound.com/spring-batch) 6 | -------------------------------------------------------------------------------- /spring-batch-itemwriter/README.md: -------------------------------------------------------------------------------- 1 | # spring-batch-itemwriter 2 | 3 | [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=com.codenotfound%3Aspring-batch-itemwriter&metric=alert_status)](https://sonarcloud.io/dashboard?id=com.codenotfound%3Aspring-batch-itemwriter) 4 | 5 | [https://www.codenotfound.com/spring-batch](https://www.codenotfound.com/spring-batch) 6 | -------------------------------------------------------------------------------- /spring-batch-embedded-database/src/main/java/com/codenotfound/SpringBatchApplication.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBatchApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBatchApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-batch-task/README.md: -------------------------------------------------------------------------------- 1 | # spring-batch-task 2 | 3 | [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=com.codenotfound%3Aspring-batch-task&metric=alert_status)](https://sonarcloud.io/dashboard?id=com.codenotfound%3Aspring-batch-task) 4 | 5 | Spring Batch Task example using Spring Boot and Maven. 6 | 7 | [https://codenotfound.com/spring-batch-admin-example.html](https://codenotfound.com/spring-batch-admin-example.html) 8 | -------------------------------------------------------------------------------- /spring-batch-tasklet/README.md: -------------------------------------------------------------------------------- 1 | # spring-batch-tasklet 2 | 3 | [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=com.codenotfound%3Aspring-batch-tasklet&metric=alert_status)](https://sonarcloud.io/dashboard?id=com.codenotfound%3Aspring-batch-tasklet) 4 | 5 | A detailed step-by-step tutorial on how to implement a Spring Batch Tasklet using Spring Boot. 6 | 7 | [https://codenotfound.com/spring-batch-tasklet-example.html](https://codenotfound.com/spring-batch-tasklet-example.html) 8 | -------------------------------------------------------------------------------- /spring-batch-hello-world/README.md: -------------------------------------------------------------------------------- 1 | # spring-batch-hello-world 2 | 3 | [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=com.codenotfound%3Aspring-batch-hello-world&metric=alert_status)](https://sonarcloud.io/dashboard?id=com.codenotfound%3Aspring-batch-hello-world) 4 | 5 | A detailed step-by-step tutorial on how to implement a Hello World Spring Batch job using Spring Boot. 6 | 7 | [https://codenotfound.com/spring-batch-example.html](https://codenotfound.com/spring-batch-example.html) 8 | -------------------------------------------------------------------------------- /spring-batch-scheduler/README.md: -------------------------------------------------------------------------------- 1 | # spring-batch-scheduler 2 | 3 | [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=com.codenotfound%3Aspring-batch-scheduler&metric=alert_status)](https://sonarcloud.io/dashboard?id=com.codenotfound%3Aspring-batch-scheduler) 4 | 5 | A detailed step-by-step tutorial on how to use a scheduler to run Spring Batch jobs using Spring Boot and Maven. 6 | 7 | [https://codenotfound.com/spring-batch-scheduler-example.html](https://codenotfound.com/spring-batch-scheduler-example.html) 8 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-batch-task/src/main/java/com/codenotfound/SpringBatchApplication.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.task.configuration.EnableTask; 6 | 7 | @EnableTask 8 | @SpringBootApplication 9 | public class SpringBatchApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBatchApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring Batch Tutorials 2 | 3 | [![Build Status](https://travis-ci.org/code-not-found/spring-batch.svg?branch=master)](https://travis-ci.org/code-not-found/spring-batch) 4 | [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE) 5 | 6 | This repository contains the source code for the Spring Batch examples posted on [https://codenotfound.com/spring-batch-tutorials](https://codenotfound.com/spring-batch-tutorials) 7 | 8 | In case of questions or remarks please leave a comment in the respective blog post or open a GitHub issue. Thanks! 9 | -------------------------------------------------------------------------------- /spring-batch-hello-world/src/main/java/com/codenotfound/SpringBatchApplication.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6 | 7 | @SpringBootApplication( 8 | exclude = {DataSourceAutoConfiguration.class}) 9 | public class SpringBatchApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBatchApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-batch-scheduler/src/main/java/com/codenotfound/SpringBatchApplication.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6 | 7 | @SpringBootApplication( 8 | exclude = {DataSourceAutoConfiguration.class}) 9 | public class SpringBatchApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBatchApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-batch-tasklet/src/main/java/com/codenotfound/SpringBatchApplication.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6 | 7 | @SpringBootApplication( 8 | exclude = {DataSourceAutoConfiguration.class}) 9 | public class SpringBatchApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBatchApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-batch-capitalize-names/src/main/java/com/codenotfound/SpringBatchApplication.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6 | 7 | @SpringBootApplication( 8 | exclude = {DataSourceAutoConfiguration.class}) 9 | public class SpringBatchApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBatchApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-batch-itemreader/src/main/java/com/codenotfound/batch/SpringBatchApplication.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6 | 7 | @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 8 | public class SpringBatchApplication { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(SpringBatchApplication.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-batch-itemwriter/src/main/java/com/codenotfound/batch/SpringBatchApplication.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6 | 7 | @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 8 | public class SpringBatchApplication { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(SpringBatchApplication.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-batch-capitalize-names/README.md: -------------------------------------------------------------------------------- 1 | # spring-batch-capitalize-names 2 | 3 | [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=com.codenotfound%3Aspring-batch-capitalize-names&metric=alert_status)](https://sonarcloud.io/dashboard?id=com.codenotfound%3Aspring-batch-capitalize-names) 4 | 5 | A basic Spring Batch example that converts person names into upper case. The input is a CSV file that contains comma separated first and last names. The output is a text file that contains comma separated first and last names that are all in upper case. 6 | 7 | [https://codenotfound.com/spring-batch-tutorials](https://codenotfound.com/spring-batch-tutorials) 8 | -------------------------------------------------------------------------------- /spring-batch-scheduler/src/main/java/com/codenotfound/batch/BatchConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import javax.sql.DataSource; 4 | import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer; 5 | import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | @Configuration 9 | @EnableBatchProcessing 10 | public class BatchConfig extends DefaultBatchConfigurer { 11 | 12 | @Override 13 | public void setDataSource(DataSource dataSource) { 14 | // initialize will use a Map based JobRepository (instead of database) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-batch-tasklet/src/main/java/com/codenotfound/batch/BatchConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import javax.sql.DataSource; 4 | import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer; 5 | import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | @Configuration 9 | @EnableBatchProcessing 10 | public class BatchConfig extends DefaultBatchConfigurer { 11 | 12 | @Override 13 | public void setDataSource(DataSource dataSource) { 14 | // initialize will use a Map based JobRepository (instead of database) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-batch-hello-world/src/main/java/com/codenotfound/batch/BatchConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import javax.sql.DataSource; 4 | import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer; 5 | import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | @Configuration 9 | @EnableBatchProcessing 10 | public class BatchConfig extends DefaultBatchConfigurer { 11 | 12 | @Override 13 | public void setDataSource(DataSource dataSource) { 14 | // initialize will use a Map based JobRepository (instead of database) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-batch-capitalize-names/src/main/java/com/codenotfound/batch/BatchConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import javax.sql.DataSource; 4 | import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer; 5 | import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | @Configuration 9 | @EnableBatchProcessing 10 | public class BatchConfig extends DefaultBatchConfigurer { 11 | 12 | @Override 13 | public void setDataSource(DataSource dataSource) { 14 | // initialize will use a Map based JobRepository (instead of database) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-batch-itemreader/src/main/java/com/codenotfound/model/Person.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.model; 2 | 3 | public class Person { 4 | private String firstName; 5 | private String lastName; 6 | 7 | public Person() {} 8 | 9 | public String getFirstName() { 10 | return firstName; 11 | } 12 | 13 | public void setFirstName(String firstName) { 14 | this.firstName = firstName; 15 | } 16 | 17 | public String getLastName() { 18 | return lastName; 19 | } 20 | 21 | public void setLastName(String lastName) { 22 | this.lastName = lastName; 23 | } 24 | 25 | @Override 26 | public String toString() { 27 | return firstName + " " + lastName; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /spring-batch-itemwriter/src/main/java/com/codenotfound/model/Person.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.model; 2 | 3 | public class Person { 4 | private String firstName; 5 | private String lastName; 6 | 7 | public Person() {} 8 | 9 | public String getFirstName() { 10 | return firstName; 11 | } 12 | 13 | public void setFirstName(String firstName) { 14 | this.firstName = firstName; 15 | } 16 | 17 | public String getLastName() { 18 | return lastName; 19 | } 20 | 21 | public void setLastName(String lastName) { 22 | this.lastName = lastName; 23 | } 24 | 25 | @Override 26 | public String toString() { 27 | return firstName + " " + lastName; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /spring-batch-scheduler/src/main/java/com/codenotfound/model/Person.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.model; 2 | 3 | public class Person { 4 | private String firstName; 5 | private String lastName; 6 | 7 | public Person() { 8 | // default constructor 9 | } 10 | 11 | public String getFirstName() { 12 | return firstName; 13 | } 14 | 15 | public void setFirstName(String firstName) { 16 | this.firstName = firstName; 17 | } 18 | 19 | public String getLastName() { 20 | return lastName; 21 | } 22 | 23 | public void setLastName(String lastName) { 24 | this.lastName = lastName; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return firstName + " " + lastName; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-batch-tasklet/src/main/java/com/codenotfound/model/Person.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.model; 2 | 3 | public class Person { 4 | private String firstName; 5 | private String lastName; 6 | 7 | public Person() { 8 | // default constructor 9 | } 10 | 11 | public String getFirstName() { 12 | return firstName; 13 | } 14 | 15 | public void setFirstName(String firstName) { 16 | this.firstName = firstName; 17 | } 18 | 19 | public String getLastName() { 20 | return lastName; 21 | } 22 | 23 | public void setLastName(String lastName) { 24 | this.lastName = lastName; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return firstName + " " + lastName; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-batch-hello-world/src/main/java/com/codenotfound/model/Person.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.model; 2 | 3 | public class Person { 4 | private String firstName; 5 | private String lastName; 6 | 7 | public Person() { 8 | // default constructor 9 | } 10 | 11 | public String getFirstName() { 12 | return firstName; 13 | } 14 | 15 | public void setFirstName(String firstName) { 16 | this.firstName = firstName; 17 | } 18 | 19 | public String getLastName() { 20 | return lastName; 21 | } 22 | 23 | public void setLastName(String lastName) { 24 | this.lastName = lastName; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return firstName + " " + lastName; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-batch-capitalize-names/src/main/java/com/codenotfound/model/Person.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.model; 2 | 3 | public class Person { 4 | private String firstName; 5 | private String lastName; 6 | 7 | public Person() { 8 | // default constructor 9 | } 10 | 11 | public String getFirstName() { 12 | return firstName; 13 | } 14 | 15 | public void setFirstName(String firstName) { 16 | this.firstName = firstName; 17 | } 18 | 19 | public String getLastName() { 20 | return lastName; 21 | } 22 | 23 | public void setLastName(String lastName) { 24 | this.lastName = lastName; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return firstName + " " + lastName; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-batch-embedded-database/src/main/java/com/codenotfound/model/Person.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.model; 2 | 3 | public class Person { 4 | private String firstName; 5 | private String lastName; 6 | 7 | public Person() { 8 | // default constructor 9 | } 10 | 11 | public String getFirstName() { 12 | return firstName; 13 | } 14 | 15 | public void setFirstName(String firstName) { 16 | this.firstName = firstName; 17 | } 18 | 19 | public String getLastName() { 20 | return lastName; 21 | } 22 | 23 | public void setLastName(String lastName) { 24 | this.lastName = lastName; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return firstName + " " + lastName; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-batch-task/src/main/java/com/codenotfound/model/Person.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.model; 2 | 3 | public class Person { 4 | private String firstName; 5 | private String lastName; 6 | 7 | public Person() { 8 | // default constructor 9 | } 10 | 11 | public String getFirstName() { 12 | return firstName; 13 | } 14 | 15 | public void setFirstName(String firstName) { 16 | this.firstName = firstName; 17 | } 18 | 19 | public String getLastName() { 20 | return lastName; 21 | } 22 | 23 | public void setLastName(String lastName) { 24 | this.lastName = lastName; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return firstName + " " + lastName; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-cloud-data-flow-server/src/main/java/com/codenotfound/SpringDataFlowServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.dataflow.server.EnableDataFlowServer; 6 | import org.springframework.cloud.deployer.spi.cloudfoundry.CloudFoundryDeployerAutoConfiguration; 7 | 8 | @EnableDataFlowServer 9 | @SpringBootApplication( 10 | exclude = {CloudFoundryDeployerAutoConfiguration.class}) 11 | public class SpringDataFlowServerApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SpringDataFlowServerApplication.class, 15 | args); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-batch-hello-world/src/main/java/com/codenotfound/batch/PersonItemProcessor.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.batch.item.ItemProcessor; 6 | import com.codenotfound.model.Person; 7 | 8 | public class PersonItemProcessor 9 | implements ItemProcessor { 10 | 11 | private static final Logger LOGGER = 12 | LoggerFactory.getLogger(PersonItemProcessor.class); 13 | 14 | @Override 15 | public String process(Person person) throws Exception { 16 | String greeting = "Hello " + person.getFirstName() + " " 17 | + person.getLastName() + "!"; 18 | 19 | LOGGER.info("converting '{}' into '{}'", person, greeting); 20 | return greeting; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: false 3 | install: true 4 | 5 | jdk: 6 | - openjdk11 7 | 8 | addons: 9 | sonarqube: 10 | organization: "code-not-found-spring-batch" 11 | token: 12 | secure: $SONAR_TOKEN 13 | 14 | env: 15 | - PROJECT_DIR=spring-batch-admin/spring-batch-task 16 | - PROJECT_DIR=spring-batch-capitalize-names 17 | - PROJECT_DIR=spring-batch-hello-world 18 | - PROJECT_DIR=spring-batch-itemreader 19 | - PROJECT_DIR=spring-batch-itemwriter 20 | - PROJECT_DIR=spring-batch-scheduler 21 | - PROJECT_DIR=spring-batch-tasklet 22 | 23 | script: 24 | - cd $PROJECT_DIR 25 | - mvn test -B 26 | - mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install sonar:sonar 27 | 28 | cache: 29 | directories: 30 | - $HOME/.m2/repository 31 | - $HOME/.sonar/cache 32 | 33 | notifications: 34 | email: false 35 | -------------------------------------------------------------------------------- /spring-batch-itemreader/src/main/java/com/codenotfound/batch/job/PersonNameProcessor.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch.job; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.batch.item.ItemProcessor; 6 | import com.codenotfound.model.Person; 7 | 8 | public class PersonNameProcessor implements ItemProcessor { 9 | 10 | private static final Logger LOGGER = LoggerFactory.getLogger(PersonNameProcessor.class); 11 | 12 | @Override 13 | public Person process(Person person) throws Exception { 14 | Person result = new Person(); 15 | result.setFirstName(person.getFirstName().toUpperCase()); 16 | result.setLastName(person.getLastName().toUpperCase()); 17 | 18 | LOGGER.info("converting '{}' into '{}'", person, result); 19 | return result; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /spring-batch-itemwriter/src/main/java/com/codenotfound/batch/job/PersonNameProcessor.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch.job; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.batch.item.ItemProcessor; 6 | import com.codenotfound.model.Person; 7 | 8 | public class PersonNameProcessor implements ItemProcessor { 9 | 10 | private static final Logger LOGGER = LoggerFactory.getLogger(PersonNameProcessor.class); 11 | 12 | @Override 13 | public Person process(Person person) throws Exception { 14 | Person result = new Person(); 15 | result.setFirstName(person.getFirstName().toUpperCase()); 16 | result.setLastName(person.getLastName().toUpperCase()); 17 | 18 | LOGGER.info("converting '{}' into '{}'", person, result); 19 | return result; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /spring-batch-tasklet/src/main/java/com/codenotfound/batch/PersonItemProcessor.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.batch.item.ItemProcessor; 6 | import com.codenotfound.model.Person; 7 | 8 | public class PersonItemProcessor 9 | implements ItemProcessor { 10 | 11 | private static final Logger LOGGER = 12 | LoggerFactory.getLogger(PersonItemProcessor.class); 13 | 14 | @Override 15 | public Person process(Person person) throws Exception { 16 | Person result = new Person(); 17 | result.setFirstName(person.getFirstName().toUpperCase()); 18 | result.setLastName(person.getLastName().toUpperCase()); 19 | 20 | LOGGER.info("converting '{}' into '{}'", person, result); 21 | return result; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-batch-scheduler/src/main/java/com/codenotfound/batch/PersonItemProcessor.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.batch.item.ItemProcessor; 6 | import com.codenotfound.model.Person; 7 | 8 | public class PersonItemProcessor 9 | implements ItemProcessor { 10 | 11 | private static final Logger LOGGER = 12 | LoggerFactory.getLogger(PersonItemProcessor.class); 13 | 14 | @Override 15 | public Person process(Person person) throws Exception { 16 | Person result = new Person(); 17 | result.setFirstName(person.getFirstName().toUpperCase()); 18 | result.setLastName(person.getLastName().toUpperCase()); 19 | 20 | LOGGER.info("converting '{}' into '{}'", person, result); 21 | return result; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-batch-capitalize-names/src/main/java/com/codenotfound/batch/PersonItemProcessor.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.batch.item.ItemProcessor; 6 | import com.codenotfound.model.Person; 7 | 8 | public class PersonItemProcessor 9 | implements ItemProcessor { 10 | 11 | private static final Logger LOGGER = 12 | LoggerFactory.getLogger(PersonItemProcessor.class); 13 | 14 | @Override 15 | public Person process(Person person) throws Exception { 16 | Person result = new Person(); 17 | result.setFirstName(person.getFirstName().toUpperCase()); 18 | result.setLastName(person.getLastName().toUpperCase()); 19 | 20 | LOGGER.info("converting '{}' into '{}'", person, result); 21 | return result; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-batch-embedded-database/src/main/java/com/codenotfound/batch/PersonItemProcessor.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.batch.item.ItemProcessor; 6 | import com.codenotfound.model.Person; 7 | 8 | public class PersonItemProcessor 9 | implements ItemProcessor { 10 | 11 | private static final Logger LOGGER = 12 | LoggerFactory.getLogger(PersonItemProcessor.class); 13 | 14 | @Override 15 | public Person process(Person person) throws Exception { 16 | Person result = new Person(); 17 | result.setFirstName(person.getFirstName().toUpperCase()); 18 | result.setLastName(person.getLastName().toUpperCase()); 19 | 20 | LOGGER.info("converting '{}' into '{}'", person, result); 21 | return result; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-batch-task/src/main/java/com/codenotfound/batch/PersonItemProcessor.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.batch.item.ItemProcessor; 6 | import com.codenotfound.model.Person; 7 | 8 | public class PersonItemProcessor 9 | implements ItemProcessor { 10 | 11 | private static final Logger LOGGER = 12 | LoggerFactory.getLogger(PersonItemProcessor.class); 13 | 14 | @Override 15 | public Person process(Person person) throws Exception { 16 | Person result = new Person(); 17 | result.setFirstName(person.getFirstName().toUpperCase()); 18 | result.setLastName(person.getLastName().toUpperCase()); 19 | 20 | LOGGER.info("converting '{}' into '{}'", person, result); 21 | return result; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-batch-scheduler/src/main/java/com/codenotfound/batch/CapitalizeNamesJobController.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | @RequestMapping("/toggle-batch-job") 12 | public class CapitalizeNamesJobController { 13 | 14 | private static final Logger LOGGER = 15 | LoggerFactory.getLogger(CapitalizeNamesJobScheduler.class); 16 | 17 | @Autowired 18 | CapitalizeNamesJobScheduler capitalizeNamesJobScheduler; 19 | 20 | @GetMapping 21 | public String toggleBatchJob() { 22 | boolean toggleEnabled = 23 | !capitalizeNamesJobScheduler.isEnabled(); 24 | capitalizeNamesJobScheduler.setEnabled(toggleEnabled); 25 | 26 | String result = "isEnabled=" + toggleEnabled; 27 | LOGGER.info(result); 28 | 29 | return result; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 CodeNotFound 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /spring-batch-tasklet/src/main/java/com/codenotfound/batch/FileDeletingTasklet.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.nio.file.Files; 6 | import java.nio.file.Path; 7 | import java.nio.file.Paths; 8 | import java.util.stream.Stream; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | import org.springframework.batch.core.StepContribution; 12 | import org.springframework.batch.core.UnexpectedJobExecutionException; 13 | import org.springframework.batch.core.scope.context.ChunkContext; 14 | import org.springframework.batch.core.step.tasklet.Tasklet; 15 | import org.springframework.batch.repeat.RepeatStatus; 16 | import org.springframework.core.io.Resource; 17 | 18 | public class FileDeletingTasklet implements Tasklet { 19 | 20 | private static final Logger LOGGER = 21 | LoggerFactory.getLogger(FileDeletingTasklet.class); 22 | 23 | private Resource directory; 24 | 25 | public FileDeletingTasklet(Resource directory) { 26 | this.directory = directory; 27 | } 28 | 29 | @Override 30 | public RepeatStatus execute(StepContribution stepContribution, 31 | ChunkContext chunkContext) { 32 | try (Stream walk = 33 | Files.walk(Paths.get(directory.getFile().getPath()))) { 34 | walk.filter(Files::isRegularFile).map(Path::toFile) 35 | .forEach(File::delete); 36 | } catch (IOException e) { 37 | LOGGER.error("error deleting files", e); 38 | throw new UnexpectedJobExecutionException( 39 | "unable to delete files"); 40 | } 41 | 42 | return RepeatStatus.FINISHED; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /spring-batch-itemreader/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.codenotfound 8 | spring-batch-itemreader 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | spring-batch-itemreader 13 | Spring Batch ItemReader Example 14 | https://www.codenotfound.com/spring-batch 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.1.5.RELEASE 20 | 21 | 22 | 23 | 24 | 11 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-batch 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | org.springframework.batch 39 | spring-batch-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /spring-batch-itemwriter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.codenotfound 8 | spring-batch-itemwriter 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | spring-batch-itemwriter 13 | Spring Batch ItemWriter Example 14 | https://www.codenotfound.com/spring-batch 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.1.5.RELEASE 20 | 21 | 22 | 23 | 24 | 11 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-batch 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | org.springframework.batch 39 | spring-batch-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /spring-batch-hello-world/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.codenotfound 8 | spring-batch-hello-world 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | spring-batch-hello-world 13 | Spring Batch Hello World Example 14 | https://codenotfound.com/spring-batch-example.html 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.1.5.RELEASE 20 | 21 | 22 | 23 | 24 | 11 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-batch 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | org.springframework.batch 39 | spring-batch-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /spring-batch-itemreader/src/test/java/com/codenotfound/batch/SpringBatchApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.batch.core.Job; 7 | import org.springframework.batch.core.JobExecution; 8 | import org.springframework.batch.core.launch.NoSuchJobException; 9 | import org.springframework.batch.test.JobLauncherTestUtils; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.context.annotation.Import; 15 | import org.springframework.test.context.junit4.SpringRunner; 16 | import com.codenotfound.batch.job.ConvertNamesJobConfig; 17 | 18 | @RunWith(SpringRunner.class) 19 | @SpringBootTest(classes = {SpringBatchApplicationTests.BatchTestConfig.class}) 20 | public class SpringBatchApplicationTests { 21 | 22 | @Autowired 23 | private JobLauncherTestUtils jobLauncherTestUtils; 24 | 25 | @Test 26 | public void testConvertNamesJob() throws Exception { 27 | JobExecution jobExecution = jobLauncherTestUtils.launchJob(); 28 | assertThat(jobExecution.getExitStatus().getExitCode()).isEqualTo("COMPLETED"); 29 | } 30 | 31 | @Configuration 32 | @Import(ConvertNamesJobConfig.class) 33 | static class BatchTestConfig { 34 | 35 | @Autowired 36 | private Job convertNamesJob; 37 | 38 | @Bean 39 | JobLauncherTestUtils jobLauncherTestUtils() throws NoSuchJobException { 40 | JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils(); 41 | jobLauncherTestUtils.setJob(convertNamesJob); 42 | 43 | return jobLauncherTestUtils; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /spring-batch-itemwriter/src/test/java/com/codenotfound/batch/SpringBatchApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.batch.core.Job; 7 | import org.springframework.batch.core.JobExecution; 8 | import org.springframework.batch.core.launch.NoSuchJobException; 9 | import org.springframework.batch.test.JobLauncherTestUtils; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.context.annotation.Import; 15 | import org.springframework.test.context.junit4.SpringRunner; 16 | import com.codenotfound.batch.job.ConvertNamesJobConfig; 17 | 18 | @RunWith(SpringRunner.class) 19 | @SpringBootTest(classes = {SpringBatchApplicationTests.BatchTestConfig.class}) 20 | public class SpringBatchApplicationTests { 21 | 22 | @Autowired 23 | private JobLauncherTestUtils jobLauncherTestUtils; 24 | 25 | @Test 26 | public void testConvertNamesJob() throws Exception { 27 | JobExecution jobExecution = jobLauncherTestUtils.launchJob(); 28 | assertThat(jobExecution.getExitStatus().getExitCode()).isEqualTo("COMPLETED"); 29 | } 30 | 31 | @Configuration 32 | @Import(ConvertNamesJobConfig.class) 33 | static class BatchTestConfig { 34 | 35 | @Autowired 36 | private Job convertNamesJob; 37 | 38 | @Bean 39 | JobLauncherTestUtils jobLauncherTestUtils() throws NoSuchJobException { 40 | JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils(); 41 | jobLauncherTestUtils.setJob(convertNamesJob); 42 | 43 | return jobLauncherTestUtils; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-batch-task/src/test/java/com/codenotfound/SpringBatchApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.batch.core.Job; 7 | import org.springframework.batch.core.JobExecution; 8 | import org.springframework.batch.core.launch.NoSuchJobException; 9 | import org.springframework.batch.test.JobLauncherTestUtils; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.context.annotation.Import; 15 | import org.springframework.test.context.junit4.SpringRunner; 16 | import com.codenotfound.batch.CapitalizeNamesJobConfig; 17 | 18 | @RunWith(SpringRunner.class) 19 | @SpringBootTest( 20 | classes = {SpringBatchApplicationTests.BatchTestConfig.class}) 21 | public class SpringBatchApplicationTests { 22 | 23 | @Autowired 24 | private JobLauncherTestUtils jobLauncherTestUtils; 25 | 26 | @Test 27 | public void testConvertNamesJob() throws Exception { 28 | JobExecution jobExecution = jobLauncherTestUtils.launchJob(); 29 | assertThat(jobExecution.getExitStatus().getExitCode()) 30 | .isEqualTo("COMPLETED"); 31 | } 32 | 33 | @Configuration 34 | @Import(CapitalizeNamesJobConfig.class) 35 | static class BatchTestConfig { 36 | 37 | @Autowired 38 | private Job capitalizeNamesJob; 39 | 40 | @Bean 41 | JobLauncherTestUtils jobLauncherTestUtils() 42 | throws NoSuchJobException { 43 | JobLauncherTestUtils jobLauncherTestUtils = 44 | new JobLauncherTestUtils(); 45 | jobLauncherTestUtils.setJob(capitalizeNamesJob); 46 | 47 | return jobLauncherTestUtils; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-cloud-data-flow-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.codenotfound 8 | spring-cloud-data-flow-server 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | spring-cloud-data-flow-server 13 | Spring Cloud Data Server on Spring Boot 14 | https://codenotfound.com/spring-batch-admin-example.html 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.1.5.RELEASE 20 | 21 | 22 | 23 | 24 | 11 25 | 2.1.0.RELEASE 26 | 1.4.193 27 | 28 | 29 | 30 | 31 | org.springframework.cloud 32 | spring-cloud-starter-dataflow-server 33 | ${spring-cloud-starter-dataflow-server.version} 34 | 35 | 36 | com.h2database 37 | h2 38 | ${h2.version} 39 | 40 | 41 | 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-maven-plugin 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /spring-batch-hello-world/src/test/java/com/codenotfound/SpringBatchApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.batch.core.Job; 7 | import org.springframework.batch.core.JobExecution; 8 | import org.springframework.batch.core.launch.NoSuchJobException; 9 | import org.springframework.batch.test.JobLauncherTestUtils; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.context.annotation.Import; 15 | import org.springframework.test.context.junit4.SpringRunner; 16 | import com.codenotfound.batch.BatchConfig; 17 | import com.codenotfound.batch.HelloWorldJobConfig; 18 | 19 | @RunWith(SpringRunner.class) 20 | @SpringBootTest( 21 | classes = {SpringBatchApplicationTests.BatchTestConfig.class}) 22 | public class SpringBatchApplicationTests { 23 | 24 | @Autowired 25 | private JobLauncherTestUtils jobLauncherTestUtils; 26 | 27 | @Test 28 | public void testHelloWorldJob() throws Exception { 29 | JobExecution jobExecution = jobLauncherTestUtils.launchJob(); 30 | assertThat(jobExecution.getExitStatus().getExitCode()) 31 | .isEqualTo("COMPLETED"); 32 | } 33 | 34 | @Configuration 35 | @Import({BatchConfig.class, HelloWorldJobConfig.class}) 36 | static class BatchTestConfig { 37 | 38 | @Autowired 39 | private Job helloWorlJob; 40 | 41 | @Bean 42 | JobLauncherTestUtils jobLauncherTestUtils() 43 | throws NoSuchJobException { 44 | JobLauncherTestUtils jobLauncherTestUtils = 45 | new JobLauncherTestUtils(); 46 | jobLauncherTestUtils.setJob(helloWorlJob); 47 | 48 | return jobLauncherTestUtils; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spring-batch-scheduler/src/main/java/com/codenotfound/batch/CapitalizeNamesJobScheduler.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import java.util.Date; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | import org.springframework.batch.core.Job; 7 | import org.springframework.batch.core.JobParametersBuilder; 8 | import org.springframework.batch.core.JobParametersInvalidException; 9 | import org.springframework.batch.core.launch.JobLauncher; 10 | import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; 11 | import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; 12 | import org.springframework.batch.core.repository.JobRestartException; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.scheduling.annotation.EnableScheduling; 15 | import org.springframework.scheduling.annotation.Scheduled; 16 | import org.springframework.stereotype.Component; 17 | 18 | @EnableScheduling 19 | @Component 20 | public class CapitalizeNamesJobScheduler { 21 | 22 | private static final Logger LOGGER = 23 | LoggerFactory.getLogger(CapitalizeNamesJobScheduler.class); 24 | 25 | @Autowired 26 | private JobLauncher jobLauncher; 27 | 28 | @Autowired 29 | private Job capitalizeNamesJob; 30 | 31 | private boolean enabled = false; 32 | 33 | @Scheduled(cron = "0/10 * * * * ?") 34 | public void runBatchJob() 35 | throws JobExecutionAlreadyRunningException, 36 | JobRestartException, JobInstanceAlreadyCompleteException, 37 | JobParametersInvalidException { 38 | LOGGER.info("start runBatchJob"); 39 | 40 | if (enabled) { 41 | jobLauncher.run(capitalizeNamesJob, new JobParametersBuilder() 42 | .addDate("date", new Date()).toJobParameters()); 43 | } 44 | } 45 | 46 | public boolean isEnabled() { 47 | return enabled; 48 | } 49 | 50 | public void setEnabled(boolean enabled) { 51 | this.enabled = enabled; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /spring-batch-scheduler/src/test/java/com/codenotfound/SpringBatchApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.batch.core.Job; 7 | import org.springframework.batch.core.JobExecution; 8 | import org.springframework.batch.core.launch.NoSuchJobException; 9 | import org.springframework.batch.test.JobLauncherTestUtils; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.context.annotation.Import; 15 | import org.springframework.test.context.junit4.SpringRunner; 16 | import com.codenotfound.batch.BatchConfig; 17 | import com.codenotfound.batch.CapitalizeNamesJobConfig; 18 | 19 | @RunWith(SpringRunner.class) 20 | @SpringBootTest( 21 | classes = {SpringBatchApplicationTests.BatchTestConfig.class}) 22 | public class SpringBatchApplicationTests { 23 | 24 | @Autowired 25 | private JobLauncherTestUtils jobLauncherTestUtils; 26 | 27 | @Test 28 | public void testConvertNamesJob() throws Exception { 29 | JobExecution jobExecution = jobLauncherTestUtils.launchJob(); 30 | assertThat(jobExecution.getExitStatus().getExitCode()) 31 | .isEqualTo("COMPLETED"); 32 | } 33 | 34 | @Configuration 35 | @Import({BatchConfig.class, CapitalizeNamesJobConfig.class}) 36 | static class BatchTestConfig { 37 | 38 | @Autowired 39 | private Job capitalizeNamesJob; 40 | 41 | @Bean 42 | JobLauncherTestUtils jobLauncherTestUtils() 43 | throws NoSuchJobException { 44 | JobLauncherTestUtils jobLauncherTestUtils = 45 | new JobLauncherTestUtils(); 46 | jobLauncherTestUtils.setJob(capitalizeNamesJob); 47 | 48 | return jobLauncherTestUtils; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spring-batch-capitalize-names/src/test/java/com/codenotfound/SpringBatchApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.batch.core.Job; 7 | import org.springframework.batch.core.JobExecution; 8 | import org.springframework.batch.core.launch.NoSuchJobException; 9 | import org.springframework.batch.test.JobLauncherTestUtils; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.context.annotation.Import; 15 | import org.springframework.test.context.junit4.SpringRunner; 16 | import com.codenotfound.batch.BatchConfig; 17 | import com.codenotfound.batch.CapitalizeNamesJobConfig; 18 | 19 | @RunWith(SpringRunner.class) 20 | @SpringBootTest( 21 | classes = {SpringBatchApplicationTests.BatchTestConfig.class}) 22 | public class SpringBatchApplicationTests { 23 | 24 | @Autowired 25 | private JobLauncherTestUtils jobLauncherTestUtils; 26 | 27 | @Test 28 | public void testConvertNamesJob() throws Exception { 29 | JobExecution jobExecution = jobLauncherTestUtils.launchJob(); 30 | assertThat(jobExecution.getExitStatus().getExitCode()) 31 | .isEqualTo("COMPLETED"); 32 | } 33 | 34 | @Configuration 35 | @Import({BatchConfig.class, CapitalizeNamesJobConfig.class}) 36 | static class BatchTestConfig { 37 | 38 | @Autowired 39 | private Job capitalizeNamesJob; 40 | 41 | @Bean 42 | JobLauncherTestUtils jobLauncherTestUtils() 43 | throws NoSuchJobException { 44 | JobLauncherTestUtils jobLauncherTestUtils = 45 | new JobLauncherTestUtils(); 46 | jobLauncherTestUtils.setJob(capitalizeNamesJob); 47 | 48 | return jobLauncherTestUtils; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spring-batch-tasklet/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.codenotfound 8 | spring-batch-tasklet 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | spring-batch-tasklet 13 | Spring Batch Tasklet Example 14 | https://codenotfound.com/spring-batch-tasklet-example.html 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.1.5.RELEASE 20 | 21 | 22 | 23 | 24 | UTF-8 25 | UTF-8 26 | 11 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-batch 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-test 37 | test 38 | 39 | 40 | org.springframework.batch 41 | spring-batch-test 42 | test 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-maven-plugin 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /spring-batch-embedded-database/src/test/java/com/codenotfound/SpringBatchApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.batch.core.Job; 7 | import org.springframework.batch.core.JobExecution; 8 | import org.springframework.batch.core.launch.NoSuchJobException; 9 | import org.springframework.batch.test.JobLauncherTestUtils; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.context.annotation.Import; 15 | import org.springframework.test.context.junit4.SpringRunner; 16 | import com.codenotfound.batch.BatchConfig; 17 | import com.codenotfound.batch.CapitalizeNamesJobConfig; 18 | 19 | @RunWith(SpringRunner.class) 20 | @SpringBootTest( 21 | classes = {SpringBatchApplicationTests.BatchTestConfig.class}) 22 | public class SpringBatchApplicationTests { 23 | 24 | @Autowired 25 | private JobLauncherTestUtils jobLauncherTestUtils; 26 | 27 | @Test 28 | public void testConvertNamesJob() throws Exception { 29 | JobExecution jobExecution = jobLauncherTestUtils.launchJob(); 30 | assertThat(jobExecution.getExitStatus().getExitCode()) 31 | .isEqualTo("COMPLETED"); 32 | } 33 | 34 | @Configuration 35 | @Import({BatchConfig.class, CapitalizeNamesJobConfig.class}) 36 | static class BatchTestConfig { 37 | 38 | @Autowired 39 | private Job capitalizeNamesJob; 40 | 41 | @Bean 42 | JobLauncherTestUtils jobLauncherTestUtils() 43 | throws NoSuchJobException { 44 | JobLauncherTestUtils jobLauncherTestUtils = 45 | new JobLauncherTestUtils(); 46 | jobLauncherTestUtils.setJob(capitalizeNamesJob); 47 | 48 | return jobLauncherTestUtils; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spring-batch-scheduler/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.codenotfound 8 | spring-batch-scheduler 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | spring-batch-scheduler 13 | Spring Batch Scheduler Example 14 | https://codenotfound.com/spring-batch-scheduler-example.html 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.1.5.RELEASE 20 | 21 | 22 | 23 | 24 | 11 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-batch 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | org.springframework.batch 43 | spring-batch-test 44 | test 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-maven-plugin 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /spring-batch-capitalize-names/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.codenotfound 8 | spring-batch-capitalize-names 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | spring-batch-capitalize-names 13 | Spring Batch Capitalize Names Example 14 | https://codenotfound.com/spring-batch-tutorials 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.1.5.RELEASE 20 | 21 | 22 | 23 | 24 | UTF-8 25 | UTF-8 26 | 11 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-batch 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-test 37 | test 38 | 39 | 40 | org.springframework.batch 41 | spring-batch-test 42 | test 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-maven-plugin 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /spring-batch-embedded-database/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.codenotfound 8 | spring-batch-embedded-database 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | spring-batch-embedded-database 13 | Spring Batch Embedded Database Example 14 | https://www.codenotfound.com/spring-batch-tutorials.html 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.1.5.RELEASE 20 | 21 | 22 | 23 | 24 | 11 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-batch 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | com.h2database 38 | h2 39 | runtime 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | org.springframework.batch 48 | spring-batch-test 49 | test 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-batch-task/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.codenotfound 8 | spring-batch-task 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | spring-batch-task 13 | Spring Batch Task Example 14 | https://codenotfound.com/spring-batch-admin-example.html 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.1.5.RELEASE 20 | 21 | 22 | 23 | 24 | 11 25 | 2.1.0.RELEASE 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-batch 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-starter-task 36 | ${spring-cloud-starter-task.version} 37 | 38 | 39 | com.h2database 40 | h2 41 | runtime 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-test 46 | test 47 | 48 | 49 | org.springframework.batch 50 | spring-batch-test 51 | test 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.springframework.boot 59 | spring-boot-maven-plugin 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /spring-batch-scheduler/src/main/java/com/codenotfound/batch/CapitalizeNamesJobConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.springframework.batch.core.Job; 4 | import org.springframework.batch.core.Step; 5 | import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 6 | import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 7 | import org.springframework.batch.item.file.FlatFileItemReader; 8 | import org.springframework.batch.item.file.FlatFileItemWriter; 9 | import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; 10 | import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.core.io.ClassPathResource; 14 | import org.springframework.core.io.FileSystemResource; 15 | import com.codenotfound.model.Person; 16 | 17 | @Configuration 18 | public class CapitalizeNamesJobConfig { 19 | 20 | @Bean 21 | public Job convertNamesJob(JobBuilderFactory jobBuilders, 22 | StepBuilderFactory stepBuilders) { 23 | return jobBuilders.get("capitalizeNamesJob") 24 | .start(convertNamesStep(stepBuilders)).build(); 25 | } 26 | 27 | @Bean 28 | public Step convertNamesStep(StepBuilderFactory stepBuilders) { 29 | return stepBuilders.get("capitalizeNamesStep") 30 | .chunk(10).reader(itemReader()) 31 | .processor(itemProcessor()).writer(itemWriter()).build(); 32 | } 33 | 34 | @Bean 35 | public FlatFileItemReader itemReader() { 36 | return new FlatFileItemReaderBuilder() 37 | .name("personItemReader") 38 | .resource(new ClassPathResource("csv/persons.csv")) 39 | .delimited().names(new String[] {"firstName", "lastName"}) 40 | .targetType(Person.class).build(); 41 | } 42 | 43 | @Bean 44 | public PersonItemProcessor itemProcessor() { 45 | return new PersonItemProcessor(); 46 | } 47 | 48 | @Bean 49 | public FlatFileItemWriter itemWriter() { 50 | return new FlatFileItemWriterBuilder() 51 | .name("personItemWriter") 52 | .resource(new FileSystemResource( 53 | "target/test-outputs/persons.txt")) 54 | .delimited().delimiter(", ") 55 | .names(new String[] {"firstName", "lastName"}).build(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /spring-batch-hello-world/src/main/java/com/codenotfound/batch/HelloWorldJobConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.springframework.batch.core.Job; 4 | import org.springframework.batch.core.Step; 5 | import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 6 | import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 7 | import org.springframework.batch.item.file.FlatFileItemReader; 8 | import org.springframework.batch.item.file.FlatFileItemWriter; 9 | import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; 10 | import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; 11 | import org.springframework.batch.item.file.transform.PassThroughLineAggregator; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.core.io.ClassPathResource; 15 | import org.springframework.core.io.FileSystemResource; 16 | import com.codenotfound.model.Person; 17 | 18 | @Configuration 19 | public class HelloWorldJobConfig { 20 | 21 | @Bean 22 | public Job helloWorlJob(JobBuilderFactory jobBuilders, 23 | StepBuilderFactory stepBuilders) { 24 | return jobBuilders.get("helloWorldJob") 25 | .start(helloWorldStep(stepBuilders)).build(); 26 | } 27 | 28 | @Bean 29 | public Step helloWorldStep(StepBuilderFactory stepBuilders) { 30 | return stepBuilders.get("helloWorldStep") 31 | .chunk(10).reader(reader()) 32 | .processor(processor()).writer(writer()).build(); 33 | } 34 | 35 | @Bean 36 | public FlatFileItemReader reader() { 37 | return new FlatFileItemReaderBuilder() 38 | .name("personItemReader") 39 | .resource(new ClassPathResource("csv/persons.csv")) 40 | .delimited().names(new String[] {"firstName", "lastName"}) 41 | .targetType(Person.class).build(); 42 | } 43 | 44 | @Bean 45 | public PersonItemProcessor processor() { 46 | return new PersonItemProcessor(); 47 | } 48 | 49 | @Bean 50 | public FlatFileItemWriter writer() { 51 | return new FlatFileItemWriterBuilder() 52 | .name("greetingItemWriter") 53 | .resource(new FileSystemResource( 54 | "target/test-outputs/greetings.txt")) 55 | .lineAggregator(new PassThroughLineAggregator<>()).build(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /spring-batch-capitalize-names/src/main/java/com/codenotfound/batch/CapitalizeNamesJobConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.springframework.batch.core.Job; 4 | import org.springframework.batch.core.Step; 5 | import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 6 | import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 7 | import org.springframework.batch.item.file.FlatFileItemReader; 8 | import org.springframework.batch.item.file.FlatFileItemWriter; 9 | import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; 10 | import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.core.io.ClassPathResource; 14 | import org.springframework.core.io.FileSystemResource; 15 | import com.codenotfound.model.Person; 16 | 17 | @Configuration 18 | public class CapitalizeNamesJobConfig { 19 | 20 | @Bean 21 | public Job convertNamesJob(JobBuilderFactory jobBuilders, 22 | StepBuilderFactory stepBuilders) { 23 | return jobBuilders.get("capitalizeNamesJob") 24 | .start(convertNamesStep(stepBuilders)).build(); 25 | } 26 | 27 | @Bean 28 | public Step convertNamesStep(StepBuilderFactory stepBuilders) { 29 | return stepBuilders.get("capitalizeNamesStep") 30 | .chunk(10).reader(itemReader()) 31 | .processor(itemProcessor()).writer(itemWriter()).build(); 32 | } 33 | 34 | @Bean 35 | public FlatFileItemReader itemReader() { 36 | return new FlatFileItemReaderBuilder() 37 | .name("personItemReader") 38 | .resource(new ClassPathResource("csv/persons.csv")) 39 | .delimited().names(new String[] {"firstName", "lastName"}) 40 | .targetType(Person.class).build(); 41 | } 42 | 43 | @Bean 44 | public PersonItemProcessor itemProcessor() { 45 | return new PersonItemProcessor(); 46 | } 47 | 48 | @Bean 49 | public FlatFileItemWriter itemWriter() { 50 | return new FlatFileItemWriterBuilder() 51 | .name("personItemWriter") 52 | .resource(new FileSystemResource( 53 | "target/test-outputs/persons.txt")) 54 | .delimited().delimiter(", ") 55 | .names(new String[] {"firstName", "lastName"}).build(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /spring-batch-embedded-database/src/main/java/com/codenotfound/batch/CapitalizeNamesJobConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.springframework.batch.core.Job; 4 | import org.springframework.batch.core.Step; 5 | import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 6 | import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 7 | import org.springframework.batch.item.file.FlatFileItemReader; 8 | import org.springframework.batch.item.file.FlatFileItemWriter; 9 | import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; 10 | import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.core.io.ClassPathResource; 14 | import org.springframework.core.io.FileSystemResource; 15 | import com.codenotfound.model.Person; 16 | 17 | @Configuration 18 | public class CapitalizeNamesJobConfig { 19 | 20 | @Bean 21 | public Job convertNamesJob(JobBuilderFactory jobBuilders, 22 | StepBuilderFactory stepBuilders) { 23 | return jobBuilders.get("capitalizeNamesJob") 24 | .start(convertNamesStep(stepBuilders)).build(); 25 | } 26 | 27 | @Bean 28 | public Step convertNamesStep(StepBuilderFactory stepBuilders) { 29 | return stepBuilders.get("capitalizeNamesStep") 30 | .chunk(10).reader(itemReader()) 31 | .processor(itemProcessor()).writer(itemWriter()).build(); 32 | } 33 | 34 | @Bean 35 | public FlatFileItemReader itemReader() { 36 | return new FlatFileItemReaderBuilder() 37 | .name("personItemReader") 38 | .resource(new ClassPathResource("csv/persons.csv")) 39 | .delimited().names(new String[] {"firstName", "lastName"}) 40 | .targetType(Person.class).build(); 41 | } 42 | 43 | @Bean 44 | public PersonItemProcessor itemProcessor() { 45 | return new PersonItemProcessor(); 46 | } 47 | 48 | @Bean 49 | public FlatFileItemWriter itemWriter() { 50 | return new FlatFileItemWriterBuilder() 51 | .name("personItemWriter") 52 | .resource(new FileSystemResource( 53 | "target/test-outputs/persons.txt")) 54 | .delimited().delimiter(", ") 55 | .names(new String[] {"firstName", "lastName"}).build(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /spring-batch-tasklet/src/test/java/com/codenotfound/SpringBatchApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import java.io.File; 5 | import java.io.IOException; 6 | import java.net.URISyntaxException; 7 | import java.nio.file.Files; 8 | import java.nio.file.Path; 9 | import java.nio.file.Paths; 10 | import org.junit.BeforeClass; 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | import org.springframework.batch.core.Job; 14 | import org.springframework.batch.core.JobExecution; 15 | import org.springframework.batch.core.launch.NoSuchJobException; 16 | import org.springframework.batch.test.JobLauncherTestUtils; 17 | import org.springframework.beans.factory.annotation.Autowired; 18 | import org.springframework.boot.test.context.SpringBootTest; 19 | import org.springframework.context.annotation.Bean; 20 | import org.springframework.context.annotation.Configuration; 21 | import org.springframework.context.annotation.Import; 22 | import org.springframework.core.io.ClassPathResource; 23 | import org.springframework.test.context.junit4.SpringRunner; 24 | import org.springframework.util.FileSystemUtils; 25 | import com.codenotfound.batch.BatchConfig; 26 | import com.codenotfound.batch.CapitalizeNamesJobConfig; 27 | 28 | @RunWith(SpringRunner.class) 29 | @SpringBootTest( 30 | classes = {SpringBatchApplicationTests.BatchTestConfig.class}) 31 | public class SpringBatchApplicationTests { 32 | 33 | private static Path csvFilesPath, testInputsPath; 34 | 35 | @Autowired 36 | private JobLauncherTestUtils jobLauncherTestUtils; 37 | 38 | @BeforeClass 39 | public static void copyFiles() 40 | throws URISyntaxException, IOException { 41 | csvFilesPath = Paths.get(new ClassPathResource("csv").getURI()); 42 | testInputsPath = Paths.get("target/test-inputs"); 43 | try { 44 | Files.createDirectory(testInputsPath); 45 | } catch (Exception e) { 46 | // if directory exists do nothing 47 | } 48 | 49 | FileSystemUtils.copyRecursively(csvFilesPath, testInputsPath); 50 | } 51 | 52 | @Test 53 | public void testHelloWorldJob() throws Exception { 54 | JobExecution jobExecution = jobLauncherTestUtils.launchJob(); 55 | assertThat(jobExecution.getExitStatus().getExitCode()) 56 | .isEqualTo("COMPLETED"); 57 | 58 | // check that all files are deleted 59 | File testInput = testInputsPath.toFile(); 60 | assertThat(testInput.list().length).isEqualTo(0); 61 | } 62 | 63 | @Configuration 64 | @Import({BatchConfig.class, CapitalizeNamesJobConfig.class}) 65 | static class BatchTestConfig { 66 | 67 | @Autowired 68 | private Job capitalizeNamesJob; 69 | 70 | @Bean 71 | JobLauncherTestUtils jobLauncherTestUtils() 72 | throws NoSuchJobException { 73 | JobLauncherTestUtils jobLauncherTestUtils = 74 | new JobLauncherTestUtils(); 75 | jobLauncherTestUtils.setJob(capitalizeNamesJob); 76 | 77 | return jobLauncherTestUtils; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /spring-batch-itemwriter/src/main/java/com/codenotfound/batch/job/ConvertNamesJobConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch.job; 2 | 3 | import org.springframework.batch.core.Job; 4 | import org.springframework.batch.core.Step; 5 | import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 6 | import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 7 | import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 8 | import org.springframework.batch.item.file.FlatFileItemReader; 9 | import org.springframework.batch.item.file.FlatFileItemWriter; 10 | import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; 11 | import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor; 12 | import org.springframework.batch.item.file.transform.DelimitedLineAggregator; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.context.annotation.Bean; 15 | import org.springframework.context.annotation.Configuration; 16 | import org.springframework.core.io.ClassPathResource; 17 | import org.springframework.core.io.FileSystemResource; 18 | import com.codenotfound.model.Person; 19 | 20 | @Configuration 21 | @EnableBatchProcessing 22 | public class ConvertNamesJobConfig { 23 | 24 | @Autowired 25 | public JobBuilderFactory jobBuilders; 26 | 27 | @Autowired 28 | public StepBuilderFactory stepBuilders; 29 | 30 | @Bean 31 | public Job convertNamesJob() { 32 | return jobBuilders.get("convertNamesJob").start(convertNamesStep()).build(); 33 | } 34 | 35 | @Bean 36 | public Step convertNamesStep() { 37 | return stepBuilders.get("convertNamesStep").chunk(10).reader(itemReader()) 38 | .processor(itemProcessor()).writer(itemWriter()).build(); 39 | } 40 | 41 | @Bean 42 | public FlatFileItemReader itemReader() { 43 | return new FlatFileItemReaderBuilder().name("personItemReader") 44 | .resource(new ClassPathResource("csv/persons.csv")).delimited() 45 | .names(new String[] {"firstName", "lastName"}).targetType(Person.class).build(); 46 | } 47 | 48 | @Bean 49 | public PersonNameProcessor itemProcessor() { 50 | return new PersonNameProcessor(); 51 | } 52 | 53 | @Bean 54 | public FlatFileItemWriter itemWriter() { 55 | BeanWrapperFieldExtractor fieldExtractor = new BeanWrapperFieldExtractor<>(); 56 | fieldExtractor.setNames(new String[] {"firstName", "lastName"}); 57 | fieldExtractor.afterPropertiesSet(); 58 | 59 | DelimitedLineAggregator lineAggregator = new DelimitedLineAggregator<>(); 60 | lineAggregator.setDelimiter(","); 61 | lineAggregator.setFieldExtractor(fieldExtractor); 62 | 63 | FlatFileItemWriter flatFileItemWriter = new FlatFileItemWriter<>(); 64 | flatFileItemWriter.setName("personItemWriter"); 65 | flatFileItemWriter.setResource(new FileSystemResource("target/test-outputs/persons.txt")); 66 | flatFileItemWriter.setLineAggregator(lineAggregator); 67 | 68 | return flatFileItemWriter; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /spring-batch-itemreader/src/main/java/com/codenotfound/batch/job/ConvertNamesJobConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch.job; 2 | 3 | import org.springframework.batch.core.Job; 4 | import org.springframework.batch.core.Step; 5 | import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 6 | import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 7 | import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 8 | import org.springframework.batch.item.file.FlatFileItemReader; 9 | import org.springframework.batch.item.file.FlatFileItemWriter; 10 | import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; 11 | import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper; 12 | import org.springframework.batch.item.file.mapping.DefaultLineMapper; 13 | import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; 14 | import org.springframework.batch.item.file.transform.PassThroughLineAggregator; 15 | import org.springframework.beans.factory.annotation.Autowired; 16 | import org.springframework.context.annotation.Bean; 17 | import org.springframework.context.annotation.Configuration; 18 | import org.springframework.core.io.ClassPathResource; 19 | import org.springframework.core.io.FileSystemResource; 20 | import com.codenotfound.model.Person; 21 | 22 | @Configuration 23 | @EnableBatchProcessing 24 | public class ConvertNamesJobConfig { 25 | 26 | @Autowired 27 | public JobBuilderFactory jobBuilders; 28 | 29 | @Autowired 30 | public StepBuilderFactory stepBuilders; 31 | 32 | @Bean 33 | public Job convertNamesJob() { 34 | return jobBuilders.get("convertNamesJob").start(convertNamesStep()).build(); 35 | } 36 | 37 | @Bean 38 | public Step convertNamesStep() { 39 | return stepBuilders.get("convertNamesStep").chunk(10).reader(itemReader()) 40 | .processor(itemProcessor()).writer(itemWriter()).build(); 41 | } 42 | 43 | @Bean 44 | public FlatFileItemReader itemReader() { 45 | BeanWrapperFieldSetMapper fieldSetMapper = new BeanWrapperFieldSetMapper<>(); 46 | fieldSetMapper.setTargetType(Person.class); 47 | 48 | DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer(); 49 | lineTokenizer.setDelimiter(","); 50 | lineTokenizer.setNames(new String[] {"firstName", "lastName"}); 51 | 52 | DefaultLineMapper lineMapper = new DefaultLineMapper<>(); 53 | lineMapper.setFieldSetMapper(fieldSetMapper); 54 | lineMapper.setLineTokenizer(lineTokenizer); 55 | 56 | FlatFileItemReader flatFileItemReader = new FlatFileItemReader(); 57 | flatFileItemReader.setName("personItemReader"); 58 | flatFileItemReader.setResource(new ClassPathResource("csv/persons.csv")); 59 | flatFileItemReader.setLineMapper(lineMapper); 60 | flatFileItemReader.setLinesToSkip(1); 61 | 62 | return flatFileItemReader; 63 | } 64 | 65 | @Bean 66 | public PersonNameProcessor itemProcessor() { 67 | return new PersonNameProcessor(); 68 | } 69 | 70 | @Bean 71 | public FlatFileItemWriter itemWriter() { 72 | return new FlatFileItemWriterBuilder().name("personItemWriter") 73 | .resource(new FileSystemResource("target/test-outputs/persons.txt")) 74 | .lineAggregator(new PassThroughLineAggregator<>()).build(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /spring-batch-admin/spring-batch-task/src/main/java/com/codenotfound/batch/CapitalizeNamesJobConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import org.springframework.batch.core.Job; 4 | import org.springframework.batch.core.Step; 5 | import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 6 | import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 7 | import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 8 | import org.springframework.batch.item.file.FlatFileItemReader; 9 | import org.springframework.batch.item.file.FlatFileItemWriter; 10 | import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper; 11 | import org.springframework.batch.item.file.mapping.DefaultLineMapper; 12 | import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor; 13 | import org.springframework.batch.item.file.transform.DelimitedLineAggregator; 14 | import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; 15 | import org.springframework.context.annotation.Bean; 16 | import org.springframework.context.annotation.Configuration; 17 | import org.springframework.core.io.ClassPathResource; 18 | import org.springframework.core.io.FileSystemResource; 19 | import com.codenotfound.model.Person; 20 | 21 | @Configuration 22 | @EnableBatchProcessing 23 | public class CapitalizeNamesJobConfig { 24 | 25 | @Bean 26 | public Job convertNamesJob(JobBuilderFactory jobBuilders, 27 | StepBuilderFactory stepBuilders) { 28 | return jobBuilders.get("capitalizeNamesJob") 29 | .start(convertNamesStep(stepBuilders)).build(); 30 | } 31 | 32 | @Bean 33 | public Step convertNamesStep(StepBuilderFactory stepBuilders) { 34 | return stepBuilders.get("capitalizeNamesStep") 35 | .chunk(10).reader(itemReader()) 36 | .processor(itemProcessor()).writer(itemWriter()).build(); 37 | } 38 | 39 | @Bean 40 | public FlatFileItemReader itemReader() { 41 | BeanWrapperFieldSetMapper fieldSetMapper = 42 | new BeanWrapperFieldSetMapper<>(); 43 | fieldSetMapper.setTargetType(Person.class); 44 | 45 | DelimitedLineTokenizer lineTokenizer = 46 | new DelimitedLineTokenizer(); 47 | lineTokenizer.setDelimiter(","); 48 | lineTokenizer.setNames(new String[] {"firstName", "lastName"}); 49 | 50 | DefaultLineMapper lineMapper = new DefaultLineMapper<>(); 51 | lineMapper.setFieldSetMapper(fieldSetMapper); 52 | lineMapper.setLineTokenizer(lineTokenizer); 53 | 54 | FlatFileItemReader flatFileItemReader = 55 | new FlatFileItemReader<>(); 56 | flatFileItemReader.setName("personItemReader"); 57 | flatFileItemReader 58 | .setResource(new ClassPathResource("csv/persons.csv")); 59 | flatFileItemReader.setLineMapper(lineMapper); 60 | 61 | return flatFileItemReader; 62 | } 63 | 64 | @Bean 65 | public PersonItemProcessor itemProcessor() { 66 | return new PersonItemProcessor(); 67 | } 68 | 69 | @Bean 70 | public FlatFileItemWriter itemWriter() { 71 | BeanWrapperFieldExtractor fieldExtractor = 72 | new BeanWrapperFieldExtractor<>(); 73 | fieldExtractor.setNames(new String[] {"firstName", "lastName"}); 74 | fieldExtractor.afterPropertiesSet(); 75 | 76 | DelimitedLineAggregator lineAggregator = 77 | new DelimitedLineAggregator<>(); 78 | lineAggregator.setDelimiter(","); 79 | lineAggregator.setFieldExtractor(fieldExtractor); 80 | 81 | FlatFileItemWriter flatFileItemWriter = 82 | new FlatFileItemWriter<>(); 83 | flatFileItemWriter.setName("personItemWriter"); 84 | flatFileItemWriter.setResource( 85 | new FileSystemResource("target/test-outputs/persons.txt")); 86 | flatFileItemWriter.setLineAggregator(lineAggregator); 87 | 88 | return flatFileItemWriter; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /spring-batch-tasklet/src/main/java/com/codenotfound/batch/CapitalizeNamesJobConfig.java: -------------------------------------------------------------------------------- 1 | package com.codenotfound.batch; 2 | 3 | import java.io.IOException; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | import org.springframework.batch.core.Job; 7 | import org.springframework.batch.core.Step; 8 | import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 9 | import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 10 | import org.springframework.batch.item.file.FlatFileItemReader; 11 | import org.springframework.batch.item.file.FlatFileItemWriter; 12 | import org.springframework.batch.item.file.MultiResourceItemReader; 13 | import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; 14 | import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; 15 | import org.springframework.batch.item.file.builder.MultiResourceItemReaderBuilder; 16 | import org.springframework.context.annotation.Bean; 17 | import org.springframework.context.annotation.Configuration; 18 | import org.springframework.core.io.FileSystemResource; 19 | import org.springframework.core.io.Resource; 20 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 21 | import org.springframework.core.io.support.ResourcePatternResolver; 22 | import com.codenotfound.model.Person; 23 | 24 | @Configuration 25 | public class CapitalizeNamesJobConfig { 26 | 27 | private static final Logger LOGGER = 28 | LoggerFactory.getLogger(CapitalizeNamesJobConfig.class); 29 | 30 | @Bean 31 | public Job capitalizeNamesJob(JobBuilderFactory jobBuilders, 32 | StepBuilderFactory stepBuilders) { 33 | return jobBuilders.get("capitalizeNamesJob") 34 | .start(capitalizeNamesStep(stepBuilders)) 35 | .next(deleteFilesStep(stepBuilders)).build(); 36 | } 37 | 38 | @Bean 39 | public Step capitalizeNamesStep(StepBuilderFactory stepBuilders) { 40 | return stepBuilders.get("capitalizeNamesStep") 41 | .chunk(10).reader(multiItemReader()) 42 | .processor(itemProcessor()).writer(itemWriter()).build(); 43 | } 44 | 45 | @Bean 46 | public Step deleteFilesStep(StepBuilderFactory stepBuilders) { 47 | return stepBuilders.get("deleteFilesStep") 48 | .tasklet(fileDeletingTasklet()).build(); 49 | } 50 | 51 | @Bean 52 | public MultiResourceItemReader multiItemReader() { 53 | ResourcePatternResolver patternResolver = 54 | new PathMatchingResourcePatternResolver(); 55 | Resource[] resources = null; 56 | try { 57 | resources = patternResolver 58 | .getResources("file:target/test-inputs/*.csv"); 59 | } catch (IOException e) { 60 | LOGGER.error("error reading files", e); 61 | } 62 | 63 | return new MultiResourceItemReaderBuilder() 64 | .name("multiPersonItemReader").delegate(itemReader()) 65 | .resources(resources).setStrict(true).build(); 66 | } 67 | 68 | @Bean 69 | public FlatFileItemReader itemReader() { 70 | return new FlatFileItemReaderBuilder() 71 | .name("personItemReader").delimited() 72 | .names(new String[] {"firstName", "lastName"}) 73 | .targetType(Person.class).build(); 74 | } 75 | 76 | @Bean 77 | public PersonItemProcessor itemProcessor() { 78 | return new PersonItemProcessor(); 79 | } 80 | 81 | @Bean 82 | public FlatFileItemWriter itemWriter() { 83 | return new FlatFileItemWriterBuilder() 84 | .name("personItemWriter") 85 | .resource(new FileSystemResource( 86 | "target/test-outputs/persons.txt")) 87 | .delimited().delimiter(", ") 88 | .names(new String[] {"firstName", "lastName"}).build(); 89 | } 90 | 91 | @Bean 92 | public FileDeletingTasklet fileDeletingTasklet() { 93 | return new FileDeletingTasklet( 94 | new FileSystemResource("target/test-inputs")); 95 | } 96 | } 97 | --------------------------------------------------------------------------------