javaspringspring-bootspring-batchitemwriter

Spring Batch Itemwriter Interface fix


The write method of Itemwriter has been changed in the spring version 5.

@Override
public void write(List<? extends List<DataDTO>> items) throws Exception{
    for(List<DataDTO> sublist:items){
        writer.write(sublist);
}

The above writer is FlatFileItemWriter.

I changed to the following

@Override
public void write(Chunk<? extends List<DataDTO>> items) throws Exception{
    for(List<DataDTO> sublist:items){
        writer.write((Chunk<? extends DataDTO>)sublist);
    }
}

Is this correct way of replacing/fix? need some help.

Im expecting the correct fix.

Expecting coreect replacement.


Solution

  • According to the Spring Batch 5.0 migration guide, most references to Lists in the API were replaced with Chunk.

    • The signature of the method ItemWriter#write(List) was changed to ItemWriter#write(Chunk)
    • All implementations of ItemWriter were updated to use the Chunk API instead of List
    • All methods in the ItemWriteListener interface were updated to use the Chunk API instead of List
    • All implementations of ItemWriteListener were updated to use the Chunk API instead of List
    • The constructor of ChunkRequest was changed to accept a Chunk instead of a Collection of items
    • The return type of ChunkRequest#getItems() was changed from List to Chunk

    A good way to do the migration in your code is to use one of Chunk's constructors.

    Example:

    @Override
    public void write(Chunk<? extends List<DataDTO>> items) throws Exception{
        for(List<DataDTO> sublist:items){
            writer.write(new Chunk<>(sublist));
        }
    }