在tasklet中返回RepeatStatus.CONTINUABLE只是单纯的重复执行该Step,那么如何重复执行不同的Step?

    pom.xml

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0</modelVersion>
    4. <parent>
    5. <groupId>org.springframework.boot</groupId>
    6. <artifactId>spring-boot-starter-parent</artifactId>
    7. <version>2.1.6.RELEASE</version>
    8. <relativePath /> <!-- lookup parent from repository -->
    9. </parent>
    10. <groupId>kagami</groupId>
    11. <artifactId>springbatchsample</artifactId>
    12. <version>0.0.1-SNAPSHOT</version>
    13. <packaging>jar</packaging>
    14. <name>springbatchsample</name>
    15. <url>http://maven.apache.org</url>
    16. <properties>
    17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    18. <maven.compiler.source>1.8</maven.compiler.source>
    19. <maven.compiler.target>1.8</maven.compiler.target>
    20. <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version><!-- https://qiita.com/kagamihoge/items/fbfe90837192fd88fee9 -->
    21. </properties>
    22. <dependencies>
    23. <dependency>
    24. <groupId>org.springframework.boot</groupId>
    25. <artifactId>spring-boot-starter-batch</artifactId>
    26. </dependency>
    27. <dependency>
    28. <groupId>com.h2database</groupId>
    29. <artifactId>h2</artifactId>
    30. <scope>runtime</scope>
    31. </dependency>
    32. <dependency>
    33. <groupId>org.springframework.boot</groupId>
    34. <artifactId>spring-boot-starter-test</artifactId>
    35. <scope>test</scope>
    36. </dependency>
    37. <dependency>
    38. <groupId>org.springframework.batch</groupId>
    39. <artifactId>spring-batch-test</artifactId>
    40. <scope>test</scope>
    41. </dependency>
    42. </dependencies>
    43. </project>

    App.java

    1. package kagami.springbatchsample.deciders;
    2. import org.springframework.batch.core.Job;
    3. import org.springframework.batch.core.JobExecution;
    4. import org.springframework.batch.core.Step;
    5. import org.springframework.batch.core.StepExecution;
    6. import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
    7. import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
    8. import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
    9. import org.springframework.batch.core.job.flow.FlowExecutionStatus;
    10. import org.springframework.batch.core.job.flow.JobExecutionDecider;
    11. import org.springframework.batch.core.launch.support.RunIdIncrementer;
    12. import org.springframework.batch.repeat.RepeatStatus;
    13. import org.springframework.beans.factory.annotation.Qualifier;
    14. import org.springframework.boot.SpringApplication;
    15. import org.springframework.boot.autoconfigure.SpringBootApplication;
    16. import org.springframework.context.annotation.Bean;
    17. @EnableBatchProcessing
    18. @SpringBootApplication
    19. public class App {
    20. @Bean
    21. public Job job(JobBuilderFactory jobs, @Qualifier("s1") Step s1, JobExecutionDecider decider) {
    22. return jobs
    23. .get("myJob")
    24. .incrementer(new RunIdIncrementer())
    25. .start(s1)
    26. .next(decider)
    27. .on("COMPLETED").end()
    28. .on("CONTINUE").to(s1)
    29. .end()
    30. .build();
    31. }
    32. @Bean(name = "s1")
    33. public Step step1(StepBuilderFactory steps) {
    34. return steps.get("step1").tasklet((stepContribution, chunkContext) -> {
    35. System.out.println("step 1");
    36. return RepeatStatus.FINISHED;
    37. }).build();
    38. }
    39. @Bean
    40. public JobExecutionDecider decider() {
    41. return new JobExecutionDecider() {
    42. int count = 0;
    43. @Override
    44. public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
    45. if (++count >= 5) {
    46. return new FlowExecutionStatus("COMPLETED");
    47. } else {
    48. return new FlowExecutionStatus("CONTINUE");
    49. }
    50. }
    51. };
    52. }
    53. public static void main(String[] args) {
    54. SpringApplication.run(App.class, args);
    55. }
    56. }

    By using JobExecutionDecider for spring-batch, execute next stepYou can implement the logic to determine. In the sample code above, count corresponds to that logic.

    Then use the implementation of JobExecutionDecider in thejob 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.

    1. 2019-06-20 11:50:20.305 INFO 10084 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
    2. step 1
    3. 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.
    4. 2019-06-20 11:50:20.349 INFO 10084 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
    5. step 1
    6. 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.
    7. 2019-06-20 11:50:20.358 INFO 10084 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
    8. step 1
    9. 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.
    10. 2019-06-20 11:50:20.368 INFO 10084 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
    11. step 1
    12. 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.
    13. 2019-06-20 11:50:20.377 INFO 10084 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
    14. 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.