Sorting using Comparator - 6.3

Talend ESB Mediation Developer Guide

Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
Design and Development
Talend ESB

Camel supports pluggable sorting strategies. This strategy it to use the built in java.util.Comparator in Java. You can then configure the endpoint with such a comparator and have Camel sort the files before being processed.

In the sample we have built our own comparator that sorts by file name:

public class MyFileSorter implements Comparator<GenericFile> {
    public int compare(GenericFile o1, GenericFile o2) {
        return o1.getFileName().compareToIgnoreCase(o2.getFileName());

Then we can configure our route using the sorter option to reference to our sorter ( mySorter ) we have defined in the Spring XML file:

<!-- Define our sorter as a plain Spring bean -->
   <bean id="mySorter" class="com.mycompany.MyFileSorter"/>

   <from uri="file://inbox?sorter=#mySorter"/>
   <to uri="bean:processInbox"/>


URI options can reference beans using the # syntax. In the Spring DSL route, notice that we can refer to beans in the Registry by prefixing the id with #. So writing sorter=#mySorter, will instruct Camel to go look in the Registry for a bean with the ID, mySorter.