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/06/06 10:44:06 UTC

[camel] branch master updated (d4eec08 -> 6f7b143)

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

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


    from d4eec08  Upgrade Xchange to version 4.3.19
     new f08a4e6  CAMEL-12868: Detect if camel on spring-boot shutdown very quickly because there is no main controller or starter-web dependency to keep the JVM alive.
     new 6f7b143  CAMEL-12868: Detect if camel on spring-boot shutdown very quickly because there is no main controller or starter-web dependency to keep the JVM alive.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../src/main/docs/spring-boot.adoc                 |  3 +-
 .../camel/spring/boot/CamelAutoConfiguration.java  | 32 +++++-------
 .../spring/boot/CamelConfigurationProperties.java  | 14 +++++
 .../camel/spring/boot/SpringBootCamelContext.java  | 59 ++++++++++++++++++++++
 .../org/apache/camel/ExtendedCamelContext.java     | 21 ++++++++
 .../camel/impl/engine/AbstractCamelContext.java    |  8 +--
 6 files changed, 111 insertions(+), 26 deletions(-)
 create mode 100644 components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootCamelContext.java


[camel] 01/02: CAMEL-12868: Detect if camel on spring-boot shutdown very quickly because there is no main controller or starter-web dependency to keep the JVM alive.

Posted by da...@apache.org.
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 f08a4e6d5b08f7edce3ccf28335787acfd63907c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jun 6 11:52:01 2019 +0200

    CAMEL-12868: Detect if camel on spring-boot shutdown very quickly because there is no main controller or starter-web dependency to keep the JVM alive.
---
 .../camel/spring/boot/CamelAutoConfiguration.java  | 32 ++++++------
 .../camel/spring/boot/SpringBootCamelContext.java  | 57 ++++++++++++++++++++++
 .../org/apache/camel/ExtendedCamelContext.java     | 21 ++++++++
 .../camel/impl/engine/AbstractCamelContext.java    |  8 +--
 4 files changed, 93 insertions(+), 25 deletions(-)

diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
index 9ee72ae..070bc68 100644
--- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
@@ -39,7 +39,6 @@ import org.apache.camel.component.properties.PropertiesParser;
 import org.apache.camel.health.HealthCheckRegistry;
 import org.apache.camel.health.HealthCheckRepository;
 import org.apache.camel.health.HealthCheckService;
-import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.FileWatcherReloadStrategy;
 import org.apache.camel.model.Model;
 import org.apache.camel.processor.interceptor.BacklogTracer;
@@ -66,7 +65,6 @@ import org.apache.camel.spi.ThreadPoolProfile;
 import org.apache.camel.spi.UnitOfWorkFactory;
 import org.apache.camel.spi.UuidGenerator;
 import org.apache.camel.spring.CamelBeanPostProcessor;
-import org.apache.camel.spring.SpringCamelContext;
 import org.apache.camel.spring.spi.ApplicationContextBeanRepository;
 import org.apache.camel.spring.spi.XmlCamelContextConfigurer;
 import org.apache.camel.support.DefaultRegistry;
@@ -115,7 +113,7 @@ public class CamelAutoConfiguration {
     @ConditionalOnMissingBean(CamelContext.class)
     CamelContext camelContext(ApplicationContext applicationContext,
                               CamelConfigurationProperties config) throws Exception {
-        CamelContext camelContext = new SpringCamelContext(applicationContext);
+        CamelContext camelContext = new SpringBootCamelContext(applicationContext);
         return doConfigureCamelContext(applicationContext, camelContext, config);
     }
 
@@ -125,20 +123,18 @@ public class CamelAutoConfiguration {
 
         camelContext.init();
 
-        if (camelContext instanceof DefaultCamelContext) {
-            final DefaultCamelContext defaultContext = (DefaultCamelContext) camelContext;
-            final Map<String, BeanRepository> repositories = applicationContext.getBeansOfType(BeanRepository.class);
-
-            if (!repositories.isEmpty()) {
-                List<BeanRepository> reps = new ArrayList<>();
-                // include default bean repository as well
-                reps.add(new ApplicationContextBeanRepository(applicationContext));
-                // and then any custom
-                reps.addAll(repositories.values());
-                // sort by ordered
-                OrderComparator.sort(reps);
-                defaultContext.setRegistry(new DefaultRegistry(reps));
-            }
+        final Map<String, BeanRepository> repositories = applicationContext.getBeansOfType(BeanRepository.class);
+
+        if (!repositories.isEmpty()) {
+            List<BeanRepository> reps = new ArrayList<>();
+            // include default bean repository as well
+            reps.add(new ApplicationContextBeanRepository(applicationContext));
+            // and then any custom
+            reps.addAll(repositories.values());
+            // sort by ordered
+            OrderComparator.sort(reps);
+            // and plugin as new registry
+            camelContext.adapt(ExtendedCamelContext.class).setRegistry(new DefaultRegistry(reps));
         }
 
         if (ObjectHelper.isNotEmpty(config.getFileConfigurations())) {
@@ -158,7 +154,7 @@ public class CamelAutoConfiguration {
         }
 
         if (config.getName() != null) {
-            ((SpringCamelContext) camelContext).setName(config.getName());
+            camelContext.adapt(ExtendedCamelContext.class).setName(config.getName());
         }
 
         if (config.getShutdownTimeout() > 0) {
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootCamelContext.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootCamelContext.java
new file mode 100644
index 0000000..bbcd395
--- /dev/null
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootCamelContext.java
@@ -0,0 +1,57 @@
+/*
+ * 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.spring.boot;
+
+import org.apache.camel.spring.SpringCamelContext;
+import org.apache.camel.util.StopWatch;
+import org.springframework.context.ApplicationContext;
+
+/**
+ * The {@link org.apache.camel.CamelContext} created by Spring Boot.
+ */
+public class SpringBootCamelContext extends SpringCamelContext {
+
+    private final StopWatch stopWatch = new StopWatch();
+
+    public SpringBootCamelContext(ApplicationContext applicationContext) {
+        super(applicationContext);
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        stopWatch.restart();
+        super.doStart();
+    }
+
+    @Override
+    protected synchronized void doStop() throws Exception {
+        // if we are stopping very quickly then its likely because the user may not have either spring-boot-web
+        // or enabled Camel's main controller, so lets log a WARN about this.
+        long taken = stopWatch.taken();
+        if (taken < 1200) { // give it a bit of slack
+            String cp = System.getProperty("java.class.path");
+            boolean starterWeb = cp != null && cp.contains("spring-boot-starter-web");
+            boolean junit = cp != null && cp.contains("junit-");
+            if (!junit && !starterWeb) {
+                log.warn("CamelContext has only been running for less than a second. If you intend to run Camel for a longer time "
+                        + "then you can set the property camel.springboot.main-run-controller=true in application.properties"
+                        + " or add spring-boot-starter-web JAR to the classpath.");
+            }
+        }
+        super.doStop();
+    }
+}
diff --git a/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java b/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java
index 1304268..450b98a 100644
--- a/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java
+++ b/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java
@@ -41,6 +41,7 @@ import org.apache.camel.spi.ModelJAXBContextFactory;
 import org.apache.camel.spi.NodeIdFactory;
 import org.apache.camel.spi.PackageScanClassResolver;
 import org.apache.camel.spi.ProcessorFactory;
+import org.apache.camel.spi.Registry;
 import org.apache.camel.spi.RouteStartupOrder;
 import org.apache.camel.spi.UnitOfWorkFactory;
 
@@ -51,6 +52,26 @@ import org.apache.camel.spi.UnitOfWorkFactory;
 public interface ExtendedCamelContext extends CamelContext {
 
     /**
+     * Sets the name (id) of the this context.
+     * <p/>
+     * This operation is mostly only used by different Camel runtimes such as camel-spring, camel-cdi, camel-spring-boot etc.
+     * Important: Setting the name should only be set before CamelContext is started.
+     *
+     * @param name the name
+     */
+    void setName(String name);
+
+    /**
+     * Sets the registry Camel should use for looking up beans by name or type.
+     * <p/>
+     * This operation is mostly only used by different Camel runtimes such as camel-spring, camel-cdi, camel-spring-boot etc.
+     * Important: Setting the registry should only be set before CamelContext is started.
+     *
+     * @param registry the registry such as DefaultRegistry or
+     */
+    void setRegistry(Registry registry);
+
+    /**
      * Method to signal to {@link CamelContext} that the process to initialize setup routes is in progress.
      *
      * @param done <tt>false</tt> to start the process, call again with <tt>true</tt> to signal its done.
diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index dd71ffd..adc276d 100644
--- a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -376,14 +376,8 @@ public abstract class AbstractCamelContext extends ServiceSupport implements Ext
         return getNameStrategy().getName();
     }
 
-    /**
-     * Sets the name of the this context.
-     *
-     * @param name the name
-     */
     public void setName(String name) {
-        // use an explicit name strategy since an explicit name was provided to
-        // be used
+        // use an explicit name strategy since an explicit name was provided to be used
         setNameStrategy(new ExplicitCamelContextNameStrategy(name));
     }
 


[camel] 02/02: CAMEL-12868: Detect if camel on spring-boot shutdown very quickly because there is no main controller or starter-web dependency to keep the JVM alive.

Posted by da...@apache.org.
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 6f7b1439a2c84aba3ca179b3d17e6bf14a976c58
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jun 6 12:43:47 2019 +0200

    CAMEL-12868: Detect if camel on spring-boot shutdown very quickly because there is no main controller or starter-web dependency to keep the JVM alive.
---
 .../camel-spring-boot/src/main/docs/spring-boot.adoc       |  3 ++-
 .../apache/camel/spring/boot/CamelAutoConfiguration.java   |  2 +-
 .../camel/spring/boot/CamelConfigurationProperties.java    | 14 ++++++++++++++
 .../apache/camel/spring/boot/SpringBootCamelContext.java   |  8 +++++---
 4 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/components/camel-spring-boot/src/main/docs/spring-boot.adoc b/components/camel-spring-boot/src/main/docs/spring-boot.adoc
index 8218937..1f4f2ce 100644
--- a/components/camel-spring-boot/src/main/docs/spring-boot.adoc
+++ b/components/camel-spring-boot/src/main/docs/spring-boot.adoc
@@ -91,7 +91,7 @@ When using Spring Boot make sure to use the following Maven dependency to have s
 ----
 
 
-The component supports 140 options, which are listed below.
+The component supports 141 options, which are listed below.
 
 
 
@@ -206,6 +206,7 @@ The component supports 140 options, which are listed below.
 | *camel.springboot.use-breadcrumb* | Set whether breadcrumb is enabled. The default value is false. | false | Boolean
 | *camel.springboot.use-data-type* | Whether to enable using data type on Camel messages. Data type are automatic turned on if one ore more routes has been explicit configured with input and output types. Otherwise data type is default off. | false | Boolean
 | *camel.springboot.use-mdc-logging* | To turn on MDC logging | false | Boolean
+| *camel.springboot.warn-on-early-shutdown* | Whether to log a WARN if Camel on Spring Boot was immediately shutdown after starting which very likely is because there is no JVM thread to keep the application running. | true | Boolean
 | *camel.springboot.xml-rests* | Directory to scan for adding additional XML rests. You can turn this off by setting the value to false. Files can be loaded from either classpath or file by prefixing with classpath: or file: Wildcards is supported using a ANT pattern style paths, such as classpath:&#42;&#42;/&#42;camel&#42;.xml Multiple directories can be specified and separated by comma, such as: file:/myapp/mycamel/&#42;.xml,file:/myapp/myothercamel/&#42;.xml | classpath:camel-rest/*.x [...]
 | *camel.springboot.xml-routes* | Directory to scan for adding additional XML routes. You can turn this off by setting the value to false. Files can be loaded from either classpath or file by prefixing with classpath: or file: Wildcards is supported using a ANT pattern style paths, such as classpath:&#42;&#42;/&#42;camel&#42;.xml Multiple directories can be specified and separated by comma, such as: file:/myapp/mycamel/&#42;.xml,file:/myapp/myothercamel/&#42;.xml | classpath:camel/*.xml  [...]
 | *camel.springboot.xml-routes-reload-directory* | To watch the directory for file changes which triggers a live reload of the Camel routes on-the-fly. For example configure this to point to the source code where the Camel XML files are located such as: src/main/resources/camel/ |  | String
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
index 070bc68..a3c4c85 100644
--- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
@@ -113,7 +113,7 @@ public class CamelAutoConfiguration {
     @ConditionalOnMissingBean(CamelContext.class)
     CamelContext camelContext(ApplicationContext applicationContext,
                               CamelConfigurationProperties config) throws Exception {
-        CamelContext camelContext = new SpringBootCamelContext(applicationContext);
+        CamelContext camelContext = new SpringBootCamelContext(applicationContext, config.isWarnOnEarlyShutdown());
         return doConfigureCamelContext(applicationContext, camelContext, config);
     }
 
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelConfigurationProperties.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelConfigurationProperties.java
index 1e87c32f..d50b189 100644
--- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelConfigurationProperties.java
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelConfigurationProperties.java
@@ -35,6 +35,12 @@ public class CamelConfigurationProperties {
     private int shutdownTimeout = 300;
 
     /**
+     * Whether to log a WARN if Camel on Spring Boot was immediately shutdown after starting which
+     * very likely is because there is no JVM thread to keep the application running.
+     */
+    private boolean warnOnEarlyShutdown = true;
+
+    /**
      * Whether Camel should try to suppress logging during shutdown and timeout was triggered,
      * meaning forced shutdown is happening. And during forced shutdown we want to avoid logging
      * errors/warnings et all in the logs as a side-effect of the forced timeout.
@@ -508,6 +514,14 @@ public class CamelConfigurationProperties {
         this.shutdownTimeout = shutdownTimeout;
     }
 
+    public boolean isWarnOnEarlyShutdown() {
+        return warnOnEarlyShutdown;
+    }
+
+    public void setWarnOnEarlyShutdown(boolean warnOnEarlyShutdown) {
+        this.warnOnEarlyShutdown = warnOnEarlyShutdown;
+    }
+
     public boolean isShutdownSuppressLoggingOnTimeout() {
         return shutdownSuppressLoggingOnTimeout;
     }
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootCamelContext.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootCamelContext.java
index bbcd395..1b32495 100644
--- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootCamelContext.java
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootCamelContext.java
@@ -26,9 +26,11 @@ import org.springframework.context.ApplicationContext;
 public class SpringBootCamelContext extends SpringCamelContext {
 
     private final StopWatch stopWatch = new StopWatch();
+    private final boolean warnOnEarlyShutdown;
 
-    public SpringBootCamelContext(ApplicationContext applicationContext) {
+    public SpringBootCamelContext(ApplicationContext applicationContext, boolean warnOnEarlyShutdown) {
         super(applicationContext);
+        this.warnOnEarlyShutdown = warnOnEarlyShutdown;
     }
 
     @Override
@@ -42,10 +44,10 @@ public class SpringBootCamelContext extends SpringCamelContext {
         // if we are stopping very quickly then its likely because the user may not have either spring-boot-web
         // or enabled Camel's main controller, so lets log a WARN about this.
         long taken = stopWatch.taken();
-        if (taken < 1200) { // give it a bit of slack
+        if (warnOnEarlyShutdown && taken < 1200) { // give it a bit of slack
             String cp = System.getProperty("java.class.path");
-            boolean starterWeb = cp != null && cp.contains("spring-boot-starter-web");
             boolean junit = cp != null && cp.contains("junit-");
+            boolean starterWeb = cp != null && cp.contains("spring-boot-starter-web");
             if (!junit && !starterWeb) {
                 log.warn("CamelContext has only been running for less than a second. If you intend to run Camel for a longer time "
                         + "then you can set the property camel.springboot.main-run-controller=true in application.properties"