1

Our Spring Batch application has:a custom JdbcCursorItemReader which reads from the database, and a custom JmsItemWriter, which enables headers to added to each message.

The idea is that each message will have a correlation id and be part of a set. Within the JmsItemWriter, we need to access the total number of rows which the item reader retrieved.

What is the best approach to do this?

Thanks

user1052610
  • 4,440
  • 13
  • 50
  • 101

2 Answers2

0

If you need to access the TOTAL count of rows (eg the select count(*) from table) the best choice is to create a previous step where you perform the count and put the result into execution-context to access it in next steps.
If you need the number of current record JdbcCursorItemReader inherits this facilities from AbstractItemCountingItemStreamItemReader

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • Thanks very much. What is the best way to access the execution context from an ItemProcessor or ItemWriter ? – user1052610 Apr 29 '14 at 15:00
  • look at http://stackoverflow.com/questions/22205778/what-are-the-spring-batch-default-context-variables/22208521#22208521 – Luca Basso Ricci Apr 29 '14 at 15:31
  • Thanks for all your help. Would this work (and avoid having both to have an extra step and to hit the DB with an extra query): 1. Create a custom JdbcCursorItemReader 2. Inject into it the execution context ( – user1052610 Apr 30 '14 at 12:10
  • Maybe.You have to try. But I prefer an extra step because I think it is more readable (or implement `StepExecutionListener.beforeStep()` to perform `select count` if you don't want to create an extra step) – Luca Basso Ricci Apr 30 '14 at 12:14
  • Thanks. Here is what I am not understanding: how from an item writer, processor or reader, to get hold of the StepExecutionContext itself so as to then add a variable to it (as opossed to getting the value of a variable which is already in it). Is that possible? – user1052610 May 01 '14 at 13:42
  • Use listeners. Or extend or delegate reader, writer or processor – Luca Basso Ricci May 01 '14 at 14:32
  • When extending reader, writer or processor, what base class functionality exposes the stepExecutionListener ? – user1052610 May 01 '14 at 18:58
  • sir, I'm not a human javadoc ;) I answered your question and more... please read official documentation at http://docs.spring.io/spring-batch/reference/html/ about listeners – Luca Basso Ricci May 01 '14 at 19:04
  • Great - implement ItemStream - very much appreciated. – user1052610 May 01 '14 at 19:11
0

Easiest way is to use the StepListener, see following example :

@Component
public class StepListener extends StepExecutionListenerSupport {

  @Override
  public ExitStatus afterStep(StepExecution stepExecution) {
    // here, you can debug stepExecution to get some values like "readCount" :
    logger.info(stepExecution.getReadCount());

    // you also have commitCount, writeCount, rollbackCount, readSkipCount, writeSkipCount, processSkipCount, filterCount

    // check stepExecution.getSummary for a nice one line info
    logger.info(stepExecution.getSummary());

    return null;
  }
}

Don't forget to configure your stepListener in the Step of the Batch configuration

DependencyHell
  • 1,027
  • 15
  • 22