You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2020/07/14 11:10:53 UTC

[camel-quarkus] branch master updated: Fix #1468 Intermittent failure of CamelDevModeTest

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 54bed57  Fix #1468 Intermittent failure of CamelDevModeTest
54bed57 is described below

commit 54bed57fb7a85f3b1f288f9ffa07e82e19da981b
Author: Peter Palaga <pp...@redhat.com>
AuthorDate: Tue Jul 14 10:28:24 2020 +0200

    Fix #1468 Intermittent failure of CamelDevModeTest
---
 .../main/deployment/CamelMainProcessor.java        | 26 +++++++++++++---------
 .../org/apache/camel/quarkus/main/CamelMain.java   | 10 ++++++++-
 .../apache/camel/quarkus/main/CamelMainConfig.java | 23 +++++++++++++++++++
 .../camel/quarkus/main/CamelMainRecorder.java      |  7 ++++--
 .../camel/quarkus/main/CamelMainRuntime.java       | 13 +++++++++--
 5 files changed, 64 insertions(+), 15 deletions(-)

diff --git a/extensions-core/main/deployment/src/main/java/org/apache/camel/quarkus/main/deployment/CamelMainProcessor.java b/extensions-core/main/deployment/src/main/java/org/apache/camel/quarkus/main/deployment/CamelMainProcessor.java
index 4f76da4..2743e60 100644
--- a/extensions-core/main/deployment/src/main/java/org/apache/camel/quarkus/main/deployment/CamelMainProcessor.java
+++ b/extensions-core/main/deployment/src/main/java/org/apache/camel/quarkus/main/deployment/CamelMainProcessor.java
@@ -42,6 +42,7 @@ import org.apache.camel.quarkus.core.deployment.spi.CamelRuntimeTaskBuildItem;
 import org.apache.camel.quarkus.core.deployment.spi.ContainerBeansBuildItem;
 import org.apache.camel.quarkus.core.deployment.spi.RuntimeCamelContextCustomizerBuildItem;
 import org.apache.camel.quarkus.main.CamelMain;
+import org.apache.camel.quarkus.main.CamelMainConfig;
 import org.apache.camel.quarkus.main.CamelMainProducers;
 import org.apache.camel.quarkus.main.CamelMainRecorder;
 import org.apache.camel.quarkus.main.deployment.spi.CamelMainBuildItem;
@@ -140,14 +141,15 @@ public class CamelMainProcessor {
      * after having processed a certain number of messages..
      * </ul>
      *
-     * @param  index         a reference to a {@link IndexView}
-     * @param  beanContainer a reference to a fully initialized CDI bean container
-     * @param  recorder      the recorder.
-     * @param  main          a reference to a {@link CamelMain}.
-     * @param  customizers   a list of {@link org.apache.camel.quarkus.core.CamelContextCustomizer} that will be
-     *                       executed before starting the {@link CamelContext} at {@link ExecutionTime#RUNTIME_INIT}.
-     * @param  runtimeTasks  a placeholder to ensure all the runtime task are properly are done.
-     * @return               a build item holding a {@link CamelRuntime} instance.
+     * @param  index           a reference to a {@link IndexView}
+     * @param  beanContainer   a reference to a fully initialized CDI bean container
+     * @param  recorder        the recorder.
+     * @param  main            a reference to a {@link CamelMain}.
+     * @param  customizers     a list of {@link org.apache.camel.quarkus.core.CamelContextCustomizer} that will be
+     *                         executed before starting the {@link CamelContext} at {@link ExecutionTime#RUNTIME_INIT}.
+     * @param  runtimeTasks    a placeholder to ensure all the runtime task are properly are done.
+     * @param  camelMainConfig a {@link CamelMainConfig}
+     * @return                 a build item holding a {@link CamelRuntime} instance.
      */
     @BuildStep
     @Record(value = ExecutionTime.RUNTIME_INIT, optional = true)
@@ -161,7 +163,8 @@ public class CamelMainProcessor {
             CamelMainRecorder recorder,
             CamelMainBuildItem main,
             List<RuntimeCamelContextCustomizerBuildItem> customizers,
-            List<CamelRuntimeTaskBuildItem> runtimeTasks) {
+            List<CamelRuntimeTaskBuildItem> runtimeTasks,
+            CamelMainConfig camelMainConfig) {
 
         // Run the customizer before starting the context to give a last chance
         // to amend the Camel Context setup.
@@ -174,7 +177,10 @@ public class CamelMainProcessor {
                 customizers.stream().map(RuntimeCamelContextCustomizerBuildItem::get).collect(Collectors.toList()));
 
         return new CamelRuntimeBuildItem(
-                recorder.createRuntime(beanContainer.getValue(), main.getInstance()),
+                recorder.createRuntime(
+                        beanContainer.getValue(),
+                        main.getInstance(),
+                        camelMainConfig.shutdown.timeout.toMillis()),
                 index.getIndex().getAnnotations(DotName.createSimple(QuarkusMain.class.getName())).isEmpty());
     }
 }
diff --git a/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMain.java b/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMain.java
index a4b70a6..7e71a52 100644
--- a/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMain.java
+++ b/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMain.java
@@ -34,8 +34,12 @@ import org.apache.camel.main.MainShutdownStrategy;
 import org.apache.camel.spi.CamelBeanPostProcessor;
 import org.apache.camel.spi.HasCamelContext;
 import org.apache.camel.support.service.ServiceHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class CamelMain extends MainCommandLineSupport implements HasCamelContext {
+    private static final Logger LOGGER = LoggerFactory.getLogger(CamelMain.class);
+
     private final AtomicBoolean engineStarted;
 
     public CamelMain(CamelContext camelContext) {
@@ -182,7 +186,11 @@ public final class CamelMain extends MainCommandLineSupport implements HasCamelC
 
         @Override
         public void await(long timeout, TimeUnit unit) throws InterruptedException {
-            latch.await(timeout, unit);
+            if (!latch.await(timeout, unit)) {
+                LOGGER.warn(
+                        "Could not await stopping CamelMain within {} {}. You may want to increase camel.main.shutdown.timeout",
+                        timeout, unit);
+            }
         }
     }
 }
diff --git a/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainConfig.java b/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainConfig.java
index a946755..ff74c28 100644
--- a/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainConfig.java
+++ b/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainConfig.java
@@ -16,5 +16,28 @@
  */
 package org.apache.camel.quarkus.main;
 
+import java.time.Duration;
+
+import io.quarkus.runtime.annotations.ConfigGroup;
+import io.quarkus.runtime.annotations.ConfigItem;
+import io.quarkus.runtime.annotations.ConfigPhase;
+import io.quarkus.runtime.annotations.ConfigRoot;
+
+@ConfigRoot(name = "camel.main", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
 public class CamelMainConfig {
+
+    /**
+     * Build time configuration options for {@link CamelMain} shutdown.
+     */
+    @ConfigItem
+    public ShutdownConfig shutdown;
+
+    @ConfigGroup
+    public static class ShutdownConfig {
+        /**
+         * A timeout (with millisecond precision) to wait for {@link CamelMain#stop()} to finish
+         */
+        @ConfigItem(defaultValue = "PT3S")
+        public Duration timeout;
+    }
 }
diff --git a/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainRecorder.java b/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainRecorder.java
index a2be10c..22baa7d 100644
--- a/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainRecorder.java
+++ b/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainRecorder.java
@@ -98,8 +98,11 @@ public class CamelMainRecorder {
         });
     }
 
-    public RuntimeValue<CamelRuntime> createRuntime(BeanContainer beanContainer, RuntimeValue<CamelMain> main) {
-        final CamelRuntime runtime = new CamelMainRuntime(main.getValue());
+    public RuntimeValue<CamelRuntime> createRuntime(
+            BeanContainer beanContainer,
+            RuntimeValue<CamelMain> main,
+            long shutdownTimeoutMs) {
+        final CamelRuntime runtime = new CamelMainRuntime(main.getValue(), shutdownTimeoutMs);
 
         // register to the container
         beanContainer.instance(CamelProducers.class).setRuntime(runtime);
diff --git a/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainRuntime.java b/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainRuntime.java
index ff0d324..d12c1f8 100644
--- a/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainRuntime.java
+++ b/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainRuntime.java
@@ -17,6 +17,7 @@
 package org.apache.camel.quarkus.main;
 
 import java.util.Arrays;
+import java.util.concurrent.TimeUnit;
 
 import io.quarkus.runtime.Quarkus;
 import org.apache.camel.CamelContext;
@@ -30,9 +31,11 @@ import org.slf4j.LoggerFactory;
 public class CamelMainRuntime implements CamelRuntime {
     private static final Logger LOGGER = LoggerFactory.getLogger(CamelMainRuntime.class);
     private final CamelMain main;
+    private final long shutdownTimeoutMs;
 
-    public CamelMainRuntime(CamelMain main) {
+    public CamelMainRuntime(CamelMain main, long shutdownTimeoutMs) {
         this.main = main;
+        this.shutdownTimeoutMs = shutdownTimeoutMs;
     }
 
     @Override
@@ -53,7 +56,7 @@ public class CamelMainRuntime implements CamelRuntime {
                     stop();
                     throw new RuntimeException(e);
                 }
-            }).start();
+            }, "camel-main").start();
         } catch (Exception e) {
             LOGGER.error("Failed to start application", e);
             stop();
@@ -64,6 +67,12 @@ public class CamelMainRuntime implements CamelRuntime {
     @Override
     public void stop() {
         main.stop();
+        /* Wait till the Camel shutdown is finished in camel-main thread started in start(String[]) above */
+        try {
+            main.getShutdownStrategy().await(shutdownTimeoutMs, TimeUnit.MILLISECONDS);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+        }
     }
 
     @Override