Fetch and maintain reference data at Job level in Spring Batch

Viewed 17

I am configuring a new Job where I need to read the data from the database and in the processor, the data will be used to call a Rest endpoint with payload. In the payload along with dynamic data, I need to pass reference data which is constant for each record getting processed in the job. This reference data is stored in DB. I am thinking to implement the following approach.

  1. In the beforeJob listener method make a DB call and populate the reference data object and use it for the whole job run.
  2. In the processor make a DB call to get the reference data and cache the query so there will be no DB call to fetch the same data for each record.

Please suggest if these approaches are correct or if there is a better way to implement them in Spring batch.

1 Answers

For performance reasons, I would not recommend doing a DB call in the item processor, unless that is really a requirement.

The first approach seems reasonable to me, since the reference data is constant. You can populate/clear a cache with a JobExecutionListener and use the cache in your chunk-oriented step. Please refer to the following thread for more details and a complete sample: Spring Batch With Annotation and Caching.

Related