You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by bijoy <bi...@gmail.com> on 2014/01/09 07:57:16 UTC

Handling ConnectException at route startup

Hi,

I'm planning to design an application which can receive files from multiple
transports like JMS, FTP, FILE, REST etc. I'm having trouble to handle the
ConnectException thrown at route startup if any endpoint(JMS,FTP etc.) is
down.

I have designed a global exception handler using onException which is able
to handle the exceptions once the route is started. Consider following
scenarios...

1) Suppose we have a route FILE --> JMS --> Process --> FTP.If FILE
consumer is trying to send a file to JMS and JMS server is down then
ConnectException is thrown and handled by global exception handler. But if
there is no file to consume for FILE consumer and JMS server goes down then
ConnectException is thrown by activemq(the broker I used) but this
exception is not handled by exception handler.

2)  Now if we have a route JMS --> FILE --> Process --> FTP. If the JMS
server is down while trying to start the route we get ConnectException
which is not handled by global exception handler.

After exploring some discussion threads I understood to handle such
ConnectException we need have exception listeners or need to do some socket
programing for individual components. But I don't want to create exception
listeners for individual component. Instead is there any way to globally
handle such ConnectException be it thrown by JMS, FTP or REST or by any
other network component?

Thanks for short feedback!

Regards,
Bijoy




--
View this message in context: http://camel.465427.n5.nabble.com/Handling-ConnectException-at-route-startup-tp5745762.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Handling ConnectException at route startup

Posted by Claus Ibsen <cl...@gmail.com>.
On Fri, Jan 10, 2014 at 1:48 PM, bijoy <bi...@gmail.com> wrote:
> Thanks everyone for your assistance!
>
> The errorHandler for JMS component was not able to handle the
> ConnectException but exceptionListener option was able to do so. Following
> is the code snippet for the same....
>
> <bean id="springErrorHandler" class="faultHandler.SpringErrorHandler"/>
> <bean id="jmsExceptionListener" class="faultHandler.JmsExceptionListener"/>
>  <bean id="edmPollingStartegy" class="strategies.EdmPollingStartegy"/>
>
>  *<camel:camelContext id="sampleContext">
>         <camel:onException>
>             <camel:exception>javax.jms.JMSException</camel:exception>
>             <camel:exception>java.lang.Exception</camel:exception>
>
> <camel:handled><camel:constant>true</camel:constant></camel:handled>
>             <camel:setHeader
> headerName="raiseAlarm"><camel:constant>true</camel:constant></camel:setHeader>
>             <camel:process ref="edmErrorProcessor"/>
>         </camel:onException>
>
> <camel:route id="fileTransport" handleFault="true" >
> <camel:from uri="activemq:queue:TEST?errorHandler=#springErrorHandler"/>
>
> <camel:to
> uri="file:home/ebijcha/Documents/Camel_Work_Dir/FTP_Inbox?noop=true;consumer.bridgeErrorHandler=true"/>
> <camel:to
> uri="ftp://admin@localhost:2121?password=xxxx;fileName=.logout;throwExceptionOnConnectFailed=true;pollStrategy=#edmPollingStartegy"/>*
>
> Also the consumer.bridgeErrorHandler=true option and pollStrategy was not
> able to handle the exceptions at connect/login failures for FTP component.
>
> public class EdmPollingStartegy implements PollingConsumerPollingStrategy{
>         @Override
>         public void afterPoll() throws Exception {
>                 System.out.println("After Poll!");
>         }
>         @Override
>          public long beforePoll(long arg0) throws Exception {
>                 System.out.println("Before Poll!");
>                 return 0;
>         }
>         @Override
>         public void onInit() throws Exception {
>                 System.out.println("Init Poll!");
>         }
> }
>
> None of the methods in pollingStrategy were called. Any idea what could be
> the problem?
>

They are only used on the ftp consumer (eg from in a route).


> Regards,
> Bijoy
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Handling-ConnectException-at-route-startup-tp5745762p5745832.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
Make your Camel applications look hawt, try: http://hawt.io

Re: Handling ConnectException at route startup

Posted by bijoy <bi...@gmail.com>.
Thanks everyone for your assistance!
 
The errorHandler for JMS component was not able to handle the
ConnectException but exceptionListener option was able to do so. Following
is the code snippet for the same....

<bean id="springErrorHandler" class="faultHandler.SpringErrorHandler"/>
<bean id="jmsExceptionListener" class="faultHandler.JmsExceptionListener"/>
 <bean id="edmPollingStartegy" class="strategies.EdmPollingStartegy"/>

 *<camel:camelContext id="sampleContext">
        <camel:onException>
            <camel:exception>javax.jms.JMSException</camel:exception>  
            <camel:exception>java.lang.Exception</camel:exception>   
           
<camel:handled><camel:constant>true</camel:constant></camel:handled>            
            <camel:setHeader
headerName="raiseAlarm"><camel:constant>true</camel:constant></camel:setHeader>
            <camel:process ref="edmErrorProcessor"/>
        </camel:onException>

<camel:route id="fileTransport" handleFault="true" >     
<camel:from uri="activemq:queue:TEST?errorHandler=#springErrorHandler"/>

<camel:to
uri="file:home/ebijcha/Documents/Camel_Work_Dir/FTP_Inbox?noop=true;consumer.bridgeErrorHandler=true"/>
<camel:to
uri="ftp://admin@localhost:2121?password=xxxx;fileName=.logout;throwExceptionOnConnectFailed=true;pollStrategy=#edmPollingStartegy"/>*

Also the consumer.bridgeErrorHandler=true option and pollStrategy was not
able to handle the exceptions at connect/login failures for FTP component.

public class EdmPollingStartegy implements PollingConsumerPollingStrategy{
	@Override
	public void afterPoll() throws Exception {
		System.out.println("After Poll!");		
	}
	@Override
	 public long beforePoll(long arg0) throws Exception {		
		System.out.println("Before Poll!");
		return 0;
	}
	@Override
	public void onInit() throws Exception {
		System.out.println("Init Poll!");		
	}	
}

None of the methods in pollingStrategy were called. Any idea what could be
the problem?

Regards,
Bijoy




--
View this message in context: http://camel.465427.n5.nabble.com/Handling-ConnectException-at-route-startup-tp5745762p5745832.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Handling ConnectException at route startup

Posted by Claus Ibsen <cl...@gmail.com>.
You can use the bridge error handler on the consumer.

See details at
http://camel.apache.org/why-does-my-file-consumer-not-pick-up-the-file-and-how-do-i-let-the-file-consumer-use-the-camel-error-handler.html



On Thu, Jan 9, 2014 at 5:00 PM, Christian Posta
<ch...@gmail.com> wrote:
> Take a look at http://camel.apache.org/polling-consumer.html and the
> pollingStrategy option. This will work for file/ftp components.
>
> The JMS component will have its own errorHandler.
> http://camel.apache.org/jms.html
>
> There is no "global exception handler" to handle exceptions outside of
> the routes. The components are different, the strategies are
> different, and should be handled individually.
>
> On Thu, Jan 9, 2014 at 6:16 AM, baudoust <st...@ubs.com> wrote:
>> Ok I catch the point, we do have the same behaviour in our implementation,
>> which is not a good news at all ...
>>
>> I guess the application server, if any in this case, catch the exception, in
>> our case we are runing under websphere and the error appears in the
>> websphere log, but not is not catch by our monitoring framework.
>>
>> I will then be interested by your solution if you do found any. I would go
>> in the direction having a class starting the camelcontext and catching
>> exception but I am even not sure that something would be thrown at that
>> level ...
>>
>>
>>
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/Handling-ConnectException-at-route-startup-tp5745762p5745787.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
>
> --
> Christian Posta
> http://www.christianposta.com/blog
> twitter: @christianposta



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
Make your Camel applications look hawt, try: http://hawt.io

Re: Handling ConnectException at route startup

Posted by Christian Posta <ch...@gmail.com>.
Take a look at http://camel.apache.org/polling-consumer.html and the
pollingStrategy option. This will work for file/ftp components.

The JMS component will have its own errorHandler.
http://camel.apache.org/jms.html

There is no "global exception handler" to handle exceptions outside of
the routes. The components are different, the strategies are
different, and should be handled individually.

On Thu, Jan 9, 2014 at 6:16 AM, baudoust <st...@ubs.com> wrote:
> Ok I catch the point, we do have the same behaviour in our implementation,
> which is not a good news at all ...
>
> I guess the application server, if any in this case, catch the exception, in
> our case we are runing under websphere and the error appears in the
> websphere log, but not is not catch by our monitoring framework.
>
> I will then be interested by your solution if you do found any. I would go
> in the direction having a class starting the camelcontext and catching
> exception but I am even not sure that something would be thrown at that
> level ...
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Handling-ConnectException-at-route-startup-tp5745762p5745787.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Christian Posta
http://www.christianposta.com/blog
twitter: @christianposta

Re: Handling ConnectException at route startup

Posted by baudoust <st...@ubs.com>.
Ok I catch the point, we do have the same behaviour in our implementation,
which is not a good news at all ...

I guess the application server, if any in this case, catch the exception, in
our case we are runing under websphere and the error appears in the
websphere log, but not is not catch by our monitoring framework.

I will then be interested by your solution if you do found any. I would go
in the direction having a class starting the camelcontext and catching
exception but I am even not sure that something would be thrown at that
level ...



--
View this message in context: http://camel.465427.n5.nabble.com/Handling-ConnectException-at-route-startup-tp5745762p5745787.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Handling ConnectException at route startup

Posted by bijoy <bi...@gmail.com>.
Hi Baudoust,

I want to configure the routes dynamically with Spring DSL. Whatever you
mentioned is achieved putting <onException> under context scope as below...

<camel:camelContext id="sampleContext">
        *<camel:onException>
            <camel:exception>java.lang.Exception</camel:exception>   
           
<camel:handled><camel:constant>true</camel:constant></camel:handled>
            
            <camel:setHeader
headerName="raiseAlarm"><camel:constant>true</camel:constant></camel:setHeader>
            <camel:process ref="edmErrorProcessor"/>
        </camel:onException> *

        <camel:route id="fileTransport" handleFault="true">
            <camel:from uri="activemq:queue:test"/>
            <camel:process ref="edmMessageProcessor"/>
            <camel:to
uri="ftp://localhost:2121?username=admin;password=admin;fileName=pom.xml;throwExceptionOnConnectFailed=true"/>
            <camel:log message="Processing ${header.CamelFileName}
file!!!"></camel:log>         
            <camel:to
uri="file:///home/ebijcha/Documents/Camel_Work_Dir/FTP_Inbox"/>         
    </camel:camelContext>

So I'm able to handle the exceptions when route is established without any
startup error and when exchange object is created. But if ConnectException
occurs at very first while connecting to JMS server, the exchange object
does not get created  and the exception is not propagated to camel. Thus the
exception is not being handled by global handler. Same is the case for FTP
component.

Is there anyway to handle those exceptions globally?

Regards,
Bijoy 





--
View this message in context: http://camel.465427.n5.nabble.com/Handling-ConnectException-at-route-startup-tp5745762p5745782.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Handling ConnectException at route startup

Posted by baudoust <st...@ubs.com>.
Hi,

what if you declare a general handling in a super class like :

public abstract class CommonHandler extends RouteBuilder {

 @Override public void configure() throws Exception {    	
    	
    	// default Technical Exception handling
        onException(Exception.class)

          do your job

}

Then just define your route in one or several class which does extends
CommonHandler

like:

public class MyRouteDefinition extends CommonHandler {

@Override public void configure() throws Exception {

        super.configure();

        from(xxx).routeId(yyy).yourjob
}
}

Hopes it helps ...

Regards.



--
View this message in context: http://camel.465427.n5.nabble.com/Handling-ConnectException-at-route-startup-tp5745762p5745780.html
Sent from the Camel - Users mailing list archive at Nabble.com.