Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

324
Views
Spring Batch Item Writer Listener not Working

I use spring batch and implemented item writer listener. My Item Writer Listener class is something like this

@Component
public class SomeItemWriterListener implements ItemWriteListener {

@Override
public void beforeWrite(List<Object> items) throws Exception {
    System.out.println("get here1");

}

@Override
public void afterWrite(List<Object> items) throws Exception {
    System.out.println("get here2");

}

@Override
public void onWriteError(List<Object> items, Exception ex) throws Exception {
    System.out.println("get here3");
}
}

Then I configure it in my step like this using Java Config

@Autowired
private SomeItemWriterListener writerListener;

@Bean
public Step step(StepBuilderFactory stepBuilderFactory,
    @Qualifier("itemReader")
        ItemReader<SomeItem> reader) {
    return stepBuilderFactory.get("step").listener(
        writerListener).chunk(
        chunkSize).reader(reader).processor(processor).writer(writer).build();
}

But, beforeWrite, afterWrite is not called. When I add some error onWriterError also not called. Did I miss something when I configure this?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your are calling the StepBuilderHelper.listener(Object) method which, as you can see only is for annotation based listeners. Next to that you are trying to register the listener at the step level, whereas you should be registering it at the chunk level.

@Bean
public Step step(StepBuilderFactory stepBuilderFactory, @Qualifier("itemReader") ItemReader<SomeItem> reader) {
    return stepBuilderFactory.get("step")
            .chunk(chunkSize)
              .reader(reader)
              .processor(processor)
              .writer(writer)
              .listener(writerListener)
            .build();
}

This will call the appropriate listener(ItemWriteListener) method (there is also listener(Object) it might be needed that you need to cast to the ItemWriteListener interface to have the correct method invoked.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!