Home > Mobile >  Conditional Flow not working in Java Spring Batch
Conditional Flow not working in Java Spring Batch

Time:04-12

I'm new to Spring batch, I want to make conditional flow like this :

  1. Run Step 1
  2. Step 1 return one of four options of flows (A, B, C, D)
    • Option A will run only step 2 then end the batch.
    • Option B will run only step 3 then end the batch.
    • Option C will run only step 4, 5 then end the batch.
    • Option D will run only step 4, 6 then end the batch.

Here are the flow illustration. Flow

What I'm doing right now is like this. Build Job

    public Job buildJob() {
        return getAdhocJobBuilder()
                .start(step1()).on(ExitStatus.STOPPED.getExitCode()).end()
                .on("OPTION_A").to(step2())
                .on("OPTION_B").to(step3())
                .on("OPTION_C").to(step4()).next(step5())
                .on("OPTION_D").to(step4()).next(step6())
                .build()
                .build();
    }

BatchJobStepListener

@Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return new ExitStatus(stepExecution.getExitStatus().getExitCode());
    }

Step1

public class Step1Tasklet extends BaseTasklet {

    @Override
    protected RepeatStatus doExecute() {
       
                if (conditionA) {
                    chunkContext.getStepContext().getStepExecution().setExitStatus(new ExitStatus("OPTION_A"));
                } else if(conditionB) {
                    chunkContext.getStepContext().getStepExecution().setExitStatus(new ExitStatus("OPTION_B"));
                }else if(conditionC) {
                    chunkContext.getStepContext().getStepExecution().setExitStatus(new ExitStatus("OPTION_C"));
                }else if(conditionD) {
                    chunkContext.getStepContext().getStepExecution().setExitStatus(new ExitStatus("OPTION_D"));
                }
        return RepeatStatus.FINISHED;
    }

}

When I run the batch, it's executing the Step1 just fine, and go through conditionA, so it should trigger OPTION_A, but after the step one is executed, it's not executing the next step defined, and the batch is shutting down.

CodePudding user response:

Try using if statements only dont use else.

CodePudding user response:

I think the problem is in if-elseif statement. use if statement for all the condition. that might work.

  • Related