spring-projects / spring-projects/spring-batch

JobExecutionDecider not working with custom ExitStatus

Open
#4,345 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

has: minimal-example in: core related-to: flow-definition type: bug
Dominant language
Java
Stars
3k
Forks
2.5k
Avg merge
6d 53m
Merged PRs (30d)
3

Description

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal reproducer in the issue and trace how the custom ExitStatus returned by MyStepExecutionListener.afterStep reaches the JobExecutionDecider. Compare the step's exit code with the decider's flow status and verify that the expected YES or NO branch completes without the reported failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.