ad3luc
ad3luc

Reputation: 312

Apache Camel. How to manage with a Custom Processor the error raised at runtime

I'm trying to convert my route made with Java DSL in a route made with XML. The following is my original route that works. And what it does is simple. Get as input an array of integer and at runtime throws some errors. At the end of the route I need to read all the error raised by myself and not with a long stacktrace or other long messages in console.

other lines of code...
camelContext.addRoutes(new RouteBuilder() {

            @Override
            public void configure() throws Exception {

                onException(Exception.class)
                        .handled(true)
                        .process(new ErrorProcessor());

                from("direct:start_route")
                        .split(body())
                        .processRef("payloadProcessor")
                        .to("direct:a")
                        .end()
                        .to("direct:d");

                from("direct:a")
                        .beanRef("stupidBean", "stupidMethod");

                from("direct:d")
                        .beanRef("errorProcessor", "check");
            }
        });
other lines of code...

The following is my xml route that doesn't work.

...    
<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="direct:call.playWithPayload" />

        <onException>
            <exception>java.lang.Exception</exception>
            <process ref="errorProcessor" />
        </onException>

        <split onPrepareRef="payloadProcessor">
            <simple>${body}</simple>
            <to uri="direct:call.a" />
        </split>
        <to uri="direct:call.d" />
    </route>

    <!--SUB ROUTE-->
    <route>
        <from uri="direct:call.a" />
        <bean ref="stupidBean" method="stupidMethod" />
    </route>

    <route>
        <from uri="direct:call.d" />
        <bean ref="errorProcessor" method="check" />
    </route>
</camelContext>
...

What I need is that the direct:call.d is called after the split. Thanks to this I can read all the errors added into a List<Exception> that is stored into the header.

I think that the problem is in the onException management. When I try to add the handled to reproduce the my Java DSL

<onException>
...
<handled>
    <constant>
        true
    </constant>
</handled>
...

I got this error:

Invalid content was found starting with element 'handled'. 
One of '{"http://camel.apache.org/schema/spring":description, "http://camel.apache.org/schema/spring":exception}' is expected.

Upvotes: 1

Views: 809

Answers (1)

ad3luc
ad3luc

Reputation: 312

Found solution.

My problem was an incorrect format of my xml route.

 <onException>
     <exception>java.lang.Exception</exception>
     <handled>
         <constant>true</constant>
     </handled>
     <process ref="errorProcessor" />
 </onException>

now it works.

Upvotes: 1

Related Questions