Repository navigation
Description
Bug description
@OnChunkError is never invoked on a chunk-oriented step.
ChunkOrientedStepBuilder#listener(Object) scans for @OnChunkError and builds a listener proxy for it, but StepListenerMetaData has no entry for that annotation. The proxy is registered and the callback is never wired, so the method is silently skipped. No exception is thrown and no warning is logged.
@BeforeChunk and @AfterChunk on the same class work, which makes the gap easy to miss.
Environment
- Spring Batch 6.0.4
- Spring Boot 4.1.0
- Java 17
Steps to reproduce
Minimal reproducer: https://github.com/benelog/spring-batch-practice/tree/main/issue-chunk-error-listener
public class AnnotatedChunkListener {
@BeforeChunk
public void beforeChunk(Chunk<Integer> chunk) { ... }
@AfterChunk
public void afterChunk(Chunk<Integer> chunk) { ... }
@OnChunkError // never called
public void onChunkError(Exception exception, Chunk<Integer> chunk) { ... }
}The listener is registered with listener(Object) on a chunk-oriented step whose writer throws on the second chunk.
./gradlew bootRunActual output:
[DIAG] ChunkListener interface = [interface:beforeChunk, interface:afterChunk, interface:beforeChunk, interface:onChunkError]
[DIAG] @OnChunkError = [annotation:beforeChunk, annotation:afterChunk, annotation:beforeChunk]
[DIAG] @AfterChunkError (5.x) = []
Expected behavior
[DIAG] @OnChunkError = [annotation:beforeChunk, annotation:afterChunk, annotation:beforeChunk, annotation:onChunkError]
The same expectation is asserted as a JUnit test in the reproducer (./gradlew test): the two control tests pass and the two tests for this issue fail.
Root cause
StepListenerMetaData (6.0.4) has no OnChunkError constant. AFTER_CHUNK_ERROR still maps to the deprecated ChunkListener#afterChunkError(ChunkContext):
BEFORE_CHUNK("beforeChunk", "before-chunk-method", BeforeChunk.class, ChunkListener.class, Chunk.class),
AFTER_CHUNK("afterChunk", "after-chunk-method", AfterChunk.class, ChunkListener.class, Chunk.class),
AFTER_CHUNK_ERROR("afterChunkError", "after-chunk-error-method", AfterChunkError.class, ChunkListener.class,
ChunkContext.class),
// no entry for OnChunkErrorAbstractListenerFactoryBean#getObject iterates over these constants, so the proxy it returns has nothing bound to onChunkError(Exception, Chunk). ChunkOrientedStep calls only onChunkError(Exception, Chunk); the deprecated afterChunkError(ChunkContext) is called by TaskletStep alone.
@BeforeChunk / @AfterChunk were migrated to the Chunk signature in #4961, and @OnChunkError was added in 6.0 with a javadoc that documents void onChunkError(Exception, Chunk). The metadata entry appears to have been left out of both changes.
@AfterChunkError is a separate, smaller gap: ChunkOrientedStepBuilder#listener(Object) does not scan for it at all, so a class carrying only that annotation registers nothing.
Suggested fix
Add the missing constant:
ON_CHUNK_ERROR("onChunkError", "on-chunk-error-method", OnChunkError.class, ChunkListener.class,
Exception.class, Chunk.class),I am happy to submit a pull request for this.
Impact
An annotation-based chunk error handler compiles, registers without complaint, and does nothing. Error notifications, cleanup, and failure counters written this way are silently lost, and the step reports the failure normally, so nothing points at the listener.
Workaround
Implement ChunkListener and override onChunkError(Exception, Chunk).
Related issues
- Conflicting info in @AfterChunk/@BeforeChunk annotation javadoc to current implementation #4961 / GH-4961: Fix @BeforeChunk/@AfterChunk to support Chunk parameter as documented #4969 —
@BeforeChunk/@AfterChunkfixed to acceptChunkas documented.AFTER_CHUNK_ERRORwas not migrated in that change. - In Spring Batch 6, using
@BeforeChunk/@AfterChunk/@AfterChunkErrorwith aChunkContextparameter (as in Spring Batch 5) triggersIllegalArgumentException#5297 —@AfterChunkErrorwith aChunkContextparameter onSimpleStepBuilder. Different path: that one throwsIllegalArgumentException, this one fails silently. - Clarify ChunkListener changes in v6 #5226 —
ChunkListenercallbacks on anItemReadernot invoked in 6.x.
Activity
benelog commentedon Jul 28, 2026
Submitted a pull request for this: #5469
fmbenhassine commentedon Aug 26, 2026
This is a valid issue. Thank you for reporting it and for providing a sample!
I planned the fix for the next release.
Add missing StepListenerMetaData entry for @OnChunkError
Add missing StepListenerMetaData entry for @OnChunkError