Repository navigation
Description
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
- @BeforeStep/@AfterStep methods are ignored by ChunkOrientedStepBuilder#listener(Object) #5451 —
@BeforeStep/@AfterStepignored byChunkOrientedStepBuilder#listener(Object): different code path, same class. - Step listeners detected and invoked twice [BATCH-1493] #2077 (BATCH-1493) — the same "registered twice, invoked twice" symptom via manual registration, fixed in 2.x.
Activity
Fix duplicate listener registration in ChunkOrientedStepBuilder
n-dlms commentedon Aug 4, 2026
I investigated this issue. The root cause is in
ChunkOrientedStepBuilder#addAsStreamAndListener: theinstanceof StepListenerbranch and theStepListenerFactoryBean.isListenerbranch are not mutually exclusive. When an annotation is present,StepListenerFactoryBean.getListenerreturns 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 toChunkOrientedStepTests. Opening a PR shortly.Fix duplicate listener registration in ChunkOrientedStepBuilder
Fix duplicate listener registration in ChunkOrientedStepBuilder
Fix duplicate listener registration in ChunkOrientedStepBuilder