Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
CodingToastBread opened this issue Nov 12, 2024 · 0 comments
Labels
status: waiting-for-triage Issues that we did not analyse yet type: feature

Comments

@CodingToastBread
Copy link

CodingToastBread commented Nov 12, 2024

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

@CodingToastBread CodingToastBread added status: waiting-for-triage Issues that we did not analyse yet type: feature labels Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage Issues that we did not analyse yet type: feature
Projects
None yet
Development

No branches or pull requests

1 participant