Skip to content

@OnChunkError is never invoked on a chunk-oriented step #5468

Closed

Description

@benelog

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 bootRun

Actual 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 OnChunkError

AbstractListenerFactoryBean#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

Activity

  1. benelog commented on Jul 28, 2026

    @benelog
    ContributorAuthor

    Submitted a pull request for this: #5469

  2. added this to the 6.0.6 milestone on Aug 13, 2026
  3. fmbenhassine commented on Aug 26, 2026

    @fmbenhassine
    Contributor

    This is a valid issue. Thank you for reporting it and for providing a sample!

    I planned the fix for the next release.

  4. added a commit that references this issue on Aug 26, 2026
    8bf3bad
  5. added a commit that references this issue on Aug 27, 2026
    b516572
  6. added
    for: backport-to-6.0.x
    on Sep 29, 2026
  7. 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