简体   繁体   中英

How to know if it is the last item in ItemProcessor of SpringBatch

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();
    }

I want to group lines in a list of 100 elements to send to a webservice, and when the last item is sent to processor the remaining lines are sent to the webservice.

You can use a chunk-oriented step with a chunk size of 100. The step will do the buffering for you, and the first point in the process where you have access to the list of items is in the ItemWriteListener#beforeWrite(List items) . So you can either use that listener to perform you web service call, or do it in the ItemWriter itself (if this is the only write operation you need to perform).

Once you have the list of items, you can do whatever you want with it (ie check the last item, filter the first 99 items and send post them to the web service, etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM