Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0A18019970 for ; Thu, 28 Apr 2016 10:37:48 +0000 (UTC) Received: (qmail 56916 invoked by uid 500); 28 Apr 2016 10:37:47 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 56869 invoked by uid 500); 28 Apr 2016 10:37:47 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 56860 invoked by uid 99); 28 Apr 2016 10:37:47 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Apr 2016 10:37:47 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 57E1FDFC6F; Thu, 28 Apr 2016 10:37:47 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Message-Id: <2018cbeb9aff429e8f52a553a4c4d947@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: camel git commit: CAMEL-9916: Added test Date: Thu, 28 Apr 2016 10:37:47 +0000 (UTC) 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 Authored: Thu Apr 28 12:37:23 2016 +0200 Committer: Claus Ibsen 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(); + } +}