Skip to content

JobExecutionDecider not working with custom ExitStatus #4345

Open
@vamsigangarapu

Description

@vamsigangarapu

Bug description
When we produce custom Exit status from a tasklet and use a JobExecutionDecider to decide the flow, we get an error.

Environment
Please provide as many details as possible: Spring Batch version, Java version, which database you use if any, etc
Spring Batch Version - 4.3.8
JDK - 11

Steps to reproduce
Send a custom Exit status from a taklet and use it in the JobExecutionDecider to decide the flow.

Expected behavior
The Tasklet should produce the status completed and the JobExecutionDecider should take the ExitStatus from the step and validate the status and decide on the flow.

Minimal Complete Reproducible example

@Bean
public Step initialStep() {
    return stepBuilderFactory.get("initialStep")
            .tasklet((contribution, chunkContext) -> {
                System.out.println("INTIAL STEP");
                return RepeatStatus.FINISHED;
            }).listener(stepExecutionListener()).build();
}

@Bean
public StepExecutionListener stepExecutionListener() {
    return new MyStepExecutionListener();
}

static class MyStepExecutionListener implements StepExecutionListener {

    @Override
    public void beforeStep(StepExecution stepExecution) {
        System.out.println("MyStepExecutionListener.beforeStep");
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        var inputFileName = stepExecution.getJobParameters().getString("inputFileName");
        System.out.println("MyStepExecutionListener.afterStep");
        if (inputFileName != null && (inputFileName.contains("abc"))) {
            return new ExitStatus("YES", "For processing step");
        }
        return new ExitStatus("NO", "For failed step");
    }
}

@Bean
public JobExecutionDecider decider() {
    return (jobExecution, stepExecution) -> {
        System.out.println(stepExecution.getExitStatus().getExitCode());
        if("YES".equals(stepExecution.getExitStatus().getExitCode())) {
            return new FlowExecutionStatus("YES");
        } else {
            return new FlowExecutionStatus("NO");
        }
    };
}

Stacktrace:

2023-04-03 15:41:42.865  INFO 17532 --- [  restartedMain] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=job]] launched with the following parameters: [{RunDate=2023-04-03T15:41:41.080044100}]
2023-04-03 15:41:44.656  INFO 17532 --- [  restartedMain] o.s.batch.core.job.SimpleStepHandler     : Executing step: [initialStep]
MyStepExecutionListener.beforeStep
INTIAL STEP
MyStepExecutionListener.afterStep
2023-04-03 15:42:04.078  INFO 17532 --- [  restartedMain] o.s.batch.core.step.AbstractStep         : Step: [initialStep] executed in 19s422ms
2023-04-03 15:42:04.868  INFO 17532 --- [  restartedMain] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=job]] completed with the following parameters: [{RunDate=2023-04-03T15:41:41.080044100}] and the following status: [FAILED] in 21s652ms

I am attaching the complete project as well to validate the issue.
demo.zip

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions