springspring-bootspring-batchspring-batch-tasklet

Spring batch - conditional step flow for Chunk model


I have two step where, step 2 should be skipped if the step 1 processor doesn't returned any item after filtration. I see ItemListenerSupport can be extended and after process can be utilized.

@Override
public void afterProcess(NumberInfo item, Integer result) {
    super.afterProcess(item, result);
    if (item.isPositive()) {
        stepExecution.setExitStatus(new ExitStatus(NOTIFY));
    }
}

My processing is chunk based, I want to set the exit status after all chunks are processed and if any item left unfiltered. I am current adding items left unfiltered to ExecutionContext and utilizing in next step.

How would i prevent next step if all the items of all chunks are filtered out


Solution

  • For programmatic decisions, you can use a JobExecutionDecider. This API gives you access to the StepExecution of the previous step, so you can base the decision to run the next step on any information from the last step execution and its execution context. In your case, it could be the filter count or anything meaningful to your decision that you store in the execution context upfront.

    You can find more details about this API and some code examples in the Programmatic Flow Decisions section of the reference documentation.