Jeff Cook
Jeff Cook

Reputation: 8814

How to do the Auditing of records from CompositeItemWriter in Spring Batch?

In Spring Batch, I want to make an audit of the No. of records being read, process and write. I know that the same functionality is available in the Batch metadata tables. Per business need we need to make an audit into say DATA_AUDIT table.

In my batch jobs, I've implemented the CompositeItemWriter, based on the different fields (say Active Accounts, InActive Accounts, Active Flag etc), based on that I am segregating the data and write it into the multiple target tables.

Here say if 5000 records are coming, those 5000 records are getting grouped into data sets and single records can satisfy different business rules and go into different groups and this way data is getting incase upto say 20,000 in the chunk of 5000.

StepListeners is only capturing the no. of records of the first writer, its not capturning any other writers data written, how can I track the no. of data has been writen from other 3 writers ?

Is there any way to do it using Spring Batch API or how can we achieve this ? I went through link, but did not find anything here - https://docs.spring.io/spring-batch/docs/current/reference/html/step.html#stepExecutionListener

Upvotes: 0

Views: 408

Answers (1)

Sushil Behera
Sushil Behera

Reputation: 971

You can get the JobExecutionContext and update the count in the writer.

Example is below.

@Bean
@StepScope
public ItemWriter<Integer> itemWriter() {
    return new ItemWriter<Integer>() {

        private StepExecution stepExecution;

        @Override
        public void write(List<? extends Integer> items) throws Exception {
            for (Integer item : items) {
                System.out.println("item = " + item);
            }
            long count=stepExecution.getJobExecution().getExecutionContext().get("count");
            count=count+items.size();
            stepExecution.getJobExecution().getExecutionContext().put("count", count);
        }

        @BeforeStep
        public void saveStepExecution(StepExecution stepExecution) {
            this.stepExecution = stepExecution;
        }

    };
}

Upvotes: 1

Related Questions