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

ItemReadListener not being correctly registered after adding a StepExecutionListener #773

Closed
spring-projects-issues opened this issue Sep 4, 2019 · 1 comment

Comments

@spring-projects-issues
Copy link
Collaborator

Benne Otten opened BATCH-2841 and commented

I've stumbled upon this bug and it gave me quite a headache.
Consider the following Step configuration

@Bean
public Step step1() {
    return steps.get("step1")
        .<SomeDto, SomeDto>chunk(10)
        .reader(reader())
        .processor(processor())
        .writer(writer())
        .listener(stepExecutionListener())
        .listener(itemReadListener())
        .build();
}

I would expect the last listener to register the ItemListener correctly, using the listener(ItemReadLiistener listener) method in the SimpleStepBuilder class, but the listener(Object listener) method in the AbstractTaskletStepBuilder gets invoked instead.

The reason for this is that the listener(StepExecutionListener listener) method resides in the StepBuilderHelper class, and that method returns an AbstractTaskletStepBuilder, and not a SimpleStepBuilder, what it should really return. In other words, the inheritance of SimpleStepBuilder > AbstractTaskletStepBuilder > StepBuilderHelper is messed up.

To illustrate, consider the following tweaked example...

@Bean
public Step step1() {
    return steps.get("step1")
        .<SomeDto, SomeDto>chunk(10)
        .reader(reader())
        .processor(processor())
        .writer(writer())
        .listener(stepExecutionListener())
        .listener(itemReadListener())
        .listener(itemReadListener())
        .build();
}

Here, the first listener(itemReadListener) gets invoked in the AbstractTaskletStepBuilder class (which does nothing for an ItemReadListener) and the second listener(itemReadListener) gets invoked in the SimpleStepBuilder class (as it should).


No further details from BATCH-2841

@spring-projects-issues
Copy link
Collaborator Author

Benne Otten commented

I've resolved this issue myself by annotating the method on my StepExecutionListener with @BeforeStep and @AfterStep, and removing the implements StepExecution from the class declaration.
I've done thesame for my ItemReaderListener and ItemWriterListener and annotated the method with @OnReadError and @OnWriteError. They're no longet implementations of ItemReadListener and ItemWriteListener.
I've only found out I could do this by inspecting the source code though. I hadn't seen it in the docs.
I still believe this is a bug though. I think the methods in the StepBuilderHelper should return a SimpleStepBuilder and not an AbstractTaskletStepBuilder

@spring-projects-issues spring-projects-issues added type: bug status: waiting-for-triage Issues that we did not analyse yet labels Dec 16, 2019
@fmbenhassine fmbenhassine added in: core and removed status: waiting-for-triage Issues that we did not analyse yet labels Jul 19, 2022
@fmbenhassine fmbenhassine added this to the 5.0.0-M4 milestone Jul 19, 2022
@fmbenhassine fmbenhassine changed the title ItemReadListener not being correctly registered after adding a StepExecutionListener [BATCH-2841] ItemReadListener not being correctly registered after adding a StepExecutionListener Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants