问题
I have 3 steps A, B, C which should execute in the sequence A->B->C where B is optional. I've to execute step B only based on some condition. I'm using JobExecutionDecider to decide as follows:
@Bean(name = "decider")
JobExecutionDecider isStepRequired {
return new JobExecutionDecider() {
@Override
public FlowExecutionStatus decide(final JobExecution jobExecution, final StepExecution stepExecution) {
if (condition not satisfied) {
// return status to skip step B and go to step C
return FlowExecutionStatus.COMPLETED;
}
// return status to proceed with step B
return new FlowExecutionStatus("CONTINUE");
}
};
}
and in the job configuration, i have the following snippet,
@Bean
Job constructJob(final JobBuilderFactory jobs, final Step a, final Step b, final JobExecutionDecider decider, final Step c) {
final JobBuilder jobBuilder = jobs.get("Job");
final JobFlowBuilder builder = jobBuilder.flow(a);
builder.from(a).next(decider);
builder.from(decider).on("CONTINUE").to(b).next(c);
builder.from(decider).on("*").to(c);
return builder.build().build();
and the above mentioned code is working as i expected. But I'm not sure whether this is the right way of doing it. Basically I'm expecting a way not to repeat the step C execution.
I did come across SimpleAsyncTaskExecutor but i understood that it is used in a scenario where we need to do parallel processing and in my case i just have to execute a step if the condition satisfies.
My questions are 1. Can I achieve what I want by using SimpleAsyncTaskExecutor? Is there any example of using SimpleAsyncTaskExecutor using annotation? 2. Is there any other better way of doing what I've done where I can avoid the above mentioned duplication?
Any help is really appreciated!
Thanks in advance, Dhinesh Kumar P
回答1:
I am not sure as how your code is working correctly - as it seems to me builder.from(decider).on("*").to(c);
will create duplicate executions for Step C.
After STEP - A finished executing ( in whatever status ), decider is called and if decider returns CONTINUE
- you execute STEP - B and then STEP - C . If that decider doesn't return CONTINUE
, STEP- C is still executed since that is not conditional in line - builder.from(decider).on("CONTINUE").to(b).next(c);
So STEP - C has already executed then you call decider again and execute STEP - C again as per line - builder.from(decider).on("*").to(c);
. You have to also notice that final StepExecution stepExecution
parameter would be step C when decider is called this time & not STEP - B.
Let me know if I misunderstood anything.
Also - SimpleAsyncTaskExecutor
is not meant to build job flows. Its purpose is to introduce multi threading / parallel processing in your job when you are trying to execute chunks in parallel or trying to execute partitioned steps in parallel.
来源:https://stackoverflow.com/questions/32527711/spring-batch-java-config-identifying-and-executing-step-using-jobexecutiondeci