You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/08/01 07:48:58 UTC

[camel] 02/02: CAMEL-13094: Ensure service is stopped if it fails to start as it did in Camel 2.x. This can also lead to JMX MBeans not being unregistered from the JVM. Thanks to Thomas Diesler for the unit test.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 58fb94fc865db636b5cda224d87f537807787dc5
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Aug 1 09:27:34 2019 +0200

    CAMEL-13094: Ensure service is stopped if it fails to start as it did in Camel 2.x. This can also lead to JMX MBeans not being unregistered from the JVM. Thanks to Thomas Diesler for the unit test.
---
 .../camel/support/service/ServiceSupport.java      | 11 +++
 .../RouteStartupFailShouldStopAlsoIssueTest.java   | 11 +--
 .../camel/management/ManagedStartupFailedTest.java | 82 ++++++++++++++++++++++
 3 files changed, 96 insertions(+), 8 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/support/service/ServiceSupport.java b/core/camel-api/src/main/java/org/apache/camel/support/service/ServiceSupport.java
index 1fbbe85..c62a998 100644
--- a/core/camel-api/src/main/java/org/apache/camel/support/service/ServiceSupport.java
+++ b/core/camel-api/src/main/java/org/apache/camel/support/service/ServiceSupport.java
@@ -98,6 +98,13 @@ public abstract class ServiceSupport implements StatefulService {
                 status = STARTED;
                 log.trace("Service started: {}", this);
             } catch (Exception e) {
+                // need to stop as some resources may have been started during startup
+                try {
+                    stop();
+                } catch (Exception e2) {
+                    // ignore
+                    log.trace("Error while stopping service after it failed to start: " + this + ". This exception is ignored", e);
+                }
                 status = FAILED;
                 log.trace("Error while starting service: " + this, e);
                 throw RuntimeCamelException.wrapRuntimeException(e);
@@ -113,6 +120,10 @@ public abstract class ServiceSupport implements StatefulService {
      */
     public void stop() {
         synchronized (lock) {
+            if (status == FAILED) {
+                log.trace("Service: {} failed and regarded as already stopped", this);
+                return;
+            }
             if (status == STOPPED || status == SHUTTINGDOWN || status == SHUTDOWN) {
                 log.trace("Service: {} already stopped", this);
                 return;
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/RouteStartupFailShouldStopAlsoIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/RouteStartupFailShouldStopAlsoIssueTest.java
index 219c1e3..b7f9490 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/RouteStartupFailShouldStopAlsoIssueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/RouteStartupFailShouldStopAlsoIssueTest.java
@@ -67,17 +67,12 @@ public class RouteStartupFailShouldStopAlsoIssueTest extends ContextTestSupport
         assertTrue(context.getRouteController().getRouteStatus("foo").isStopped());
         assertFalse(context.getRouteController().getRouteStatus("foo").isStarted());
 
-        assertFalse(context.getRouteController().getRouteStatus("bar").isStopped());
-        assertTrue(context.getRouteController().getRouteStatus("bar").isStarted());
-
-        context.stop();
-
-        assertTrue(context.getRouteController().getRouteStatus("foo").isStopped());
-        assertFalse(context.getRouteController().getRouteStatus("foo").isStarted());
-
         assertTrue(context.getRouteController().getRouteStatus("bar").isStopped());
         assertFalse(context.getRouteController().getRouteStatus("bar").isStarted());
 
+        assertFalse(context.getStatus().isStarted());
+        assertTrue(context.getStatus().isStopped());
+
         assertEquals(3, EVENTS.size());
         assertEquals("constructor", EVENTS.get(0));
         assertEquals("doStart", EVENTS.get(1));
diff --git a/core/camel-management-impl/src/test/java/org/apache/camel/management/ManagedStartupFailedTest.java b/core/camel-management-impl/src/test/java/org/apache/camel/management/ManagedStartupFailedTest.java
new file mode 100644
index 0000000..54c1c37
--- /dev/null
+++ b/core/camel-management-impl/src/test/java/org/apache/camel/management/ManagedStartupFailedTest.java
@@ -0,0 +1,82 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.management;
+
+import java.util.Collections;
+import java.util.Set;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class ManagedStartupFailedTest extends ManagementTestSupport {
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Test
+    public void testAllGood() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").transform(body().prepend("Hello "));
+            }
+        });
+
+        context.start();
+
+        MBeanServer server = getMBeanServer();
+        try {
+            Set<ObjectName> onames = server.queryNames(new ObjectName("org.apache.camel:*"), null);
+            assertTrue(onames.size() > 0);
+
+            ProducerTemplate producer = context.createProducerTemplate();
+            String result = producer.requestBody("direct:start", "Kermit", String.class);
+            assertEquals("Hello Kermit", result);
+        } finally {
+            context.stop();
+        }
+
+        Set<ObjectName> onames = server.queryNames(new ObjectName("org.apache.camel:*"), null);
+        assertEquals(Collections.emptySet(), onames);
+    }
+
+    @Test
+    public void testStartupFailure() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("invalid:start");
+            }
+        });
+
+        try {
+            context.start();
+            fail("Startup failure expected");
+        } catch (Exception ex) {
+            // expected
+        }
+
+        MBeanServer server = getMBeanServer();
+        Set<ObjectName> onames = server.queryNames(new ObjectName("org.apache.camel:*"), null);
+        assertEquals(Collections.emptySet(), onames);
+    }
+}