1

How would one implement a custom counter in itemProcessor? A basic counter could work as defined here, but I need the counter not to include retried items or items in rolled back chuncks. Maybe there is itemStream like interface for itemProcessor that I haven't found yet. Using SpringBatch 2.1.7.

EDIT: The batch configuration can be found here (Using compositeProcessor). I've tried to implement the counter like follows (with no luck): Setting itemProcessListener for all processors and in afterProcess(I,O) increment the counters for each processor in a cache (cache also is in each processor). Then using itemWriteListener for all processors and in afterWrite() flush the cache to stepExecution. But this don't work as the itemProcessListener is not working with the compositeProcessors child processors as I would have expected. Any other idea?

EDIT: I've removed the compositeProcessor and tried to use only a single processor and found out that the itemProcessListener.afterProcess will be called too many times. I am guessing that this is related to the chunk-processing-mode vs. single-processing-mode. So some of the not retried items of a chunk will be re-processed. I've also tried to use RetryListener (to disable counter increments if a retry is in progress), but was not able to configure it. The open and close would not have been called on RetryListener.

Community
  • 1
  • 1
user313
  • 11
  • 4

2 Answers2

0

I think StepExecution domain object should fits your request.
Intercept it with a StepExecutionListener and access wanted properties.

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • How would you handle chunk rollback or item retry in chunk? I think that the link in my question points to a solution you describe. – user313 Aug 18 '14 at 15:58
  • if domain object is not suffice create a custom bean (or custom properties) and store it into execution context; using listeners update custom bean properties. – Luca Basso Ricci Aug 18 '14 at 17:03
  • Could you elaborate? Currently I am using similar code like the link above is, but when item is retried, the counter increments would retry also; giving me incorrect counter values when batch is finished. Is the only way to synchronize counters with the real outcome of the batch to catch retries/rollbacks/etc and try to decrease the counters respectively? This sounds like a workaround. Or maybe I am missing something fundamental here. – user313 Aug 18 '14 at 18:41
  • I've updated the question to include link to my configuration and my attempt to implement the counters. – user313 Aug 19 '14 at 13:46
0

I was able to solve this counter issue; However I had change the batch a bit. First thing to remove was the compositeItemProcessor. Then I needed to update SpringBatch version to 2.2.7 (To get ChunkListener.afterChunkError). Now when processor increments a counter, I'll cache the counter to processor. I also use ChunkListener.afterChunkError to clear the cache, so when items are reprocessed, the old counter values will be cleared. When ItemWriteListener.afterWrite() occurs I'll flush the cache to stepExecutionContext. This way I was able to overcome the retry increments counters problem.

user313
  • 11
  • 4