This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 8413c30c952 CAMEL-18425: camel-cli - Make regular Camel applications work with Camel CLI 8413c30c952 is described below commit 8413c30c9525047fba891337f9e81aac17de512d Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Aug 27 12:17:03 2022 +0200 CAMEL-18425: camel-cli - Make regular Camel applications work with Camel CLI --- .../camel/impl/engine/AbstractCamelContext.java | 4 +- .../camel/cli/connector/LocalCliConnector.java | 46 ++++++++++++++-------- .../core/commands/process/ProcessBaseCommand.java | 6 +-- 3 files changed, 35 insertions(+), 21 deletions(-) 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 93ee4bf669a..480d533493a 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 @@ -3424,8 +3424,8 @@ public abstract class AbstractCamelContext extends BaseService // Stop the route controller ServiceHelper.stopAndShutdownService(this.routeController); - // stop route inputs in the same order as they was started so we stop - // the very first inputs first + // stop route inputs in the same order as they were started, so we stop + // the very first inputs at first try { // force shutting down routes as they may otherwise cause shutdown to hang if (shutdownStrategy != null) { diff --git a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java index b4fea6c2331..661cc080fae 100644 --- a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java +++ b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java @@ -19,8 +19,10 @@ package org.apache.camel.cli.connector; import java.io.File; import java.util.Collection; import java.util.Locale; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -56,6 +58,7 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C private String mainClass; private final AtomicBoolean terminating = new AtomicBoolean(); private ScheduledExecutorService executor; + private volatile ExecutorService terminateExecutor; private File lockFile; private File statusFile; @@ -124,32 +127,37 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C @Override public void sigterm() { // we are terminating - LOG.info("Camel CLI terminating JVM"); terminating.set(true); - try { - camelContext.stop(); - } finally { - if (lockFile != null) { - FileUtil.deleteFile(lockFile); - } - if (statusFile != null) { - FileUtil.deleteFile(statusFile); + // spawn a thread that terminates, so we can keep this thread to update status + terminateExecutor = Executors.newSingleThreadExecutor(r -> { + String threadName = ThreadHelper.resolveThreadName(null, "Terminate JVM task"); + return new Thread(r, threadName); + }); + terminateExecutor.submit(new Runnable() { + @Override + public void run() { + LOG.info("Camel CLI terminating JVM"); + try { + camelContext.stop(); + } finally { + ServiceHelper.stopAndShutdownService(this); + } } - ServiceHelper.stopAndShutdownService(this); - } + }); } protected void statusTask() { - if (terminating.get()) { - return; // terminating in progress - } - if (!lockFile.exists()) { - // if the lock file is deleted then stop + if (!lockFile.exists() && terminating.compareAndSet(false, true)) { + // if the lock file is deleted then trigger termination sigterm(); return; } + if (!statusFile.exists()) { + return; + } try { + // even during termination then collect status as we want to see status changes during stopping JsonObject root = new JsonObject(); // what runtime are in use @@ -209,6 +217,12 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C @Override protected void doStop() throws Exception { // cleanup + if (lockFile != null) { + FileUtil.deleteFile(lockFile); + } + if (statusFile != null) { + FileUtil.deleteFile(statusFile); + } if (executor != null) { camelContext.getExecutorServiceManager().shutdown(executor); executor = null; diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ProcessBaseCommand.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ProcessBaseCommand.java index d0582c12c00..ff236c95a13 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ProcessBaseCommand.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ProcessBaseCommand.java @@ -203,11 +203,11 @@ abstract class ProcessBaseCommand extends CamelCommand { } else if (status == 7) { return "Suspended"; } else if (status == 8) { - return "Stopping"; + return "Terminating"; } else if (status == 9) { - return "Stopped"; + return "Terminated"; } else { - return "Terminating"; + return "Terminated"; } }