0

I have a Spring Batch reader with following configurations. This reader is reading from the database and and at a time its reading a page size records.

    @Autowired
    private SomeCreditRepot someCreditRepo;

    public RepositoryItemReader<SomeCreditModel> reader() {
         RepositoryItemReader<SomeCreditModel> reader = new RepositoryItemReader<>();
         reader.setRepository(someCreditRepo);
         reader.setMethodName("someCreditTransfer");
         .
         .
         
         ..
         return reader;
      }

I want to call utils method,

     refValue = BatchProcessingUtil.generateSomeRefValue();

before the processor step, so that all the records fetched by the reader will have the same value set by which is given by the above call.

So that all the entity fetched by the reader will get the same value, in the processor.

And then this refValue will be written to another table StoreRefValue(table).

What is the right way to do this in Spring Batch? Should I fire the query to write the refValue, to the table StoreRefValue in the processor?

John Doe
  • 2,752
  • 5
  • 40
  • 58

1 Answers1

1

You can let your processor implement the interface StepExecutionListener. You'll then have to implement the methods afterStep and beforeStep. The first should simply return null, and in beforeStep you can call the utility method and save its return value.

Alternatively, you can use the annotation @BeforeStep. If you use the usual Java DSL, it's not required to explicitly add the processor as a listener to the step. Adding it as a processor should suffice.

There are more details in the reference documentation: https://docs.spring.io/spring-batch/docs/current/reference/html/step.html#interceptingStepExecution

Henning
  • 3,055
  • 6
  • 30
  • I have a doubt, suppose, I am getting a list of result and the list has given number of results. So the reader will be called once for each list items, or it will be called only once it reads all the given sized, configured in the configurations, list results. And after read call the optional processor if its there will be called, I mean if I have provided the processor then it will be called after each reader or not. And the writer will write to the database all the results read till now, or it will do it one by one. – John Doe Mar 08 '22 at 15:39
  • 1
    Spring Batch retrieves one item at a time from the `read` method of the reader until a chunk is complete (although the readers themselves usually fetch more than one item at a time from their sources). All items of a chunk are then processed and passed to the writer. Then, the next chunk will be started. There is very helpful pseudo code about this in the documentation: https://docs.spring.io/spring-batch/docs/current/reference/html/step.html#chunkOrientedProcessing – Henning Mar 08 '22 at 16:23
  • Thanks!! Suppose, I need to make a rest call in a step then how can I do it!! – John Doe Mar 08 '22 at 16:41