Reputation: 300
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();
Upvotes: 1
Views: 3143
Reputation: 3320
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();
Upvotes: 5
Reputation: 183
Answering first two points of your question: It is possible - take a look at documentation. Parameter called fileName is well described there, and it allows you to control file names and extensions (actually part of name) very strictly.
When it comes to point number 3, then if your "choice" is about only one case, then I'd like recomend using filter component rather then choice.
It will allow you easily model route where for example *xml is routed to your "testing" directory when it indeed is XML and contains country "us". Note that catching some exceptions might be necessary when faulty file (non XML) will appear ;).
Upvotes: 0