Skip to content

Instantly share code, notes, and snippets.

@fmbenhassine
Created August 6, 2021 08:12
Show Gist options
  • Save fmbenhassine/e51516e63580e505bc6610e8d3e61523 to your computer and use it in GitHub Desktop.
Save fmbenhassine/e51516e63580e505bc6610e8d3e61523 to your computer and use it in GitHub Desktop.
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
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.launch.JobLauncher;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class MyJob {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
public Step step1() {
return steps.get("step1")
.tasklet((contribution, chunkContext) -> {
System.out.println("step1");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step step2() {
return steps.get("step2")
.tasklet((contribution, chunkContext) -> {
System.out.println("step2");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step step3() {
return steps.get("step3")
.tasklet((contribution, chunkContext) -> {
System.out.println("step3");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Job job() {
return jobs.get("job")
.start(step1())
.on(ExitStatus.FAILED.getExitCode()).fail()
.on(ExitStatus.COMPLETED.getExitCode()).to(step2())
.from(step2())
.on(ExitStatus.FAILED.getExitCode()).fail()
.on(ExitStatus.COMPLETED.getExitCode()).to(step3())
.from(step3())
.on(ExitStatus.FAILED.getExitCode()).fail()
.on(ExitStatus.COMPLETED.getExitCode()).end()
.build()
.build();
}
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
}
}
@fmbenhassine
Copy link
Author

prints:

step1
step2
step3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment