Camel and file sorting

The file component to be used for file connectivity in Camel is a key component in the kind of integration works I am hustling with a clients. We are still doing a lot of file and messaging based on IBM platforms from large mainframes and other older platforms. So I am not riding on the Camel with the yellow jersey in the "peloton" (hint: Tour de France).

So my Camel is up for a maintenance upgrade now that we are working on Camel 2.0. A part of this upgrade is being able to support ease of use for pluggable file filtering and sorting.

The sorting comes in two flavors:
- comparator
- textual expressions

Sorting by comparator
We leverage the java.util.Comparator to allow end users to configure their own implementation to use, or use what we have on the shelves out-of-the-box for the regular sorting by name, modified date, size etc provided in org.apache.camel.component.file.DefaultFileSorter.

We can configure the comparator in the file endpoint using the sorterRef option:

... where myFileSorter is lookup up in the registry, typically Spring application context defined as a spring bean:

<bean id="myFileSorter" class="com.mycompany.MyFileSorter"/>

Sorting by expressions
This is where we did go the extra mile and implemented textual configuration of sorting. You just add a sortBy option to the file endpoint URI configuration where you express what and how to sort.

A plain example would be:

We can specify reverse if we wanted to sort in reverse order

And the ignore case is also supported so you can process files regardless of the case

And on top of that we have grouped sorting so you can append a 2nd sort by, where the groups are separated with semi colon. So if we want to sort first by file extension and then by name we do:

As the expression is the file language you got its full power as well so if you want to sort by modified timestamp but it should only consider the date part, we can use the yyyyMMdd pattern and express this as:

Currently this is only implemented on the file component, but next up is to implement this for the FTP component as well.

Check out the file component and the file language to get an idea how this new sortBy option can be used.


satyam said...

I have some files(i.e xml files) in a folder. Can u guide me how can i sort that files based on time stamp and also i have to pick the latest file.
Thanks in advance,

Claus Ibsen said...

Yeah check out this page the section - using sortBy

You do something like

And you can reverse the sorting by using reverse

satyam said...

Hi Claus,

thanks for ur reply....

Vinu Raj said...

sorterRef=myFileSorter is not working in the vesion (2.13.1) I am using. Instead sorter=#myFileSorter is working fine.