В вашем примере поток не определен, начиная с cleanup
. Вы должны уточнить, что с cleanup
поток должен продолжаться до echo
с использованием .from(cleanup).to(echo("End batch job")).end()
. Вот пример:
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 extractorStep() {
return steps.get("extractorStep")
.tasklet((contribution, chunkContext) -> {
System.out.println("extractorStep");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step filesProcessStep() {
return steps.get("filesProcessStep")
.tasklet((contribution, chunkContext) -> {
System.out.println("filesProcessStep");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step cleanupStep() {
return steps.get("cleanupStep")
.tasklet((contribution, chunkContext) -> {
System.out.println("cleanupStep");
return RepeatStatus.FINISHED;
})
.build();
}
public Step echo(String message) {
return steps.get("echo-" + message)
.tasklet((contribution, chunkContext) -> {
System.out.println(message);
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Job job() {
Step start = echo("Starting batch job");
Step extract = extractorStep();
Step process = filesProcessStep();
Step cleanup = cleanupStep();
Step stop = echo("End batch job");
return jobs.get("job")
.start(start).on("*").to(extract)
.from(extract).on(ExitStatus.FAILED.getExitCode()).to(cleanup)
.from(extract).on("*").to(process)
.from(process).on("*").to(cleanup)
.from(cleanup).next(stop)
.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());
}
}
печатает:
Starting batch job
extractorStep
filesProcessStep
cleanupStep
End batch job
в случае сбоя extractorStep
, например:
@Bean
public Step extractorStep() {
return steps.get("extractorStep")
.tasklet((contribution, chunkContext) -> {
System.out.println("extractorStep");
chunkContext.getStepContext().getStepExecution().setExitStatus(ExitStatus.FAILED);
return RepeatStatus.FINISHED;
})
.build();
}
поток пропустит filesProcessStep
и перейдет к очистке:
Starting batch job
extractorStep
cleanupStep
End batch job
Надеюсь, это поможет.