mikriuzikri
mikriuzikri

Reputation: 11

update camel exchange body after exception and continue the route

Hope you have some spare minutes for my question. For the last couple of days I was reading about Camel and managed to setup everything up and running. Now, I have bumped on a tricky part :). Basically, I define a route using Java in runtime and put the route configuration in DB. Routes are working and messages are flowing from one side to another. But, when an exception occurs I would like to know where the exception has occurred (between which route endpoints), store the current exchange body (in-flight message that is useful for further processing) in the DB, update the message by the user and then retry the route execution from the point of exception (where it failed). Lets say that all route steps are idempotent.

My solution:

  1. Make a Interceptor for the route
  2. Granulate the route in as much as possible parts (each new step is a new route)
  3. Between each step update the DB with the current and future step and current exchange body
  4. If exception occurs, store the message in DB, update it using an editor
  5. Send a message to a next route point (taken from DB) using ProducerTemplate

What do you think about this ? Is it doable or Camel cannot support me with this approach ?

Thank you for patience and your time. Hope I was clear enough.

Upvotes: 1

Views: 3517

Answers (2)

Sikorski
Sikorski

Reputation: 2691

You should use onException() clause to tackle this. For eg :

public void configure() throws Exception{
  //This is a global onException definition and will work for all routes defined in this calss
    onException().process(new Processor(){
        public void process(Exchang arg0){
            Exception e = arg0.getProperty(Exchange.EXCEPTION_CAUGHT,Exception.class);
            //get message and other properties that you  are interested in 
            db.saveOrUpdate(/*Pass your custom object here*/); 
        }

    }).handled(true);

   from("direct:route1")
   //some processing
   .to("jms:route1");

   from("direct:route2")
   //some processing
   .to("http://route2");

}

You might need to consult exact details at apache camel site, since i just wrote this code here.

Upvotes: 0

Hussain Pirosha
Hussain Pirosha

Reputation: 1376

You can use Camel's tracer component. A detailed example meeting your needs is already available on camel's site : http://camel.apache.org/tracer-example.html

Upvotes: 0

Related Questions