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 2021/03/16 08:27:50 UTC

[camel] 01/02: CAMEL-116340: Ensure components are started when CamelContext is starting.

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 625a5a2d7665490f514ba5b9dca85dc55f6990eb
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Mar 16 09:19:49 2021 +0100

    CAMEL-116340: Ensure components are started when CamelContext is starting.
---
 .../camel/FailedToStartComponentException.java     | 35 ++++++++++++++++++++++
 .../camel/impl/engine/AbstractCamelContext.java    | 13 ++++++++
 2 files changed, 48 insertions(+)

diff --git a/core/camel-api/src/main/java/org/apache/camel/FailedToStartComponentException.java b/core/camel-api/src/main/java/org/apache/camel/FailedToStartComponentException.java
new file mode 100644
index 0000000..a8aad24
--- /dev/null
+++ b/core/camel-api/src/main/java/org/apache/camel/FailedToStartComponentException.java
@@ -0,0 +1,35 @@
+/*
+ * 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;
+
+/**
+ * Exception when failing to start a {@link Component}.
+ */
+public class FailedToStartComponentException extends RuntimeCamelException {
+
+    private final String componentName;
+
+    public FailedToStartComponentException(String componentName, String message, Throwable cause) {
+        super("Failed to start component " + componentName + " because of " + message, cause);
+        this.componentName = componentName;
+    }
+
+    public String getComponentName() {
+        return componentName;
+    }
+
+}
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 6f4ac82..c638bd5 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -53,6 +53,7 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.ErrorHandlerFactory;
 import org.apache.camel.ExchangeConstantProvider;
 import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.FailedToStartComponentException;
 import org.apache.camel.FluentProducerTemplate;
 import org.apache.camel.GlobalEndpointConfiguration;
 import org.apache.camel.IsSingleton;
@@ -2829,6 +2830,18 @@ public abstract class AbstractCamelContext extends BaseService
         startDate = System.currentTimeMillis();
         stopWatch.restart();
 
+        // ensure components are started
+        for (Map.Entry<String, Component> entry : components.entrySet()) {
+            StartupStep step = startupStepRecorder.beginStep(Component.class, entry.getKey(), "Start Component");
+            try {
+                ServiceHelper.startService(entry.getValue());
+            } catch (Exception e) {
+                throw new FailedToStartComponentException(entry.getKey(), e.getMessage(), e);
+            } finally {
+                startupStepRecorder.endStep(step);
+            }
+        }
+
         // Start the route controller
         ServiceHelper.startService(this.routeController);