Kiran Kumar
Kiran Kumar

Reputation: 11

Continue the exception from one camel route and handle it in another camel route

I have an Apache Camel route which invokes restlet component and would like to use the redelivery mechanism from the exception handler which performs some processing on every failure to update my database record which should be done on generic route. So, I'm trying to continue the exception in one route and handle the same exception in another route through direct component. But exception is not continuing to generic route.

1st route:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:cxf="http://camel.apache.org/schema/cxf"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="  http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd   http://camel.apache.org/schema/spring       http://camel.apache.org/schema/spring/camel-spring.xsd  http://camel.apache.org/schema/cxf    http://camel.apache.org/schema/cxf/camel-cxf.xsd">
    <!-- CXF Rest Endpoint Declaration -->
    <cxf:rsServer address="http://localhost:9092/rest/corp"
        id="FetchCDFRestRequest" serviceClass="com.tcl.Service.Service" />

    <bean class="com.tcl.ExceptionOccurredRefProcessor" id="exProc" />

    <bean class="org.apache.camel.builder.DeadLetterChannelBuilder"
        id="DLCErrorHandler">
        <property name="deadLetterUri"
            value="activemq:queue:DMS.FAILURES.DLQ" />
        <property name="redeliveryPolicy" ref="redeliveryPolicy" />
    </bean>
    <bean class="org.apache.camel.processor.RedeliveryPolicy"
        id="redeliveryPolicy">
        <property name="maximumRedeliveries"
            value="3" />
        <property name="maximumRedeliveryDelay" value="2000" />
        <property name="retryAttemptedLogLevel" value="WARN" />
    </bean>
    <camelContext id="Corp"
        xmlns="http://camel.apache.org/schema/spring">

        <errorHandler id="eh" onExceptionOccurredRef="exProc">
            <redeliveryPolicy id="redeliveryPolicy" />
        </errorHandler>

        <route id="MainRouteOppIDFolder" streamCache="true">
            <from id="_CreateOppIDFolder"
                uri="restlet:http://localhost:9092/rest/corp/createOppIDFolder?restletMethod=POST" />
            ----------
            <to uri="restlet:http://localhost:9902/CreateOppIDFolder?restletMethod=POST" />
            ------------    
            <onException id="_onException1" useOriginalMessage="true">
                <exception>java.lang.Exception</exception>
                <handled>
                    <simple>false</simple>
                </handled>
                <log id="_log1" message="Cont... ex >>>>> ${exception.message} "/>
                <to uri="direct:ExceptionHandle"/>
            </onException>
        </route>
    </camelContext>
</beans>

2nd route:

<route errorHandlerRef="DLCErrorHandler" >
            <from id="_from2" uri="direct:ExceptionHandle"/>
            <log id="_log4" message="Req>>>>> ${body}"/>
            <onException id="_onException2"
                onExceptionOccurredRef="exProc" redeliveryPolicyRef="redeliveryPolicy">
                <exception>java.lang.Exception</exception>
                <handled>
                    <simple>true</simple>
                </handled>
                <log id="_log3" loggingLevel="INFO" message="Handled ex >>>>> ${exception.message} "/>
                <choice id="_choice1">
                    <when id="_when1">
                        <simple>${header.errorMessage} contains 'Could not send Message' || ${header.errorMessage} contains 'Connection timed out'</simple>
                        <to id="_to1" pattern="OutOnly" uri="activemq:queue:DMS.FAILURES"/>
                    </when>
                    <otherwise id="_otherwise1">
                        <log id="_log5" loggingLevel="ERROR" message="Exception occured in otherwise block >>>>>> ${exception.stacktrace} "/>
                    </otherwise>
                </choice>
            </onException>
        </route>

12:11:24.384 [Restlet-781390346] WARN  o.a.c.processor.DeadLetterChannel - Failed delivery for (MessageId: ID-DESKTOP-P2DBOO5-1580280046927-0-8 on ExchangeId: ID-DESKTOP-P2DBOO5-1580280046927-0-7).On delivery attempt: 0 caught: org.apache.camel.component.restlet.RestletOperationException: Restlet operation failed invoking http://localhost:9902/CreateOppIDFolder with statusCode: 500 /n responseBody:org.apache.cxf.interceptor.Fault: Could not send Message.
Caused by: java.net.ConnectException: ConnectException invoking http://172.16.18.113:7001/services/TCLDMSSFDCService/TCLDMSSFDCService: Connection timed out: connect
Caused by: java.net.ConnectException: Connection timed out: connect

12:11:24.392 [Restlet-781390346] INFO  MainRouteOppIDFolder - Cont... ex >>>>> Restlet operation failed invoking http://localhost:9902/CreateOppIDFolder with statusCode: 500 /n responseBody:org.apache.cxf.interceptor.Fault: Could not send Message.
Caused by: java.net.ConnectException: ConnectException invoking http://172.16.18.113:7001/services/TCLDMSSFDCService/TCLDMSSFDCService: Connection timed out: connect
Caused by: java.net.ConnectException: Connection timed out: connect

12:11:24.396 [Restlet-781390346] INFO  _route1 - Req>>>>> {
  "oppID" : "10268216",
  "salesOrganization" : "Commerical Finance",
  "salesVertical" : "CEQ",
  "productName" : "Construction Equipment Finance",
  "applicantName" : "SGS INFRASTRUCTURE",
  "docName" : null,
  "appNature" : null,
  "docType" : null,
  "base64Content" : null
}

12:11:24.405 [Restlet-781390346] ERROR o.a.c.processor.DeadLetterChannel - Failed delivery for (MessageId: ID-DESKTOP-P2DBOO5-1580280046927-0-8 on ExchangeId: ID-DESKTOP-P2DBOO5-1580280046927-0-7). Exhausted after delivery attempt: 1 caught: org.apache.camel.component.restlet.RestletOperationException: Restlet operation failed invoking http://localhost:9902/CreateOppIDFolder with statusCode: 500 /n responseBody:org.apache.cxf.interceptor.Fault: Could not send Message.
Caused by: java.net.ConnectException: ConnectException invoking http://172.16.18.113:7001/services/TCLDMSSFDCService/TCLDMSSFDCService: Connection timed out: connect
Caused by: java.net.ConnectException: Connection timed out: connect
. Processed by failure processor: FatalFallbackErrorHandler[Pipeline[[Channel[Log(MainRouteOppIDFolder)[Cont... ex >>>>> ${exception.message} ]], Channel[sendTo(direct://ExceptionHandle)], Channel[Log(MainRouteOppIDFolder)]]]]

Message History
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[MainRouteOppIDFold] [MainRouteOppIDFold] [restlet://http://localhost:9092/rest/corp/createOppIDFolder?restletMethod=POST] [     21608]
[MainRouteOppIDFold] [_CreateOppIDFolder] [restlet:http://localhost:9902/CreateOppIDFolder?restletMethod=POST            ] [     21319]
[MainRouteOppIDFold] [_to2              ] [direct:ExceptionHandle                                                        ] [         1]
[_route1           ] [_log4             ] [log                                                                           ] [         1]

Stacktrace
org.apache.camel.component.restlet.RestletOperationException: Restlet operation failed invoking http://localhost:9902/CreateOppIDFolder with statusCode: 500 /n responseBody:org.apache.cxf.interceptor.Fault: Could not send Message.
Caused by: java.net.ConnectException: ConnectException invoking http://172.16.18.113:7001/services/TCLDMSSFDCService/TCLDMSSFDCService: Connection timed out: connect
Caused by: java.net.ConnectException: Connection timed out: connect```

**Any suggestions please?**

Upvotes: 0

Views: 1846

Answers (1)

burki
burki

Reputation: 7035

To be honest: I don't see your problem.

Your exception block does

<log id="_log1" message="Cont... ex >>>>> ${exception.message} "/>
<to uri="direct:ExceptionHandle"/>

The direct route does

<log id="_log4" message="Req>>>>> ${body}"/>

And your log shows

Cont... ex >>>>> [error message]
Req>>>>> [message body]

So everything works as you expect. Am I missing something?

Update due to comment

The onException block of the second route is NOT called as long as no Exception occurs in the second route. The second route normally receives a message and processes it.

You can still access the exception information on the message with ${exception}.

Upvotes: 1

Related Questions