spring-projects / spring-projects/spring-batch

[Spring-batch Documentation - Configuring for Stop] description is not correct

Open
#4,704 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: feature
Dominant language
Java
Stars
3k
Forks
2.5k
Avg merge
6d 53m
Merged PRs (30d)
3

Description

Bug description

I am studying Spring Batch using the official documentation. While practicing, I came across a statement explaining that if a step’s ExitStatus is FAILED in a job without a Flow, the job’s Status will also be FAILED.

msedge_B7gIVLyVk0

However, in my tests, even when I returned ExitStatus.FAILED in the afterStep method of a StepExecutionListener, the job’s status was still COMPLETE, contrary to the documentation.


Environment

  • OS : window 10
  • jdk : temurin-jdk-21
  • spring boot:3.3.4, spring batch core:5.1.2

Steps to reproduce


import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import java.util.List;

@Configuration
@RequiredArgsConstructor
public class MyJobConfig {
    private final JobRepository jobRepository;
    private final PlatformTransactionManager transactionManager;

    @Bean
    public Job customItemStreamConfigJob() {
        return new JobBuilder("customItemStreamConfigJob", jobRepository)
                .start(customItemStreamConfigStep())
                .build();
    }

    @Bean
    public Step customItemStreamConfigStep() {
        return new StepBuilder("customItemStreamConfigStep", jobRepository)
                .<Integer, Integer>chunk(5, transactionManager)
                .reader(new ListItemReader<>(List.of())) // give empty List to see EmptyReadFailureListener works properly
                .writer(chunk -> chunk.getItems().forEach(System.out::println))
                .listener(emptyReadFailureListener())
                .build();
    }

    @Bean
    public EmptyReadFailureListener emptyReadFailureListener() {
        return new EmptyReadFailureListener();
    }
}

The stepExecutionListener( = EmptyReadFailureListener ) code is like below.

package coding.toast.batch.job.simple;

import org.springframework.batch.core.*;

public class EmptyReadFailureListener implements StepExecutionListener {
    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
       // if there is nothing read from itemReader, Then return ExitStatus.FAILED.
        if (stepExecution.getReadCount() > 0) {
            return stepExecution.getExitStatus();
        } else {
            return ExitStatus.FAILED.addExitDescription("exit Because Of No Item Read");
        }
    }
}

Expected behavior

the job's batch status will be fail.


Test Result

But the Result was different.
the picture under this comment is the result i get after the Spring Batch Execution Ends.

batch_job_execution table:

idea64_utUIMGv1k4

batch_step_execution table:

idea64_k70hFdUqXo

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 by reviewing the documented stop configuration statement and run the supplied Spring Batch reproduction with the empty ListItemReader and EmptyReadFailureListener. Compare the resulting job and step statuses with the documentation, then correct the documentation so the stated behavior matches the verified result.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.