You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pp...@apache.org on 2020/07/02 08:19:44 UTC

[camel-quarkus] branch master updated: Split main startup logic

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

ppalaga 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 794de32  Split main startup logic
794de32 is described below

commit 794de3297abbc0e9669ef56646d7ef087715c43d
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Thu Jul 2 08:21:36 2020 +0200

    Split main startup logic
---
 .../deployment/CamelMainAutoConfigurationTest.java |  5 ---
 .../main/deployment/CamelMainInjectTest.java       |  5 ---
 .../deployment/CamelMainRoutesDiscoveryTest.java   |  5 ---
 .../main/deployment/CamelMainRoutesFilterTest.java |  5 ---
 .../org/apache/camel/quarkus/main/CamelMain.java   | 46 ++++++++++++++++++++++
 .../camel/quarkus/main/CamelMainRuntime.java       | 27 ++++++++-----
 6 files changed, 64 insertions(+), 29 deletions(-)

diff --git a/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainAutoConfigurationTest.java b/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainAutoConfigurationTest.java
index 54c9e0f..9d45d7a 100644
--- a/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainAutoConfigurationTest.java
+++ b/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainAutoConfigurationTest.java
@@ -20,7 +20,6 @@ import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.util.Properties;
-import java.util.concurrent.TimeUnit;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.inject.Produces;
@@ -40,7 +39,6 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.awaitility.Awaitility.await;
 
 public class CamelMainAutoConfigurationTest {
     @RegisterExtension
@@ -69,9 +67,6 @@ public class CamelMainAutoConfigurationTest {
 
     @Test
     public void testComponentAutoConfiguration() {
-        // ensure the camel context is started before doing any assertion
-        await().atMost(10, TimeUnit.SECONDS).until(main::isStarted);
-
         // ensure that the exchange formatter explicit set to the LogComponent
         // is not overridden by any ExchangeFormatter instance available from
         // the container
diff --git a/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainInjectTest.java b/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainInjectTest.java
index 6788d59..14de57f 100644
--- a/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainInjectTest.java
+++ b/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainInjectTest.java
@@ -20,7 +20,6 @@ import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.util.Properties;
-import java.util.concurrent.TimeUnit;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
@@ -39,7 +38,6 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.awaitility.Awaitility.await;
 
 public class CamelMainInjectTest {
     @RegisterExtension
@@ -68,9 +66,6 @@ public class CamelMainInjectTest {
 
     @Test
     public void testInject() {
-        // ensure the camel context is started before doing any assertion
-        await().atMost(10, TimeUnit.SECONDS).until(main::isStarted);
-
         // ensure Camel's DI works
         assertThat(main.configure().getRoutesBuilders())
                 .first()
diff --git a/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainRoutesDiscoveryTest.java b/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainRoutesDiscoveryTest.java
index dbeb01b..e58cbad 100644
--- a/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainRoutesDiscoveryTest.java
+++ b/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainRoutesDiscoveryTest.java
@@ -20,7 +20,6 @@ import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.util.Properties;
-import java.util.concurrent.TimeUnit;
 
 import javax.inject.Inject;
 
@@ -35,7 +34,6 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.awaitility.Awaitility.await;
 
 public class CamelMainRoutesDiscoveryTest {
     @RegisterExtension
@@ -64,9 +62,6 @@ public class CamelMainRoutesDiscoveryTest {
 
     @Test
     public void testRoutesDiscovery() {
-        // ensure the camel context is started before doing any assertion
-        await().atMost(10, TimeUnit.SECONDS).until(main::isStarted);
-
         assertThat(main.getCamelContext().getRoutes()).isEmpty();
         assertThat(main.configure().getRoutesBuilders()).isEmpty();
     }
diff --git a/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainRoutesFilterTest.java b/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainRoutesFilterTest.java
index a9bfb95..fa0ea0f 100644
--- a/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainRoutesFilterTest.java
+++ b/extensions-core/main/deployment/src/test/java/org/apache/camel/quarkus/main/deployment/CamelMainRoutesFilterTest.java
@@ -20,7 +20,6 @@ import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.util.Properties;
-import java.util.concurrent.TimeUnit;
 
 import javax.inject.Inject;
 
@@ -35,7 +34,6 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.awaitility.Awaitility.await;
 
 public class CamelMainRoutesFilterTest {
     @RegisterExtension
@@ -65,9 +63,6 @@ public class CamelMainRoutesFilterTest {
 
     @Test
     public void testRoutesFilter() {
-        // ensure the camel context is started before doing any assertion
-        await().atMost(10, TimeUnit.SECONDS).until(main::isStarted);
-
         assertThat(main.configure().getRoutesBuilders())
                 .hasSize(1)
                 .first().isInstanceOf(MyRoute.class);
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 5d2b383..a4b70a6 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
@@ -33,10 +33,14 @@ import org.apache.camel.main.MainListener;
 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;
 
 public final class CamelMain extends MainCommandLineSupport implements HasCamelContext {
+    private final AtomicBoolean engineStarted;
+
     public CamelMain(CamelContext camelContext) {
         this.camelContext = camelContext;
+        this.engineStarted = new AtomicBoolean();
     }
 
     @Override
@@ -74,6 +78,7 @@ public final class CamelMain extends MainCommandLineSupport implements HasCamelC
     protected void doStop() throws Exception {
         super.doStop();
         this.camelContext.stop();
+        this.engineStarted.set(false);
     }
 
     @Override
@@ -100,6 +105,47 @@ public final class CamelMain extends MainCommandLineSupport implements HasCamelC
     }
 
     /**
+     * Start the engine.
+     */
+    public void startEngine() throws Exception {
+        if (shutdownStrategy.isRunAllowed() && engineStarted.compareAndSet(false, true)) {
+            init();
+            beforeStart();
+            start();
+            afterStart();
+        }
+    }
+
+    /**
+     * Start the engine if not done already and wait until completed, or the JVM terminates.
+     */
+    public void runEngine() throws Exception {
+        if (shutdownStrategy.isRunAllowed()) {
+            startEngine();
+            waitUntilCompleted();
+
+            try {
+                if (camelTemplate != null) {
+                    ServiceHelper.stopService(camelTemplate);
+                    camelTemplate = null;
+                }
+            } catch (Exception e) {
+                LOG.debug("Error stopping camelTemplate due " + e.getMessage() + ". This exception is ignored.", e);
+            }
+
+            beforeStop();
+            stop();
+            afterStop();
+        }
+    }
+
+    @Override
+    public void run(String[] args) throws Exception {
+        parseArguments(args);
+        runEngine();
+    }
+
+    /**
      * Implementation of a {@link MainShutdownStrategy} based on Quarkus Command Mode.
      *
      * @see <a href="https://quarkus.io/guides/command-mode-reference">Quarkus Command Mode Applications</a>
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 7b1a964..ff0d324 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
@@ -41,15 +41,24 @@ public class CamelMainRuntime implements CamelRuntime {
             LOGGER.info("Starting camel-quarkus with args: {}", Arrays.toString(args));
         }
 
-        new Thread(() -> {
-            try {
-                main.run(args);
-            } catch (Exception e) {
-                LOGGER.error("Failed to start application", e);
-                stop();
-                throw new RuntimeException(e);
-            }
-        }).start();
+        try {
+            main.parseArguments(args);
+            main.startEngine();
+
+            new Thread(() -> {
+                try {
+                    main.runEngine();
+                } catch (Exception e) {
+                    LOGGER.error("Failed to start application", e);
+                    stop();
+                    throw new RuntimeException(e);
+                }
+            }).start();
+        } catch (Exception e) {
+            LOGGER.error("Failed to start application", e);
+            stop();
+            throw new RuntimeException(e);
+        }
     }
 
     @Override