Author: ningjiang
Date: Tue Apr 19 03:23:23 2011
New Revision: 1094847
URL: http://svn.apache.org/viewvc?rev=1094847&view=rev
Log:
CAMEL-3714 testConnectionOnStartup supports to test connection for jms producers
Modified:
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTestConnectionOnStartupTest.java
Modified:
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java?rev=1094847&r1=1094846&r2=1094847&view=diff
==============================================================================
---
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
(original)
+++
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
Tue Apr 19 03:23:23 2011
@@ -18,6 +18,8 @@ package org.apache.camel.component.jms;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
+
+import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
@@ -38,9 +40,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsOperations;
import org.springframework.jms.core.MessageCreator;
+import org.springframework.jms.support.JmsUtils;
import static
org.apache.camel.component.jms.JmsMessageHelper.normalizeDestinationName;
-
/**
* @version
*/
@@ -402,12 +404,38 @@ public class JmsProducer extends Default
this.uuidGenerator = uuidGenerator;
}
+ /**
+ * Pre tests the connection before starting the listening.
+ * <p/>
+ * In case of connection failure the exception is thrown which prevents
Camel from starting.
+ *
+ * @throws FailedToCreateProducerException is thrown if testing the
connection failed
+ */
+ protected void testConnectionOnStartup() throws
FailedToCreateProducerException {
+ try {
+ CamelJmsTemplate template = (CamelJmsTemplate) getInOnlyTemplate();
+
+ if (log.isDebugEnabled()) {
+ log.debug("Testing JMS Connection on startup for destination:
" + template.getDefaultDestinationName());
+ }
+
+ Connection conn =
template.getConnectionFactory().createConnection();
+ JmsUtils.closeConnection(conn);
+
+ log.info("Successfully tested JMS Connection on startup for
destination: " + template.getDefaultDestinationName());
+ } catch (Exception e) {
+ throw new FailedToCreateProducerException(getEndpoint(), e);
+ }
+ }
protected void doStart() throws Exception {
super.doStart();
if (uuidGenerator == null) {
// use the generator configured on the camel context
uuidGenerator = getEndpoint().getCamelContext().getUuidGenerator();
}
+ if (endpoint.isTestConnectionOnStartup()) {
+ testConnectionOnStartup();
+ }
}
protected void doStop() throws Exception {
Modified:
camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTestConnectionOnStartupTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTestConnectionOnStartupTest.java?rev=1094847&r1=1094846&r2=1094847&view=diff
==============================================================================
---
camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTestConnectionOnStartupTest.java
(original)
+++
camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTestConnectionOnStartupTest.java
Tue Apr 19 03:23:23 2011
@@ -21,6 +21,7 @@ import javax.jms.ConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.FailedToCreateConsumerException;
+import org.apache.camel.FailedToCreateProducerException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
@@ -33,7 +34,7 @@ import static org.apache.camel.component
public class JmsTestConnectionOnStartupTest extends CamelTestSupport {
@Test
- public void testConnectionOnStartupTest() throws Exception {
+ public void testConnectionOnStartupConsumerTest() throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
@@ -51,6 +52,28 @@ public class JmsTestConnectionOnStartupT
}
}
+ @Test
+ public void testConnectionOnStartupProducerTest() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+
from("direct:start").to("activemq:queue:foo?testConnectionOnStartup=true");
+ }
+ });
+
+ try {
+ context.start();
+ fail("Should have thrown an exception");
+ } catch (FailedToCreateProducerException e) {
+ // expected
+ assertEquals("Failed to create Producer for endpoint:
Endpoint[activemq://queue:foo?testConnectionOnStartup=true]. "
+ + "Reason: org.apache.camel.FailedToCreateProducerException:
Failed to create Producer for endpoint: "
+ +
"Endpoint[activemq://queue:foo?testConnectionOnStartup=true]. Reason:
javax.jms.JMSException: "
+ + "Could not connect to broker URL: tcp://localhost:61111.
Reason: java.net.ConnectException: Connection refused",
+ e.getMessage());
+ }
+ }
+
protected CamelContext createCamelContext() throws Exception {
CamelContext camelContext = super.createCamelContext();