I have the following ItemProcessor, I need to know if the item is the last item the reader sends. Is this possible?
The flow is:
ItemReader
- read lines from database (expected millions of lines)ItemProcessor
- perform some validations on each line, send to webservice and perform more validations with webservice response.ItemWriter
- write in database errors from validations, and change line status.
Another possible solution would be the ItemReader to send a list of Line
with size NUMBER_OF_LINES_TO_CALL_WEBSERVICE
, but I could not find examples on how to modify the JpaPagingItemReader
.
@Override
public List<Line> process(Line line) {
lineList.add(line);
if (lineList.size() == NUMBER_OF_LINES_TO_CALL_WEBSERVICE) {
callWs(lineList);
return lineList; // goes to ItemWriter to write the response from ws in database
}
if( /* TODO if last line */) {
callWs(lineList);
return lineList;
}
return null;
}
private void callWs(List<Line> lineList){
...
//Get response from webservice and add info to lines
}
@Override
public void afterChunk(ChunkContext chunkContext) {
if (lineList.size() == NUMBER_OF_LINES_TO_CALL_WEBSERVICE) {
lineList.clear();
}
}
The reader is a JpaPagingItemReader
:
@Bean
@StepScope
public JpaPagingItemReader<Line> itemReader(@Qualifier("EntityManagerFactory") EntityManagerFactory entityManagerFactory) {
String queryStr = "select l from Line l" ;
return new JpaPagingItemReaderBuilder<Linha>()
.name("lineReader")
.entityManagerFactory(entityManagerFactory)
.queryString(queryStr)
.pageSize(PAGE_SIZE)
.build();
}