You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Jim Newsham <jn...@referentia.com> on 2011/04/19 00:03:30 UTC

exception handlers and jms (connection pool) failure

Hi,

We are using Camel 2.5.0 and ActiveMQ 5.4.1.  In our application, we see 
that failure to obtain a jms connection from the connection pool results 
in an unhandled exception which bubbles up to the caller, instead of it 
being handled by one of the global exception handlers that we have 
configured.  Is this expected behavior or a bug?

Below is a simple test case (code and output) which demonstrates what we 
are seeing.  We intentionally configure an activemq thread pool which 
will fail to create a connection, and then send a message to an activemq 
queue.  We expect the global exception handler for JMSException to 
handle the failure; instead, it is caught by the caller's (sender's) 
try/catch block.

package test;

import javax.jms.Connection;
import javax.jms.JMSException;
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class TestCamelJmsExceptionHandling {

   private static final String QUEUE_URL = "activemq:queue:myqueue";

   public static void main(String... args) throws Exception {
     CamelContext context = new DefaultCamelContext();
     ActiveMQComponent activemqComponent = (ActiveMQComponent) 
context.getComponent("activemq");
     activemqComponent.setConnectionFactory(new PooledConnectionFactory() {
       public Connection createConnection(String userName, String 
password) throws JMSException {
         throw new JMSException("can't create connection");
       }
     });

     context.addRoutes(new RouteBuilder() {
       public void configure() throws Exception {
         onException(JMSException.class).log("handled jms exception");
         onException(Throwable.class).log("handled throwable");
       }
     });
     context.start();

     ProducerTemplate producerTemplate = context.createProducerTemplate();
     try {
       producerTemplate.sendBody(QUEUE_URL, "test");
     }
     catch(Exception e) {
       System.err.println("caught exception");
       e.printStackTrace();
     }
   }

}

- Apache Camel 2.5.0 (CamelContext: camel-1) is starting
- JMX enabled. Using ManagedManagementStrategy.
- Found 4 packages with 15 @Converter classes to load
- Loaded 149 type converters in 0.449 seconds
- Total 0 routes, of which 0 is started.
- Apache Camel 2.5.0 (CamelContext: camel-1) started in 0.771 seconds
caught exception
org.apache.camel.CamelExecutionException: Exception occurred during 
execution on the exchange: Exchange[Message: test]
     at 
org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1156)
     at 
org.apache.camel.util.ExchangeHelper.extractResultBody(ExchangeHelper.java:456)
     at 
org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:441)
     at 
org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:437)
     at 
org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:125)
     at 
org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:130)
     at 
util.TestCamelJmsExceptionHandling.main(TestCamelJmsExceptionHandling.java:50)
Caused by: org.springframework.jms.UncategorizedJmsException: 
Uncategorized exception occured during JMS processing; nested exception 
is javax.jms.JMSException: can't create connection
     at 
org.springframework.jms.support.JmsUtils.convertJmsAccessException(JmsUtils.java:316)
     at 
org.springframework.jms.support.JmsAccessor.convertJmsAccessException(JmsAccessor.java:168)
     at 
org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:469)
     at 
org.apache.camel.component.jms.JmsConfiguration$CamelJmsTemplate.send(JmsConfiguration.java:171)
     at 
org.apache.camel.component.jms.JmsProducer.doSend(JmsProducer.java:315)
     at 
org.apache.camel.component.jms.JmsProducer.processInOnly(JmsProducer.java:269)
     at 
org.apache.camel.component.jms.JmsProducer.process(JmsProducer.java:93)
     at 
org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:99)
     at 
org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:91)
     at 
org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:85)
     at 
org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:63)
     at 
org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:333)
     at 
org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:303)
     at 
org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:208)
     at 
org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:303)
     at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:154)
     at 
org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:110)
     at 
org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:123)
     ... 2 more
Caused by: javax.jms.JMSException: can't create connection
     at 
util.TestCamelJmsExceptionHandling$1.createConnection(TestCamelJmsExceptionHandling.java:36)
     at 
org.apache.activemq.pool.PooledConnectionFactory.createConnection(PooledConnectionFactory.java:92)
     at 
org.springframework.jms.support.JmsAccessor.createConnection(JmsAccessor.java:184)
     at 
org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:456)
     ... 17 more

Thanks,
Jim

Re: exception handlers and jms (connection pool) failure

Posted by Jim Newsham <jn...@referentia.com>.
Thanks Willem, I didn't realize that the error handlers would take no 
part if the producerTemplate didn't send a message to a defined route.  
Bad assumption on my part.

Regards,
Jim

On 4/18/2011 8:52 PM, Willem Jiang wrote:
> Hi,
>
> You are using the producerTemplate to send the message, and there is 
> no any camel route involved.
> If you want to test the camel route with the error handler you can add 
> route like this
>
> from("direct:start").to("activemq:queue:myqueue");
>
> Willem
> On 4/19/11 6:03 AM, Jim Newsham wrote:
>>
>> Hi,
>>
>> We are using Camel 2.5.0 and ActiveMQ 5.4.1. In our application, we see
>> that failure to obtain a jms connection from the connection pool results
>> in an unhandled exception which bubbles up to the caller, instead of it
>> being handled by one of the global exception handlers that we have
>> configured. Is this expected behavior or a bug?
>>
>> Below is a simple test case (code and output) which demonstrates what we
>> are seeing. We intentionally configure an activemq thread pool which
>> will fail to create a connection, and then send a message to an activemq
>> queue. We expect the global exception handler for JMSException to handle
>> the failure; instead, it is caught by the caller's (sender's) try/catch
>> block.
>>
>> package test;
>>
>> import javax.jms.Connection;
>> import javax.jms.JMSException;
>> import org.apache.activemq.camel.component.ActiveMQComponent;
>> import org.apache.activemq.pool.PooledConnectionFactory;
>> import org.apache.camel.CamelContext;
>> import org.apache.camel.ProducerTemplate;
>> import org.apache.camel.builder.RouteBuilder;
>> import org.apache.camel.impl.DefaultCamelContext;
>>
>> public class TestCamelJmsExceptionHandling {
>>
>> private static final String QUEUE_URL = "activemq:queue:myqueue";
>>
>> public static void main(String... args) throws Exception {
>> CamelContext context = new DefaultCamelContext();
>> ActiveMQComponent activemqComponent = (ActiveMQComponent)
>> context.getComponent("activemq");
>> activemqComponent.setConnectionFactory(new PooledConnectionFactory() {
>> public Connection createConnection(String userName, String password)
>> throws JMSException {
>> throw new JMSException("can't create connection");
>> }
>> });
>>
>> context.addRoutes(new RouteBuilder() {
>> public void configure() throws Exception {
>> onException(JMSException.class).log("handled jms exception");
>> onException(Throwable.class).log("handled throwable");
>> }
>> });
>> context.start();
>>
>> ProducerTemplate producerTemplate = context.createProducerTemplate();
>> try {
>> producerTemplate.sendBody(QUEUE_URL, "test");
>> }
>> catch(Exception e) {
>> System.err.println("caught exception");
>> e.printStackTrace();
>> }
>> }
>>
>> }
>>
>> - Apache Camel 2.5.0 (CamelContext: camel-1) is starting
>> - JMX enabled. Using ManagedManagementStrategy.
>> - Found 4 packages with 15 @Converter classes to load
>> - Loaded 149 type converters in 0.449 seconds
>> - Total 0 routes, of which 0 is started.
>> - Apache Camel 2.5.0 (CamelContext: camel-1) started in 0.771 seconds
>> caught exception
>> org.apache.camel.CamelExecutionException: Exception occurred during
>> execution on the exchange: Exchange[Message: test]
>> at
>> org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1156) 
>>
>>
>> at
>> org.apache.camel.util.ExchangeHelper.extractResultBody(ExchangeHelper.java:456) 
>>
>>
>> at
>> org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:441) 
>>
>>
>> at
>> org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:437) 
>>
>>
>> at
>> org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:125) 
>>
>>
>> at
>> org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:130) 
>>
>>
>> at
>> util.TestCamelJmsExceptionHandling.main(TestCamelJmsExceptionHandling.java:50) 
>>
>>
>> Caused by: org.springframework.jms.UncategorizedJmsException:
>> Uncategorized exception occured during JMS processing; nested exception
>> is javax.jms.JMSException: can't create connection
>> at
>> org.springframework.jms.support.JmsUtils.convertJmsAccessException(JmsUtils.java:316) 
>>
>>
>> at
>> org.springframework.jms.support.JmsAccessor.convertJmsAccessException(JmsAccessor.java:168) 
>>
>>
>> at 
>> org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:469)
>> at
>> org.apache.camel.component.jms.JmsConfiguration$CamelJmsTemplate.send(JmsConfiguration.java:171) 
>>
>>
>> at 
>> org.apache.camel.component.jms.JmsProducer.doSend(JmsProducer.java:315)
>> at
>> org.apache.camel.component.jms.JmsProducer.processInOnly(JmsProducer.java:269) 
>>
>>
>> at 
>> org.apache.camel.component.jms.JmsProducer.process(JmsProducer.java:93)
>> at
>> org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:99) 
>>
>>
>> at
>> org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:91) 
>>
>>
>> at
>> org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:85) 
>>
>>
>> at
>> org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:63) 
>>
>>
>> at
>> org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:333) 
>>
>> at
>> org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:303) 
>>
>> at 
>> org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:208)
>> at 
>> org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:303)
>> at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:154)
>> at
>> org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:110) 
>>
>>
>> at
>> org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:123) 
>>
>>
>> ... 2 more
>> Caused by: javax.jms.JMSException: can't create connection
>> at
>> util.TestCamelJmsExceptionHandling$1.createConnection(TestCamelJmsExceptionHandling.java:36) 
>>
>>
>> at
>> org.apache.activemq.pool.PooledConnectionFactory.createConnection(PooledConnectionFactory.java:92) 
>>
>>
>> at
>> org.springframework.jms.support.JmsAccessor.createConnection(JmsAccessor.java:184) 
>>
>>
>> at 
>> org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:456)
>> ... 17 more
>>
>> Thanks,
>> Jim
>>
>
>


Re: exception handlers and jms (connection pool) failure

Posted by Willem Jiang <wi...@gmail.com>.
Hi,

You are using the producerTemplate to send the message, and there is no 
any camel route involved.
If you want to test the camel route with the error handler you can add 
route like this

from("direct:start").to("activemq:queue:myqueue");

Willem
On 4/19/11 6:03 AM, Jim Newsham wrote:
>
> Hi,
>
> We are using Camel 2.5.0 and ActiveMQ 5.4.1. In our application, we see
> that failure to obtain a jms connection from the connection pool results
> in an unhandled exception which bubbles up to the caller, instead of it
> being handled by one of the global exception handlers that we have
> configured. Is this expected behavior or a bug?
>
> Below is a simple test case (code and output) which demonstrates what we
> are seeing. We intentionally configure an activemq thread pool which
> will fail to create a connection, and then send a message to an activemq
> queue. We expect the global exception handler for JMSException to handle
> the failure; instead, it is caught by the caller's (sender's) try/catch
> block.
>
> package test;
>
> import javax.jms.Connection;
> import javax.jms.JMSException;
> import org.apache.activemq.camel.component.ActiveMQComponent;
> import org.apache.activemq.pool.PooledConnectionFactory;
> import org.apache.camel.CamelContext;
> import org.apache.camel.ProducerTemplate;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.impl.DefaultCamelContext;
>
> public class TestCamelJmsExceptionHandling {
>
> private static final String QUEUE_URL = "activemq:queue:myqueue";
>
> public static void main(String... args) throws Exception {
> CamelContext context = new DefaultCamelContext();
> ActiveMQComponent activemqComponent = (ActiveMQComponent)
> context.getComponent("activemq");
> activemqComponent.setConnectionFactory(new PooledConnectionFactory() {
> public Connection createConnection(String userName, String password)
> throws JMSException {
> throw new JMSException("can't create connection");
> }
> });
>
> context.addRoutes(new RouteBuilder() {
> public void configure() throws Exception {
> onException(JMSException.class).log("handled jms exception");
> onException(Throwable.class).log("handled throwable");
> }
> });
> context.start();
>
> ProducerTemplate producerTemplate = context.createProducerTemplate();
> try {
> producerTemplate.sendBody(QUEUE_URL, "test");
> }
> catch(Exception e) {
> System.err.println("caught exception");
> e.printStackTrace();
> }
> }
>
> }
>
> - Apache Camel 2.5.0 (CamelContext: camel-1) is starting
> - JMX enabled. Using ManagedManagementStrategy.
> - Found 4 packages with 15 @Converter classes to load
> - Loaded 149 type converters in 0.449 seconds
> - Total 0 routes, of which 0 is started.
> - Apache Camel 2.5.0 (CamelContext: camel-1) started in 0.771 seconds
> caught exception
> org.apache.camel.CamelExecutionException: Exception occurred during
> execution on the exchange: Exchange[Message: test]
> at
> org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1156)
>
> at
> org.apache.camel.util.ExchangeHelper.extractResultBody(ExchangeHelper.java:456)
>
> at
> org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:441)
>
> at
> org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:437)
>
> at
> org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:125)
>
> at
> org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:130)
>
> at
> util.TestCamelJmsExceptionHandling.main(TestCamelJmsExceptionHandling.java:50)
>
> Caused by: org.springframework.jms.UncategorizedJmsException:
> Uncategorized exception occured during JMS processing; nested exception
> is javax.jms.JMSException: can't create connection
> at
> org.springframework.jms.support.JmsUtils.convertJmsAccessException(JmsUtils.java:316)
>
> at
> org.springframework.jms.support.JmsAccessor.convertJmsAccessException(JmsAccessor.java:168)
>
> at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:469)
> at
> org.apache.camel.component.jms.JmsConfiguration$CamelJmsTemplate.send(JmsConfiguration.java:171)
>
> at org.apache.camel.component.jms.JmsProducer.doSend(JmsProducer.java:315)
> at
> org.apache.camel.component.jms.JmsProducer.processInOnly(JmsProducer.java:269)
>
> at org.apache.camel.component.jms.JmsProducer.process(JmsProducer.java:93)
> at
> org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:99)
>
> at
> org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:91)
>
> at
> org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:85)
>
> at
> org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:63)
>
> at
> org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:333)
> at
> org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:303)
> at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:208)
> at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:303)
> at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:154)
> at
> org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:110)
>
> at
> org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:123)
>
> ... 2 more
> Caused by: javax.jms.JMSException: can't create connection
> at
> util.TestCamelJmsExceptionHandling$1.createConnection(TestCamelJmsExceptionHandling.java:36)
>
> at
> org.apache.activemq.pool.PooledConnectionFactory.createConnection(PooledConnectionFactory.java:92)
>
> at
> org.springframework.jms.support.JmsAccessor.createConnection(JmsAccessor.java:184)
>
> at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:456)
> ... 17 more
>
> Thanks,
> Jim
>


-- 
Willem
----------------------------------
FuseSource
Web: http://www.fusesource.com
Blog:    http://willemjiang.blogspot.com (English)
          http://jnn.javaeye.com (Chinese)
Twitter: willemjiang

Connect at CamelOne May 24-26
The Open Source Integration Conference
http://camelone.com