5 Replies Latest reply on Oct 7, 2009 1:40 PM by Claus Ibsen

    Exception handling

    sriram_imshriram Novice

      This online forum is really good, people get quick responses here..... keep up the good work fuse.....


      I have only bookish knowledge abt camel and I am trying to get my hands dirty......


      Either my doubt is so simple that everyine knows how to do it or it is so illogical that no one wants to do it only.... anyways here is the scenario,

      I route my message like this,












      In effect, when i receive a message from my client i send it to my server. If the client had not sent a proper message, my server sends back an object and i intimate it to the client of failure due to the message he sent. If i dont get that particular object i send a success message to the client.


      Now the scenario is, my server is down and i get a java Exception.


      in this case i need to call another set of xslts to convert the object's props to client understandable format (basically we need to send diff xmls to client for diff failures.)


      In java, i would easily do it with try/catch......but how to attain the same behavior in routing?

      I am imagination is that when, when .to(my_server) is executed, camel would get an exception, i dont want that exception to be passed to caller or something, i want to handle it right there....... is it possible?

        • 1. Re: Exception handling
          Claus Ibsen Master



          Thanks for the words about the forums.


          In Camel 2.0 we have added doTry .. doCatch .. doFinally so you could do something that you can easily do in regular java.


          However in Camel 1.x and/or 2.0 sometimes its easier to just use a processor where you can invoke your server in a try .. catch and handle the exception there.


          So replace .to(server) with .process(new MyCallServerProcessor())


          And in this processor you can invoke the server and handle the response


          public void process(Exchange exchange) {
             ProducerTemplate template = exchange.getContext().createNewProducerTemplate();
             template.send("server uri", exchange);
             Exception e = exchange.getException();
             if (e != null) {
                // we got an exception when calling the server


          • 3. Re: Exception handling
            sriram_imshriram Novice

            Thank you very much......you guys are real good......

            • 4. Re: Exception handling
              parasmk Newbie

              I have a similar problem, except my server is the producer as opposed to client in Sriram's case.


              I need to transfer files from a FTP server to a local directory and know if the server is down and if so retry every 10 minutes.  Is there any method to catch exceptions from the producer?


              Camel doTry/doCatch, I believe, catches exceptions within the exchange, i.e. after the from(). producer.


              My route: from("ftp:// ...").to("file:// ..."). I'm using camel-2.0-M3.


              Thanks a lot in advance. By the way, thanks for your contribution to this cause.




              <!Session data>

              • 5. Re: Exception handling
                Claus Ibsen Master

                Are you sure you mean producer? You want to consume messages from a remote FTP server? So I assume its consumer you mean.


                Camel will by default retry so you do not really need to do anything than define the delay. So if you want 10 min delay then set it as delay=600000 as its value is in milli seconds.




                In Camel 2.0 you can fine tune what to do in case the remote ftp server has a problem. Its the option.