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 48f0448f54a CAMEL-18425: camel-cli - Make regular Camel applications 
work with Camel CLI
48f0448f54a is described below

commit 48f0448f54ad4f2df1802ba87b783a7758da5b1b
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Sat Aug 27 09:00:32 2022 +0200

    CAMEL-18425: camel-cli - Make regular Camel applications work with Camel CLI
---
 .../java/org/apache/camel/spi/CliConnector.java    | 26 +++++++++++++++
 .../org/apache/camel/spi/CliConnectorFactory.java  |  2 +-
 .../camel/cli/connector/LocalCliConnector.java     | 39 ++++++++++++++++------
 3 files changed, 55 insertions(+), 12 deletions(-)

diff --git 
a/core/camel-api/src/main/java/org/apache/camel/spi/CliConnector.java 
b/core/camel-api/src/main/java/org/apache/camel/spi/CliConnector.java
index 3baf3ae682a..ded99a33e06 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/CliConnector.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/CliConnector.java
@@ -1,8 +1,34 @@
+/*
+ * 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.spi;
 
 import org.apache.camel.NonManagedService;
 import org.apache.camel.StaticService;
 
+/**
+ * Local {@link CliConnector} that allows Camel CLI to manage local running 
Camel integrations.
+ */
 public interface CliConnector extends StaticService, NonManagedService {
 
+    /**
+     * Signal to terminate Camel and the JVM.
+     *
+     * The Camel CLI can trigger this by the command: camel stop PID
+     */
+    void sigterm();
+
 }
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/spi/CliConnectorFactory.java 
b/core/camel-api/src/main/java/org/apache/camel/spi/CliConnectorFactory.java
index 2a3710e1f03..e3caf59c108 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/CliConnectorFactory.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/CliConnectorFactory.java
@@ -21,7 +21,7 @@ import org.apache.camel.Service;
 /**
  * Factory for creating connector to CLI tooling.
  * <p/>
- * Such as a local connector that allows Camel CLI to manage local running 
Camel integrations.
+ * Such as a local {@link CliConnector} that allows Camel CLI to manage local 
running Camel integrations.
  */
 public interface CliConnectorFactory {
 
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 7a3ca9fc88c..f1b557df341 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
@@ -22,6 +22,7 @@ import java.util.Locale;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
@@ -31,6 +32,7 @@ import org.apache.camel.health.HealthCheck;
 import org.apache.camel.health.HealthCheckHelper;
 import org.apache.camel.spi.CliConnector;
 import org.apache.camel.spi.CliConnectorFactory;
+import org.apache.camel.support.service.ServiceHelper;
 import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
@@ -51,6 +53,7 @@ public class LocalCliConnector extends ServiceSupport 
implements CliConnector, C
     private String platform;
     private String platformVersion;
     private String mainClass;
+    private final AtomicBoolean terminating = new AtomicBoolean();
     private ScheduledExecutorService executor;
     private File lockFile;
     private File statusFile;
@@ -67,6 +70,8 @@ public class LocalCliConnector extends ServiceSupport 
implements CliConnector, C
 
     @Override
     protected void doStart() throws Exception {
+        terminating.set(false);
+
         // what platform are we running
         CliConnectorFactory ccf = 
camelContext.adapt(ExtendedCamelContext.class).getCliConnectorFactory();
         mainClass = ccf.getRuntimeStartClass();
@@ -112,19 +117,31 @@ public class LocalCliConnector extends ServiceSupport 
implements CliConnector, C
         }
     }
 
+    @Override
+    public void sigterm() {
+        // we are terminating
+        terminating.set(true);
+
+        try {
+            camelContext.stop();
+        } finally {
+            if (lockFile != null) {
+                FileUtil.deleteFile(lockFile);
+            }
+            if (statusFile != null) {
+                FileUtil.deleteFile(statusFile);
+            }
+            ServiceHelper.stopAndShutdownService(this);
+        }
+    }
+
     protected void statusTask() {
-        // if the lock file is deleted then stop
+        if (terminating.get()) {
+            return; // terminating in progress
+        }
         if (!lockFile.exists()) {
-            try {
-                camelContext.stop();
-            } finally {
-                if (lockFile != null) {
-                    FileUtil.deleteFile(lockFile);
-                }
-                if (statusFile != null) {
-                    FileUtil.deleteFile(statusFile);
-                }
-            }
+            // if the lock file is deleted then stop
+            sigterm();
             return;
         }
         try {

Reply via email to