Created
August 6, 2021 08:12
-
-
Save fmbenhassine/e51516e63580e505bc6610e8d3e61523 to your computer and use it in GitHub Desktop.
#SpringBatch https://stackoverflow.com/questions/68670670
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
prints: