Loop step in Spring Batch

As an image, I want to loop `step``` with a for statement. How to do this with the `job``` definition of spring-batch.

Source code

pom.xml

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<groupId>kagami</groupId>
	<artifactId>springbatchsample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springbatchsample</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version><!-- https://qiita.com/kagamihoge/items/fbfe90837192fd88fee9 -->
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-batch</artifactId>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.batch</groupId>
			<artifactId>spring-batch-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

App

package kagami.springbatchsample.deciders;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@EnableBatchProcessing
@SpringBootApplication
public class App {

  @Bean
  public Job job(JobBuilderFactory jobs, @Qualifier("s1") Step s1, JobExecutionDecider decider) {
    return jobs
        .get("myJob")
        .incrementer(new RunIdIncrementer())
        .start(s1)
        .next(decider)
          .on("COMPLETED").end()
          .on("CONTINUE").to(s1)
         .end()
        .build();
  }

  @Bean(name = "s1")
  public Step step1(StepBuilderFactory steps) {
    return steps.get("step1").tasklet((stepContribution, chunkContext) -> {
      System.out.println("step 1");
      return RepeatStatus.FINISHED;
    }).build();
  }
  
  @Bean
  public JobExecutionDecider decider() {
    return new JobExecutionDecider() {
      int count = 0;
      @Override
      public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
        if (++count >= 5) {
          return new FlowExecutionStatus("COMPLETED");
        } else {
          return new FlowExecutionStatus("CONTINUE");
        }
      }
    };
  }

  public static void main(String[] args) {
    SpringApplication.run(App.class, args);
  }

}

By using JobExecutionDecider for spring-batch, execute next `step` You can implement the logic to determine. In the sample code above, count corresponds to that logic. `` `JobExecutionDecider``` is [here](https://www.omotenashi-mind.com/index.php?title=SpringBatch%EF%BC%9A%E3%83%AB%E3%83%BC% E3% 83% 97% E5% 87% A6% E7% 90% 86% E3% 82% 92% E5% BF% 9C% E7% 94% A8% E3% 81% 97% E3% 81% 9F% E3% 82% B5% E3% 83% 96% E3% 83% 95% E3% 83% AD% E3% 83% BC% E5% AE% 9F% E7% 8F% BE% E6% 96% B9% E6% B3% I implemented it with reference to 95).

Then use the implementation of `JobExecutionDecider``` in the` `job``` definition. In the sample code above, if the return value of ``` JobExecutionDecider``` is " COMPLETED "```, it ends, and if it is `` " CONTINUE ", then `` `step Is executed. As a result, `step``` is repeatedly executed until the condition of `count``` is satisfied.

Below is the run-time log.

2019-06-20 11:50:20.305  INFO 10084 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
step 1
2019-06-20 11:50:20.347  INFO 10084 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Duplicate step [step1] detected in execution of job=[myJob]. If either step fails, both will be executed again on restart.
2019-06-20 11:50:20.349  INFO 10084 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
step 1
2019-06-20 11:50:20.356  INFO 10084 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Duplicate step [step1] detected in execution of job=[myJob]. If either step fails, both will be executed again on restart.
2019-06-20 11:50:20.358  INFO 10084 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
step 1
2019-06-20 11:50:20.366  INFO 10084 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Duplicate step [step1] detected in execution of job=[myJob]. If either step fails, both will be executed again on restart.
2019-06-20 11:50:20.368  INFO 10084 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
step 1
2019-06-20 11:50:20.375  INFO 10084 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Duplicate step [step1] detected in execution of job=[myJob]. If either step fails, both will be executed again on restart.
2019-06-20 11:50:20.377  INFO 10084 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
step 1

Duplicate step [step1] detected ...about. As you can see in the log, the same step is used multiple times in a single job, and if either step fails, both will be executed at restart. Since this is a phenomenon, it seems to mean that you should consider it when restarting.



# References
* https://www.omotenashi-mind.com/index.php?title=SpringBatch%EF%BC%9A%E3%83%AB%E3%83%BC%E3%83%97%E5%87%A6%E7%90%86%E3%82%92%E5%BF%9C%E7%94%A8%E3%81%97%E3%81%9F%E3%82%B5%E3%83%96%E3%83%95%E3%83%AD%E3%83%BC%E5%AE%9F%E7%8F%BE%E6%96%B9%E6%B3%95


Recommended Posts

Loop step in Spring Batch
[Tutorial] Spring Batch
Summary of what I learned in Spring Batch
Inject Logger in Spring
Use Interceptor in Spring
I tried Spring Batch
Microservices in Spring Cloud
Get cookies in Spring
How to use CommandLineRunner in Spring Batch of Spring Boot
When Spring Batch is executed continuously in Oracle, ORA-08177
How to use In-Memory Job repository in Spring Batch
[Spring Batch] Realization verification of chunk step and tasklet step of Spring Batch
Set context-param in Spring Boot
Spring Batch job launch parameters
Spring Boot 2 multi-project in Gradle
Error in Spring database connection
NoHttpResponseException in Spring Boot + WireMock
How to use Lombok in Spring
Call Chain from Chain in Spring Integration
Spring Boot Hello World in Eclipse
Spring Boot application development in Eclipse
Java Spring environment in vs Code
Write test code in Spring Boot
Run Java application in Azure Batch
I participated in JJUG CCC 2019 Spring
Implement reCAPTCHA v3 in Java / Spring
Implement REST API in Spring Boot
What is @Autowired in Spring boot?
Implement Spring Boot application in Gradle
Thymeleaf usage notes in Spring Boot