1

I'm new with Spring-batch. I have a simple, proof-of-concept, job which does something dummy.

ItemReader: Get a number of records between dateA and max date (last record at the moment) ItemProcessor: aggregates the rows per city ItemWriter: writes the result to the database

My question is that when the job finishes, I want to keep the last date in order for the next job execution to start from that date as dateA. I know that I can use JobExecutionContext to share data but if on the second run, I tried to get dateA but the value is null

IS there a way to get the previous dateA value recorded from the previous job execution? and how?

Thanks in advance

tbo
  • 9,398
  • 8
  • 40
  • 51

2 Answers2

0

There are multiple ways of implementing this. The simplest way would be just write it in a file and read that file on next start. But this is somehow dirty. You can also write it in DB or just run an sql that will return the max date from data you already have in db. Basically I would implement a simple tasklet(similar Tasklet to delete a table in spring batch ) that runs the sql and adds max data on the chunkContext.

Community
  • 1
  • 1
Liviu Stirb
  • 5,876
  • 3
  • 35
  • 40
  • Hi and thx for the reply. The thing is that the data are already there is there a proper way to get a value that has been updated from the previous job execution? – tbo Jan 15 '14 at 15:21
  • just implement a simple tasklet like here: http://stackoverflow.com/questions/7834304/tasklet-to-delete-a-table-in-spring-batch that runs the sql that returns the max date and add it on the chunkContext – Liviu Stirb Jan 15 '14 at 15:28
  • that is a good approach. I was wondering maybe if I implement a listerer and add something like beforeJob to retrieve the value and afterJob to persist the new value for the next operation. My question is that does this belongs to the same transaction? with the write operation? thx – tbo Jan 15 '14 at 15:31
  • idk about transaction; i prefer a tasklet but i'm quite sure you can also implement using beforeJob and afterJob – Liviu Stirb Jan 15 '14 at 15:47
  • did some research and by default, the execute method of a tasklet is transactional. more here: http://www.javabeat.net/transaction-management-in-spring-batch-components/ – Liviu Stirb Jan 15 '14 at 16:06
0

The following may be an alternative.

Store the date in a bean holder using JobExecutionListener#afterJob. The following link answers passing a collection at a Step level.

What's the best way to pass a huge collection to a Spring Batch Step?

Community
  • 1
  • 1
ram
  • 747
  • 2
  • 11
  • 34