camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-9916: Added test
Date Thu, 28 Apr 2016 10:37:47 GMT
Repository: camel
Updated Branches:
  refs/heads/master d527303c8 -> d78e0908d


CAMEL-9916: Added test


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d78e0908
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d78e0908
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d78e0908

Branch: refs/heads/master
Commit: d78e0908db4418bd36b482cddde71256a4977a43
Parents: d527303
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Apr 28 12:37:23 2016 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Apr 28 12:37:39 2016 +0200

----------------------------------------------------------------------
 .../camel/component/sjms/SjmsComponent.java     |  14 ++-
 .../sjms/SjmsComponentRestartTest.java          | 112 +++++++++++++++++++
 2 files changed, 123 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d78e0908/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsComponent.java
b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsComponent.java
index 2748aae..e1132bf 100644
--- a/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsComponent.java
+++ b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsComponent.java
@@ -44,6 +44,7 @@ public class SjmsComponent extends UriEndpointComponent implements HeaderFilterS
 
     private ConnectionFactory connectionFactory;
     private ConnectionResource connectionResource;
+    private volatile boolean closeConnectionResource;
     private HeaderFilterStrategy headerFilterStrategy = new SjmsHeaderFilterStrategy();
     private JmsKeyFormatStrategy jmsKeyFormatStrategy = new DefaultJmsKeyFormatStrategy();
     private Integer connectionCount = 1;
@@ -115,6 +116,8 @@ public class SjmsComponent extends UriEndpointComponent implements HeaderFilterS
             ConnectionFactoryResource connections = new ConnectionFactoryResource(getConnectionCount(),
getConnectionFactory());
             connections.fillPool();
             setConnectionResource(connections);
+            // we created the resource so we should close it when stopping
+            closeConnectionResource = true;
         } else if (getConnectionResource() instanceof ConnectionFactoryResource) {
             ((ConnectionFactoryResource) getConnectionResource()).fillPool();
         }
@@ -124,13 +127,18 @@ public class SjmsComponent extends UriEndpointComponent implements HeaderFilterS
     protected void doStop() throws Exception {
         if (timedTaskManager != null) {
             timedTaskManager.cancelTasks();
+            timedTaskManager = null;
         }
 
-        if (getConnectionResource() != null) {
-            if (getConnectionResource() instanceof ConnectionFactoryResource) {
-                ((ConnectionFactoryResource) getConnectionResource()).drainPool();
+        if (closeConnectionResource) {
+            if (getConnectionResource() != null) {
+                if (getConnectionResource() instanceof ConnectionFactoryResource) {
+                    ((ConnectionFactoryResource) getConnectionResource()).drainPool();
+                }
             }
+            connectionResource = null;
         }
+
         super.doStop();
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/d78e0908/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/SjmsComponentRestartTest.java
----------------------------------------------------------------------
diff --git a/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/SjmsComponentRestartTest.java
b/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/SjmsComponentRestartTest.java
new file mode 100644
index 0000000..494b5de
--- /dev/null
+++ b/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/SjmsComponentRestartTest.java
@@ -0,0 +1,112 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.sjms;
+
+import javax.jms.ConnectionFactory;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class SjmsComponentRestartTest extends CamelTestSupport {
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://broker?broker.persistent=false&broker.useJmx=false");
+
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("activemqCF", connectionFactory);
+        return jndi;
+    }
+
+    @Test
+    public void testRestartWithStopStart() throws Exception {
+        SjmsComponent sjmsComponent = new SjmsComponent();
+        sjmsComponent.setConnectionFactory((ConnectionFactory) context.getRegistry().lookupByName("activemqCF"));
+        context.addComponent("sjms", sjmsComponent);
+
+        RouteBuilder routeBuilder = new RouteBuilder(context) {
+            @Override
+            public void configure() throws Exception {
+                from("sjms:queue:test").to("mock:test");
+            }
+        };
+        context.addRoutes(routeBuilder);
+
+        context.start();
+
+        getMockEndpoint("mock:test").expectedMessageCount(1);
+        template.sendBody("sjms:queue:test", "Hello World");
+        assertMockEndpointsSatisfied();
+
+        // restart
+        context.stop();
+
+        // must add our custom component back again
+        context.addComponent("sjms", sjmsComponent);
+
+        context.start();
+
+        getMockEndpoint("mock:test").expectedMessageCount(1);
+
+        // and re-create template
+        template = context.createProducerTemplate();
+        template.sendBody("sjms:queue:test", "Hello World");
+        assertMockEndpointsSatisfied();
+
+        context.stop();
+    }
+
+    @Test
+    public void testRestartWithSuspendResume() throws Exception {
+        SjmsComponent sjmsComponent = new SjmsComponent();
+        sjmsComponent.setConnectionFactory((ConnectionFactory) context.getRegistry().lookupByName("activemqCF"));
+        context.addComponent("sjms", sjmsComponent);
+
+        RouteBuilder routeBuilder = new RouteBuilder(context) {
+            @Override
+            public void configure() throws Exception {
+                from("sjms:queue:test").to("mock:test");
+            }
+        };
+        context.addRoutes(routeBuilder);
+
+        context.start();
+
+        getMockEndpoint("mock:test").expectedMessageCount(1);
+        template.sendBody("sjms:queue:test", "Hello World");
+        assertMockEndpointsSatisfied();
+
+        // restart
+        context.suspend();
+        context.resume();
+
+        getMockEndpoint("mock:test").expectedMessageCount(1);
+
+        template.sendBody("sjms:queue:test", "Hello World");
+        assertMockEndpointsSatisfied();
+
+        context.stop();
+    }
+}


Mime
View raw message