You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by j vh <jv...@hotmail.com> on 2022/06/16 14:44:06 UTC

RE: Apache Camel Exception Handling doubt

Hello,
I am also trying to use global exception configuration in a Camel route that is built from a template. The template is created ok and the route created from it is also ok & working. But the global exception handling is not working as expected.

Maybe there is another way to customize the exception handling in the route built from the template?

For example is there some way using a bean?
Or maybe via the .configure that is documented here: https://camel.apache.org/manual/route-template.html.

Thanks,
jvh003

On 2022/03/31 15:32:21 "Chawla, Manan" wrote:
> Hey,
> I am trying to integrate Global OnException block with route template but the onException block isn’t getting executed. Can you please suggest me a way to get around this issue?
> Thank you
> Regards,
> Manan Chawla
> 

Re: Apache Camel Exception Handling doubt

Posted by Ephemeris Lappis <ep...@gmail.com>.
Hello.

Ideally, I'd like to replace the long repetitive code from all the
existing projects blueprints (and future projects too). We have
something like the following sequence in every project :

<?xml version="1.0" encoding="UTF-8"?>
<blueprint
    xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
    xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0
https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprintz.xsd
    http://camel.apache.org/schema/blueprint
http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
    http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0
http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd">

    <reference
        id="myUnitOfWorkFactory"
        interface="x.y.z.MyMDCUnitOfWorkFactory" />

    <reference
        id="support"
        interface="x.y.z.Support" />

<bean
        id="oneEventNotifier"
        class="org.apache.camel.spi.EventNotifier"
        factory-ref="support"
        factory-method="makeSupportEventNotifierForX" />
 <bean
        id="otherEventNotifier"
        class="org.apache.camel.spi.EventNotifier"
        factory-ref="support"
        factory-method="makeSupportEventNotifierForY" />

<reference
        id="tools"
        interface="x.y.z.Tools" />

    <reference
        id="myDS"
        interface="javax.sql.DataSource"
        filter="(osgi.jndi.service.name=jdbc/our-ds)" />

    <camelContext
        id="my-context"
        streamCache="true"
        useMDCLogging="true"
        xmlns="http://camel.apache.org/schema/blueprint">

And it should be easier to manage provided services, beans and so on
changing only the CamelContext or its associated registry. Something
like that :

<?xml version="1.0" encoding="UTF-8"?>
<blueprint
    xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
    xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0
https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprintz.xsd
    http://camel.apache.org/schema/blueprint
http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
    http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0
http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd">

    <reference
        id="myCamelContextFactory"
        interface="???" />

    <camelContext
        id="my-context"
        streamCache="true"
        useMDCLogging="true"
        xmlns="http://camel.apache.org/schema/blueprint">

Then, adding new services from an external part of the system doesn't
need projects modifications any more.
What is my problem, is what kind of "hook" I can use to change the
CamelContext a,d/or its Registry.

Is it clearer ?

Anyway, thanks again for your help.

Regards.

Le ven. 17 juin 2022 à 14:28, ski n <ra...@gmail.com> a écrit :
>
> Hi,
>
> It's good to provide a code example to see what you tried.
>
> Have you also tried to combine it with route configuration?
>
> https://camel.apache.org/manual/route-configuration.html
>
> This is available from 3.12.
>
> Raymond
>
> On Thu, Jun 16, 2022 at 7:43 PM j vh <jv...@hotmail.com> wrote:
>
> > Hello,
> > btw... I'm using Camel 3.13.0 for this work.
> > ...jvh003

Re: Apache Camel Exception Handling doubt

Posted by ski n <ra...@gmail.com>.
Hi,

I looked at your at your code and the idea and code looks just fine (thus
the new approach with routeConfiguration).

I would suggest to test this code on the latest version (3.17.0) and if
this still gives the same results, create a Jira ticket for it (including
your example):

https://issues.apache.org/jira/projects/CAMEL/issues


That said there maybe other ways to work around this. I started
implementing routes in Camel 2, so there weren't route templates yet and
used alternative approaches. Here are some ways I handled errors:


1) Custom error class

I created a custom error class "ErrorHandler".

Example code to call the errorhandler from a route:

@Override
public void configure() {

  routeErrorHandler = deadLetterChannel(errorUri);
  ErrorHandler errorHandler = new ErrorHandler(routeErrorHandler, props);
  routeErrorHandler = errorHandler.configure();

  from(fromUri)
    .errorHandler(routeErrorHandler)
    .log("someLog");

}

Here is my errorhandler implementation:

https://github.com/assimbly/modules/blob/develop/integrationModule/src/main/java/org/assimbly/integration/routes/errorhandler/ErrorHandler.java


2) Generate a route with the XML DSL:

Generate a route as XML (You may use a template engine like FreeMarker,
VeloCity or Mustache). The result of the template could look like this:

<routeConfiguration id="xmlError">
    <onException>
        <exception>java.lang.Exception</exception>
        <handled><constant>true</constant></handled>
        <log message="XML WARN: ${exception.message}"/>
    </onException></routeConfiguration>


And then add the routeConfiguration (can also be templated):

<route routeConfigurationId="xmlError">
    <from uri="timer:xml?period=5s"/>
    <log message="I am XML"/>
    <throwException exceptionType="java.lang.Exception" message="Some
kind of XML error"/></route>

The nice thing about template engines is that you can also you
conditionals or just remove/add stuff during the generation of the
XML.
The functionality to dynamically included/exclude route steps isn't
part of Camel route templates yet.


3. Run XML routes in the browser

In my open source project Assimbly, I also added ways to configure
routes as XML in the browser (including route configurations). This
was added in the

current beta: https://github.com/assimbly/gateway/releases/tag/3.7.0Beta.

You can check it here: https://github.com/assimbly/gateway/wiki/quick-start


Good luck,

Raymond










On Fri, Jun 17, 2022 at 8:37 PM j vh <jv...@hotmail.com> wrote:

> Hi Raymond,
> Thanks for the suggestion. But seems that defining exception handling via
> the RouteConfigurationBuilder doesn't work either.
> Let me further explain with some piece of code.
> Maybe this will trigger some thoughts as you'll be able to see what I'm
> trying to do here.
>
> Hi-level overview:
> - 2 projects: 1 camel template base jar and 1 springboot camel application
> (that builds its route using the template from the other project).
> - the base jar builds a route template
> - the application jar builds a route using the template from the base jar
> - the desire here is to do some extra customization in the application (at
> templated-route creation time) to setup extra exception handling that is
> not included in the base template.
> - I have also tried to define global level exception handling using
> onException() in my application project's configure() method. But this is
> not catching anything either.
>
> Thanks,
> jvh
>
> ps: the post in between here by Ephemeris Lappis seems to be targeted for
> a different thread.   :-)
>
> Code snippets:
> 1) Base project route configure method:
>
>     @Override
>     public void configure() {
>       final RouteTemplateDefinition rtd = routeTemplate("route-template-1")
>             .templateParameter("queue-name")
>             .templateParameter("rest-endpoint")
>
>             .from("jmsBroker:queue:{{queue-name}}")
>             // .routeConfigurationId("test-route-configuration-id")    //
> tried to add (and remove the local .onException from here)
>
>            .onException(ConnectException.class)
>               .handled(false)
>               .log(LoggingLevel.ERROR, logger, "--> Exception:
> ${exception.message}, Delivery was rolled back")
>               .end()
>
>          .process(restHeaderProcessor)
>          .to("{{rest-endpoint}}")
>          .log(LoggingLevel.INFO, logger, "Message sent, ResponseCode:
> ${header.CamelHttpResponseCode}");
>     }
>
> 2) Application project configure method, building route from base project
> template:
>
>     @Override
>     public void configure() {
>       final String route = TemplatedRouteBuilder.builder(camelContext,
> "route-template-1")
>             .routeId("route-jms-to-rest")
>             .parameter("myQueue")
>             .parameter("http://localhost:9000/myRestEndpoint")
>             .add();
>       log.info("buildTemplatedRoute() created templated route: {}",
> route);
>     }
>
> 3)  Application project Configuration spring boot component class:
>
> @Component
> public class MyCustomErrorHandler extends RouteConfigurationBuilder {
>
>    @Override
>    public void configuration() throws Exception {
>       routeConfiguration("test-route-configuration-id")
>       .onException(Exception.class)
>          .handled(true)  // discard the msg
>          .log(LoggingLevel.INFO, ">>>>> Hit this custom error handler:
> ${exception.message}")
>          .end();
>    }
> }
>
>
> ________________________________________
> From: ski n <ra...@gmail.com>
> Sent: June 17, 2022 8:28 AM
> To: users@camel.apache.org
> Subject: Re: Apache Camel Exception Handling doubt
>
> Hi,
>
> It's good to provide a code example to see what you tried.
>
> Have you also tried to combine it with route configuration?
>
> https://camel.apache.org/manual/route-configuration.html
>
> This is available from 3.12.
>
> Raymond
>
> On Thu, Jun 16, 2022 at 7:43 PM j vh <jv...@hotmail.com> wrote:
>
> > Hello,
> > btw... I'm using Camel 3.13.0 for this work.
> > ...jvh003
>

Re: Apache Camel Exception Handling doubt

Posted by j vh <jv...@hotmail.com>.
Hi Raymond,
Thanks for the suggestion. But seems that defining exception handling via the RouteConfigurationBuilder doesn't work either.
Let me further explain with some piece of code.
Maybe this will trigger some thoughts as you'll be able to see what I'm trying to do here.

Hi-level overview:
- 2 projects: 1 camel template base jar and 1 springboot camel application (that builds its route using the template from the other project).
- the base jar builds a route template
- the application jar builds a route using the template from the base jar
- the desire here is to do some extra customization in the application (at templated-route creation time) to setup extra exception handling that is not included in the base template.
- I have also tried to define global level exception handling using onException() in my application project's configure() method. But this is not catching anything either.

Thanks,
jvh

ps: the post in between here by Ephemeris Lappis seems to be targeted for a different thread.   :-)

Code snippets:
1) Base project route configure method:

    @Override
    public void configure() {
      final RouteTemplateDefinition rtd = routeTemplate("route-template-1")
            .templateParameter("queue-name")
            .templateParameter("rest-endpoint")

            .from("jmsBroker:queue:{{queue-name}}")
            // .routeConfigurationId("test-route-configuration-id")    // tried to add (and remove the local .onException from here)

           .onException(ConnectException.class)
              .handled(false)
              .log(LoggingLevel.ERROR, logger, "--> Exception: ${exception.message}, Delivery was rolled back")
              .end()

         .process(restHeaderProcessor)
         .to("{{rest-endpoint}}")
         .log(LoggingLevel.INFO, logger, "Message sent, ResponseCode: ${header.CamelHttpResponseCode}");
    }

2) Application project configure method, building route from base project template:

    @Override
    public void configure() {
      final String route = TemplatedRouteBuilder.builder(camelContext, "route-template-1")
            .routeId("route-jms-to-rest")
            .parameter("myQueue")
            .parameter("http://localhost:9000/myRestEndpoint")
            .add();
      log.info("buildTemplatedRoute() created templated route: {}", route);
    }

3)  Application project Configuration spring boot component class:

@Component
public class MyCustomErrorHandler extends RouteConfigurationBuilder {

   @Override
   public void configuration() throws Exception {
      routeConfiguration("test-route-configuration-id")
      .onException(Exception.class)
         .handled(true)  // discard the msg
         .log(LoggingLevel.INFO, ">>>>> Hit this custom error handler: ${exception.message}")
         .end();
   }
}


________________________________________
From: ski n <ra...@gmail.com>
Sent: June 17, 2022 8:28 AM
To: users@camel.apache.org
Subject: Re: Apache Camel Exception Handling doubt

Hi,

It's good to provide a code example to see what you tried.

Have you also tried to combine it with route configuration?

https://camel.apache.org/manual/route-configuration.html

This is available from 3.12.

Raymond

On Thu, Jun 16, 2022 at 7:43 PM j vh <jv...@hotmail.com> wrote:

> Hello,
> btw... I'm using Camel 3.13.0 for this work.
> ...jvh003

Re: Apache Camel Exception Handling doubt

Posted by ski n <ra...@gmail.com>.
Hi,

It's good to provide a code example to see what you tried.

Have you also tried to combine it with route configuration?

https://camel.apache.org/manual/route-configuration.html

This is available from 3.12.

Raymond

On Thu, Jun 16, 2022 at 7:43 PM j vh <jv...@hotmail.com> wrote:

> Hello,
> btw... I'm using Camel 3.13.0 for this work.
> ...jvh003

Re: Apache Camel Exception Handling doubt

Posted by j vh <jv...@hotmail.com>.
Hello,
btw... I'm using Camel 3.13.0 for this work.
...jvh003