Douglas Tober Douglas Tober - 1 month ago 16
Java Question

Apache-Camel Choice on Filetype

The Code listed below works just fine, but it's function is to look into an XML file and if the field is 'us' to move it to another directory; what I would like to know about using the .choice() function:

1) How do I specify a specific file to be routed? (Adding the filename to the end of the path did not work)

2) How do I specify a filetype to be routed? (Ex: route all .txt files to "blah")

3) Are there other options besides using .choice that will help me to do this?

CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder()
{
public void configure() throws Exception
{
from("file:C:\\camels\\inner?delete=true")
.choice()
.when(xpath("/author/country = 'us'"))
.to("file:C:\\testing");
}
});
context.start();
Thread.sleep(10000);
context.stop();

Answer

Here are some of the ways to do this

  • Have a look at http://camel.apache.org/file-language.html for file languages exposed by camel this provides some option you can use to get file name with extension , file only name, file parent file extension etc..

  • Also look at the include option at http://camel.apache.org/file2.html, this will help to poll out only the files with filename matching a regex pattern.

    from("file:C:\\camels\\inner?delete=true&include=abc")
    
  • Build a Predicate and use it like below:

    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder()
    {
        Predicate predicate = PredicateBuilder.and(simple("${file:name.ext} == 'txt'"), XPathBuilder.xpath("/author/country = 'us'"));
        public void configure() throws Exception
        {
            from("file:C:\\camels\\inner?delete=true")
                .choice()
                    .when(predicate)
                        .to("file:C:\\testing");
        }
    });
    context.start();
    Thread.sleep(10000);
    context.stop();