camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Raul Kripalani <r...@evosent.com>
Subject Re: Looking for ideas to reduce code volume and duplication
Date Wed, 02 Apr 2014 10:27:33 GMT
Try this technique. Nothing fancy, just a plain old Java static helper
method. It relies on Camel being able to configure exception handlers if
placed at the end of the route definition, which I think is possible.
Please give it a spin and report back to the community!

public class RouteExceptionHandlerEnhancer {
   public static void enhance(RouteDefinition def) {
      def.onException(Exception.class).useOriginalMessage().handled(true)
 // catch exceptions
               .setHeader(Exchange.FAILURE_ROUTE_ID,
property(Exchange.FAILURE_ROUTE_ID))    // set route that errored
               .setHeader(Exchange.EXCEPTION_CAUGHT,
simple("${exception.stacktrace}"))
// Store reason for error
               .to(ExchangePattern.InOnly, endpointAMQ(config.
queueCaseAutomationDead()))
          .end();
   }
}


public class MyRoute extends RouteBuilder() {

    @Override
    public void configure() throws Exception {

       RouteDefinition route =
          from("direct:myendpoint")
              .log("Hello ${body")
              .to("activemq:queue:foo");

       RouteExceptionHandlerEnhancer.enhance(route);

    }

}

Regards,

*Raúl Kripalani*
Apache Camel PMC Member & Committer | Enterprise Architect, Open Source
Integration specialist
http://about.me/raulkripalani | http://www.linkedin.com/in/raulkripalani
http://blog.raulkr.net | twitter: @raulvk

On Tue, Apr 1, 2014 at 3:51 PM, kraythe . <kraythe@gmail.com> wrote:

> Greetings:
>
> I have dozens of routes currently in our system and they have one of two
> variants of the following code:
>
>         .onException(Exception.class).useOriginalMessage().handled(true) //
> catch exceptions
> .setHeader(Exchange.FAILURE_ROUTE_ID, property(Exchange.FAILURE_ROUTE_ID))
> // set route that errored
> .setHeader(Exchange.EXCEPTION_CAUGHT, simple("${exception.stacktrace}")) //
> Store reason for error
> .to(ExchangePattern.InOnly,
> endpointAMQ(config.queueCaseAutomationDead())).end() // to DLQ
>
> The problem is I am getting more than annoyed at having to copy paste this
> all over the place but since there are a couple of variants, I cant declare
> a global handler. Making a direct is all well and good but doesnt really
> buy me much as I still have half the handler on every route.
>
> Any ideas how I could reduce code volume here ?
>
> *Robert Simmons Jr. MSc. - Lead Java Architect @ EA*
> *Author of: Hardcore Java (2003) and Maintainable Java (2012)*
> *LinkedIn: **http://www.linkedin.com/pub/robert-simmons/40/852/a39
> <http://www.linkedin.com/pub/robert-simmons/40/852/a39>*
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message