Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File processing order with Spring Batch

I have a question about the order of processing of csv files with Spring Batch, I have several csv files to process, when I launch the spring batch I do not know in what order spring processes these files.

I would like to know how Spring chose the first file to treat ? and where can I find this setting ?

Is it possible to define a sort ? for example, processing files based on a date written on the file name? and how can I customize the choice of the first files to process?

Thank you all,

like image 894
Mohcine Avatar asked Feb 15 '26 18:02

Mohcine


1 Answers

Spring batch's MultiResourceItemReader uses Comparator<Resource> to preserve ordering. If we don't provide comparator, then the default ordering will be based on file name. If you want to give your custom sorting, you can write your own comparator logic like following(sort based on last modified time).

public class FileModifiedComparator implements Comparator<FileSystemResource>{

        @Override
        public int compare(FileSystemResource file1, FileSystemResource file2) {
       //comparing based on last modified time
            return Long.compare(file1.lastModified(),file2.lastModified());
        }
 }

You can modify the comparator to check modify your sort logic such as file name, created etc. for eg: return file1.getFilename().compareTo(file2.getFilename()); or return Long.compare(file1.contentLength(),file2.contentLength()); and in the MultiResourceItemReader bean, set this comparator.

<bean id="fileModifiedComparator" class="FileModifiedComparator"/> 

<bean id="multiResourceReader"
    class=" org.springframework.batch.item.file.MultiResourceItemReader">
    <property name="resources" value="file:inputs/input*.csv" />
    <property name="delegate" ref="flatFileItemReader" />
    <property name="comparator" ref="fileModifiedComparator" />
</bean>
like image 86
Binu Avatar answered Feb 17 '26 07:02

Binu