Skip to content

Interface listener callbacks are invoked twice when an item handler also has a listener annotation #5466

Closed

Description

@benelog

Bug description

If an ItemReader, ItemProcessor, or ItemWriter implements a StepListener sub-interface (e.g. ItemProcessListener) and also has a method annotated with a listener annotation (e.g. @BeforeStep), the object is registered as a listener twice, so every interface callback fires twice per item. The annotated method itself fires once; only the interface callbacks are doubled.

ChunkOrientedStepBuilder#addAsStreamAndListener has two non-exclusive branches: instanceof StepListener adds the raw object, and StepListenerFactoryBean.isListener(...) adds a proxy. Since AbstractListenerFactoryBean#getObject resolves callbacks by interface as well as by annotation, the proxy delegates the same interface callbacks back to the same target.

This is a regression from 5.2.x, where SimpleStepBuilder#registerAsStreamsAndListeners used a single StepListenerFactoryBean.isListener(...) branch, so the object was registered once. It fails silently: a listener that accumulates state (counters, files) produces doubled results while the job still completes successfully.

Environment

Spring Batch 6.0.4 (Spring Boot 4.1.0), Java 17+, no database. Still present on main.

Steps to reproduce

Run the reproducer below (./gradlew bootRun). Two identical jobs run: one processor only implements ItemProcessListener, the other is the same processor plus one @BeforeStep method.

without a listener annotation = [beforeProcess:1, afterProcess:1, beforeProcess:2, afterProcess:2]
with @BeforeStep added        = [beforeStep, beforeProcess:1, beforeProcess:1, afterProcess:1, afterProcess:1, beforeProcess:2, beforeProcess:2, afterProcess:2, afterProcess:2]

./gradlew test asserts the same: callbacksFireOnceWhenOnlyTheInterfaceIsImplemented passes, callbacksShouldAlsoFireOnceWhenAListenerAnnotationIsAdded fails.

Expected behavior

Adding an annotated listener method must not change how the interface callbacks are invoked:

with @BeforeStep added        = [beforeStep, beforeProcess:1, afterProcess:1, beforeProcess:2, afterProcess:2]

Minimal Complete Reproducible example

https://github.com/benelog/spring-batch-practice/tree/main/issue-duplicate-listener

Suggested fix

Make the two branches mutually exclusive, as in 5.2.x. StepListenerFactoryBean.isListener already returns true for interface-only implementations, so the factory branch covers both cases:

if (StepListenerFactoryBean.isListener(itemHandler)) {
    this.stepListeners.add(StepListenerFactoryBean.getListener(itemHandler));
}
else if (itemHandler instanceof StepListener listener) {
    this.stepListeners.add(listener);
}

Related

Activity

  1. added a commit that references this issue on Jul 23, 2026
    d48e5df
  2. n-dlms commented on Aug 4, 2026

    @n-dlms

    I investigated this issue. The root cause is in ChunkOrientedStepBuilder#addAsStreamAndListener: the instanceof StepListener branch and the StepListenerFactoryBean.isListener branch are not mutually exclusive. When an annotation is present, StepListenerFactoryBean.getListener returns a proxy (a different instance than the raw object), so both the raw object and the proxy are registered, causing interface callbacks to fire twice.

    I have prepared a fix that makes the branches mutually exclusive (factory branch first, falling back to instanceof StepListener) and added two regression tests to ChunkOrientedStepTests. Opening a PR shortly.

  3. added a commit that references this issue on Aug 4, 2026
    4f527b7
  4. added this to the 6.0.6 milestone on Aug 13, 2026
  5. added a commit that references this issue on Aug 26, 2026
    2708101
  6. added a commit that references this issue on Aug 27, 2026
    188ffe0
  7. added
    for: backport-to-6.0.x
    on Sep 29, 2026
  8. modified the milestones: 6.0.6, 6.1.0-M2 on Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: backport-to-6.0.xin: coretype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions