3

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

  • Although I don't know about the SimpleAsyncTaskExecutor, I only found these alternatives: [flows](http://stackoverflow.com/a/32362705/2375586) and [workarounds](http://stackoverflow.com/a/33267011/2375586) – jmmut Oct 26 '15 at 14:10

1 Answers1

0

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.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98