1
votes

Can anyone help me in this issue about apache camel? I'm trying to execute a route that acts as follow:

  1. Send a XML archive to a Webservice
  2. If the response is OK, move this file to a directory called "successImport"
  3. If the response fails, move this same file to a directory called "failImport"

The actual problem is: Camel is moving my files to both directoris, failImport and successImport, even when the webservice process fail.

This is a snippet of my code:

String webservice310 = "localhost:8080/api/importxml/version310";
String webservice200 = "localhost:8080/api/importxml/version200";
String dir = "c:/imports/";

from("file:" + dir + "?include=.*.xml&delay=1000&move=successImport")
.process(new ProcessorXml())
.doTry()
    .choice()
        .when(header("version").isEqualTo("3.10"))
            .to("restlet:"+ webservice310 + "?restletMethod=post")
        .when(header("version").isEqualTo("2.00"))
            .to("restlet:"+ webservice200 + "?restletMethod=post")
.endDoTry()
.doCatch(XmlBindException.class)
    .to("file://" + dir + "failImport");

When I change the route for something like this:

from("file:" + dir + "?include=.*.xml&delay=1000&move=successImport&moveFailed=failImport")

Camel works good, but any error goes to the "failImport" even though this is not triggered by the exception I'm expecting. How can I move only exceptions that I'm specifying on my route "doCatch" block?

2

2 Answers

3
votes

Have you tried exception clause?

Your code would look like

from("file:" + dir + "?include=.*.xml&delay=1000&move=successImport")
.onException(XmlBindException.class)
    .handled(true)
    .to("file://" + dir + "failImport")
.end()
.process(new ProcessorXml())
.choice()
    .when(header("version").isEqualTo("3.10"))
        .to("restlet:"+ webservice310 + "?restletMethod=post")
    .when(header("version").isEqualTo("2.00"))
        .to("restlet:"+ webservice200 + "?restletMethod=post")

But be careful, if exception is not XmlBindException, Camel will try to redeliver the file endlessly

1
votes

After trying Sergey's solution, I've found that I could achieve my expected results using the code below:

String webservice310 = "localhost:8080/api/importxml/version310";
String webservice200 = "localhost:8080/api/importxml/version200";
String dir = "c:/imports/";

from("file:" + dir + "?include=.*.xml&delay=1000&move=successImport&moveFailed=failImport")
.process(new ProcessadorNfe())
.onException(RestletOperationException.class)
    .handled(false)
    .maximumRedeliveries(-1).delay(60 * 1000)
    .end()
.choice()
    .when(header("version").isEqualTo("3.10"))
        .to("restlet:"+ webservice310 + "?restletMethod=post")
    .when(header("version").isEqualTo("2.00"))
        .to("restlet:"+ webservice200 + "?restletMethod=post")

As you can see, I used a "moveFailed" clause on my route and tailored my .onException clause to wait for the server to be up. So, whenever the server is down, camel will wait, but when it receives another kind of exception, it will move my file, and only my file to the failImport folder.