问题
I am working on Spring Boot and Batch Project and using spring Batch decider.
Error
The method next(Step) is undefined for the type FlowJobBuilder
return jobBuilderFactory.get("sampleJob")
.incrementer(new RunIdIncrementer())
.start(abcStep(stepBuilderFactory, abcReader))
.next(sampleDecider())
.from(sampleDecider())
.on(MDSConst.SUCCESS).to(xyxStep(stepBuilderFactory, xyzReader))
.from(sampleDecider())
.on(MDSConst.FAILED).end().build()
.next(mnoStep(stepBuilderFactory, mnoReder))
.build();
- If sampleDecider gives SUCCESS, xyxStep should execute
- If sampleDecider gives FAILED, batch Job should stop
- If sampleDecider gives SUCCESS, xyxStep should execute and then mnoStep should execute
回答1:
I hope this is the write way of doing things even for the chunk based Steps.
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.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
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("hello");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public JobExecutionDecider decider() {
return (jobExecution, stepExecution) -> new FlowExecutionStatus("YES"); // or NO
}
@Bean
public Step step2() {
return steps.get("step2")
.tasklet((contribution, chunkContext) -> {
System.out.println("world");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step step3() {
return steps.get("step3")
.tasklet((contribution, chunkContext) -> {
System.out.println("!!");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step step5() {
return steps.get("step5")
.tasklet((contribution, chunkContext) -> {
System.out.println("Step 5");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step step4() {
return steps.get("step4")
.tasklet((contribution, chunkContext) -> {
System.out.println("Step 4");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Job job() {
return jobs.get("job")
.incrementer(new RunIdIncrementer())
.start(step1())
.next(decider())
.on("YES").to(step2())
.next(step4())
.next(step5())
.from(decider())
.on("NO").to(step3())
.end()
.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());
}
}
来源:https://stackoverflow.com/questions/64678776/the-method-nextstep-is-undefined-for-the-type-flowjobbuilder