Spring Package - counting processed rows

So, I am creating a Spring batch job to read a CSV file and for certain lines containing incomplete data; it checks, logs that the line is incomplete, and skips. It works great, except at the end of the job I want it to record the number of rows found that were incomplete. Just something like "X incomplete lines."

I searched Google and searched for a solution, but found nothing.

Any help is appreciated and you need more information.

+6
source share
3 answers

To solve this problem, here is how I did it:

In ItemProcessor, I added an attribute and method to access the ExecutionContext from the processing method,

private ExecutionContext executionContext; @BeforeStep public void beforeStep(StepExecution stepExecution) { this.executionContext = stepExecution.getExecutionContext(); } 

... and then in the process () method, when I find one of the lines that I want to write, I can do this,

 this.executionContext.putInt( "i_ThoseRows", this.executionContext.getInt( "i_ThoseRows", 0 ) + 1 ); 

Finally, I add another method for ItemProcessor to print the result at the end of the step,

 @AfterStep public void afterStep(StepExecution stepExecution) { System.out.println( "Number of 'Those rows': " + this.executionContext.getInt( "i_ThoseRows", 0 ) ); } 

Hope this helps someone

+6
source

Spring Package itself keeps track of how many records it reads, writes, processes, and how much it skips (for each of these numbers). This information is stored in StepExecution . StepExecution can be accessed from StepExecutionListener . In this case, the afterStep method will afterStep .

 public class SkippedItemStepExecutionListener extends StepExecutionListenerSupport { @Override public ExitStatus afterStep(StepExecution stepExecution) { int skipped = stepExecution.getSkipCount(); // Total for read+write+process // Log it to somewhere. return null; } } 

How to add it to your work / step is explained in the reference guide

References

+10
source

To complement @dogfight's answer:

from spring batch documents:

Annotations are parsed by the XML parser for elements, so all you have to do is use the XML namespace to register listeners in steps

So, to call the annotated callback functions of the listener beforeStep () and afterStep (), you need to register the ItemProcessor element as a listener in step:

 <listeners> <listener ref="MyItemProcessor"> </listeners> 

Otherwise, you will have a NullPointerException when using executionContext .

0
source

Source: https://habr.com/ru/post/953632/


All Articles