Skip to content

Proposal: Automatically register ItemHandler as StepListener instead of only StepExecutionListener in ChunkOrientedStepBuilder #5087

@KILL9-NO-MERCY

Description

@KILL9-NO-MERCY

Hello, Spring Batch team! I would like to submit a proposal regarding how item handlers are automatically registered as listeners on ChunkOrientedStepBuilder

Context
Starting from commit 52875e7, an ItemReader, ItemProcessor, or ItemWriter is automatically registered to stepListeners(as a StepExecutionListener) only when it directly implements StepExecutionListener:

if (this.reader instanceof StepExecutionListener listener) {
    this.stepListeners.add(listener);
}
if (this.writer instanceof StepExecutionListener listener) {
    this.stepListeners.add(listener);
}
if (this.processor instanceof StepExecutionListener listener) {
    this.stepListeners.add(listener);
}

In Batch 5, however, these components were automatically registered as listeners if:

  1. they implemented StepListener, or
  2. they had methods annotated with any listener annotation defined in StepListenerMetaData
    (handled internally by StepListenerFactoryBean.isListener(itemHandler) in SimpleStepBuilder#registerAsStreamsAndListeners())

This allowed ItemReader/ItemProcessor/ItemWriter to be detected as a listener even when implementing more specific listener interfaces (e.g., ItemReadListener, ItemProcessListener, etc.) or when using listener annotations such as @BeforeRead, @AfterRead, @OnReadError, etc.

Proposal
Although I haven’t personally used this pattern extensively, allowing item handlers that implement ItemReadListener/ItemProcessListener/ItemWriteListener to be automatically registered via the StepListener interface could increase their practical utility, as the listener could access internal state of the item handler directly.

if (this.reader instanceof StepListener listener) {
    this.stepListeners.add(listener);
}
if (this.writer instanceof StepListener listener) {
    this.stepListeners.add(listener);
}
if (this.processor instanceof StepListener listener) {
    this.stepListeners.add(listener);
}

Thank you for your time and consideration!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions