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>