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 2021/11/23 09:24:01 UTC

[camel] 01/08: CAMEL-17223: camel-main - durationMaxAction to control whether to shutdown or stop all routes.

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

commit fc13464dd4a48150c523408bb28ce35da0760ca6
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Nov 22 15:34:36 2021 +0100

    CAMEL-17223: camel-main - durationMaxAction to control whether to shutdown or stop all routes.
---
 .../MainConfigurationPropertiesConfigurer.java     |  6 +++
 .../camel-main-configuration-metadata.json         |  1 +
 core/camel-main/src/main/docs/main.adoc            |  3 +-
 .../camel/main/DefaultConfigurationProperties.java | 23 ++++++++++
 .../camel/main/MainDurationEventNotifier.java      | 52 ++++++++++++++++++----
 .../java/org/apache/camel/main/MainSupport.java    |  3 +-
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 10 +++--
 7 files changed, 83 insertions(+), 15 deletions(-)

diff --git a/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java b/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
index ea92db0..ca41200 100644
--- a/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
+++ b/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
@@ -57,6 +57,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
         case "DumpRoutes": target.setDumpRoutes(property(camelContext, boolean.class, value)); return true;
         case "durationhitexitcode":
         case "DurationHitExitCode": target.setDurationHitExitCode(property(camelContext, int.class, value)); return true;
+        case "durationmaxaction":
+        case "DurationMaxAction": target.setDurationMaxAction(property(camelContext, java.lang.String.class, value)); return true;
         case "durationmaxidleseconds":
         case "DurationMaxIdleSeconds": target.setDurationMaxIdleSeconds(property(camelContext, int.class, value)); return true;
         case "durationmaxmessages":
@@ -266,6 +268,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
         case "DumpRoutes": return boolean.class;
         case "durationhitexitcode":
         case "DurationHitExitCode": return int.class;
+        case "durationmaxaction":
+        case "DurationMaxAction": return java.lang.String.class;
         case "durationmaxidleseconds":
         case "DurationMaxIdleSeconds": return int.class;
         case "durationmaxmessages":
@@ -476,6 +480,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
         case "DumpRoutes": return target.isDumpRoutes();
         case "durationhitexitcode":
         case "DurationHitExitCode": return target.getDurationHitExitCode();
+        case "durationmaxaction":
+        case "DurationMaxAction": return target.getDurationMaxAction();
         case "durationmaxidleseconds":
         case "DurationMaxIdleSeconds": return target.getDurationMaxIdleSeconds();
         case "durationmaxmessages":
diff --git a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
index bbb41bd..8efbdb0 100644
--- a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
+++ b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
@@ -28,6 +28,7 @@
     { "name": "camel.main.consumerTemplateCacheSize", "description": "Consumer template endpoints cache size.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "integer", "javaType": "int", "defaultValue": 1000 },
     { "name": "camel.main.dumpRoutes", "description": "If dumping is enabled then Camel will during startup dump all loaded routes (incl rests and route templates) represented as XML DSL into the log. This is intended for trouble shooting or to assist during development. Sensitive information that may be configured in the route endpoints could potentially be included in the dump output and is therefore not recommended to be used for production usage. This requires to have camel-xml-jaxb  [...]
     { "name": "camel.main.durationHitExitCode", "description": "Sets the exit code for the application if duration was hit", "sourceType": "org.apache.camel.main.MainConfigurationProperties", "type": "integer", "javaType": "int" },
+    { "name": "camel.main.durationMaxAction", "description": "Controls whether the Camel application should shutdown the JVM, or stop all routes, when duration max is triggered.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "string", "javaType": "java.lang.String", "defaultValue": "terminate", "enum": [ "shutdown", "stop" ] },
     { "name": "camel.main.durationMaxIdleSeconds", "description": "To specify for how long time in seconds Camel can be idle before automatic terminating the JVM. You can use this to run Camel for a short while.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "integer", "javaType": "int" },
     { "name": "camel.main.durationMaxMessages", "description": "To specify how many messages to process by Camel before automatic terminating the JVM. You can use this to run Camel for a short while.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "integer", "javaType": "int" },
     { "name": "camel.main.durationMaxSeconds", "description": "To specify for how long time in seconds to keep running the JVM before automatic terminating the JVM. You can use this to run Camel for a short while.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "integer", "javaType": "int" },
diff --git a/core/camel-main/src/main/docs/main.adoc b/core/camel-main/src/main/docs/main.adoc
index 577e32e..86a90b3 100644
--- a/core/camel-main/src/main/docs/main.adoc
+++ b/core/camel-main/src/main/docs/main.adoc
@@ -21,7 +21,7 @@ The following tables lists all the options:
 
 // main options: START
 === Camel Main configurations
-The camel.main supports 101 options, which are listed below.
+The camel.main supports 102 options, which are listed below.
 
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
@@ -44,6 +44,7 @@ The camel.main supports 101 options, which are listed below.
 | *camel.main.consumerTemplate{zwsp}CacheSize* | Consumer template endpoints cache size. | 1000 | int
 | *camel.main.dumpRoutes* | If dumping is enabled then Camel will during startup dump all loaded routes (incl rests and route templates) represented as XML DSL into the log. This is intended for trouble shooting or to assist during development. Sensitive information that may be configured in the route endpoints could potentially be included in the dump output and is therefore not recommended to be used for production usage. This requires to have camel-xml-jaxb on the classpath to be able [...]
 | *camel.main.durationHitExitCode* | Sets the exit code for the application if duration was hit |  | int
+| *camel.main.durationMaxAction* | Controls whether the Camel application should shutdown the JVM, or stop all routes, when duration max is triggered. | terminate | String
 | *camel.main.durationMaxIdle{zwsp}Seconds* | To specify for how long time in seconds Camel can be idle before automatic terminating the JVM. You can use this to run Camel for a short while. |  | int
 | *camel.main.durationMaxMessages* | To specify how many messages to process by Camel before automatic terminating the JVM. You can use this to run Camel for a short while. |  | int
 | *camel.main.durationMaxSeconds* | To specify for how long time in seconds to keep running the JVM before automatic terminating the JVM. You can use this to run Camel for a short while. |  | int
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
index 649483c..9bb457b 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
@@ -37,6 +37,8 @@ public abstract class DefaultConfigurationProperties<T> {
     private int durationMaxSeconds;
     private int durationMaxIdleSeconds;
     private int durationMaxMessages;
+    @Metadata(defaultValue = "terminate", enums = "shutdown,stop")
+    private String durationMaxAction = "shutdown";
     private int shutdownTimeout = 45;
     private boolean shutdownSuppressLoggingOnTimeout;
     private boolean shutdownNowOnTimeout = true;
@@ -195,6 +197,18 @@ public abstract class DefaultConfigurationProperties<T> {
         this.durationMaxMessages = durationMaxMessages;
     }
 
+    public String getDurationMaxAction() {
+        return durationMaxAction;
+    }
+
+    /**
+     * Controls whether the Camel application should shutdown the JVM, or stop all routes, when duration max is
+     * triggered.
+     */
+    public void setDurationMaxAction(String durationMaxAction) {
+        this.durationMaxAction = durationMaxAction;
+    }
+
     public int getShutdownTimeout() {
         return shutdownTimeout;
     }
@@ -1421,6 +1435,15 @@ public abstract class DefaultConfigurationProperties<T> {
     }
 
     /**
+     * Controls whether the Camel application should shutdown the JVM, or stop all routes, when duration max is
+     * triggered.
+     */
+    public T withDurationMaxAction(String durationMaxAction) {
+        this.durationMaxAction = durationMaxAction;
+        return (T) this;
+    }
+
+    /**
      * Timeout in seconds to graceful shutdown Camel.
      */
     public T withShutdownTimeout(int shutdownTimeout) {
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainDurationEventNotifier.java b/core/camel-main/src/main/java/org/apache/camel/main/MainDurationEventNotifier.java
index 699669f..586ad25 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainDurationEventNotifier.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainDurationEventNotifier.java
@@ -33,8 +33,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * A {@link org.apache.camel.spi.EventNotifier} to trigger shutdown of the Main JVM when maximum number of messages has
- * been processed.
+ * A {@link org.apache.camel.spi.EventNotifier} to trigger (shutdown of the Main JVM, or stopping all routes) when
+ * maximum number of messages has been processed.
  */
 public class MainDurationEventNotifier extends EventNotifierSupport {
 
@@ -45,6 +45,7 @@ public class MainDurationEventNotifier extends EventNotifierSupport {
     private final MainShutdownStrategy shutdownStrategy;
     private final boolean stopCamelContext;
     private final boolean restartDuration;
+    private final String action;
     private final AtomicInteger doneMessages;
 
     private volatile StopWatch watch;
@@ -52,13 +53,14 @@ public class MainDurationEventNotifier extends EventNotifierSupport {
 
     public MainDurationEventNotifier(CamelContext camelContext, int maxMessages, long maxIdleSeconds,
                                      MainShutdownStrategy shutdownStrategy, boolean stopCamelContext,
-                                     boolean restartDuration) {
+                                     boolean restartDuration, String action) {
         this.camelContext = camelContext;
         this.maxMessages = maxMessages;
         this.maxIdleSeconds = maxIdleSeconds;
         this.shutdownStrategy = shutdownStrategy;
         this.stopCamelContext = stopCamelContext;
         this.restartDuration = restartDuration;
+        this.action = action.toLowerCase();
         this.doneMessages = new AtomicInteger();
     }
 
@@ -98,9 +100,16 @@ public class MainDurationEventNotifier extends EventNotifierSupport {
             LOG.trace("Duration max messages check {} >= {} -> {}", doneMessages.get(), maxMessages, result);
 
             if (result && shutdownStrategy.isRunAllowed()) {
-                LOG.info("Duration max messages triggering shutdown of the JVM");
-                // use thread to stop Camel as otherwise we would block current thread
-                camelContext.getExecutorServiceManager().newThread("CamelMainShutdownCamelContext", this::shutdownTask).start();
+                if ("shutdown".equalsIgnoreCase(action)) {
+                    LOG.info("Duration max messages triggering shutdown of the JVM");
+                    // use thread to shutdown Camel as otherwise we would block current thread
+                    camelContext.getExecutorServiceManager().newThread("CamelMainShutdownCamelContext", this::shutdownTask)
+                            .start();
+                } else if ("stop".equalsIgnoreCase(action)) {
+                    LOG.info("Duration max messages triggering stopping all routes");
+                    // use thread to stop routes as otherwise we would block current thread
+                    camelContext.getExecutorServiceManager().newThread("CamelMainShutdownCamelContext", this::stopTask).start();
+                }
             }
         }
 
@@ -125,6 +134,15 @@ public class MainDurationEventNotifier extends EventNotifierSupport {
     }
 
     @Override
+    protected void doInit() throws Exception {
+        super.doInit();
+
+        if (!action.equals("shutdown") && !action.equals("stop")) {
+            throw new IllegalArgumentException("Unknown action: " + action);
+        }
+    }
+
+    @Override
     protected void doStart() throws Exception {
         if (maxIdleSeconds > 0) {
             // we only start watch when Camel is started
@@ -136,6 +154,16 @@ public class MainDurationEventNotifier extends EventNotifierSupport {
         }
     }
 
+    private void stopTask() {
+        // don't run the task if shutdown is in process
+        if (!shutdownStrategy.isRunAllowed()) {
+            return;
+        }
+
+        // TODO: stop all routes via shutdown strategy
+        LOG.warn("Stopping all routes!!!");
+    }
+
     private void shutdownTask() {
         // don't run the task if shutdown is in process
         if (!shutdownStrategy.isRunAllowed()) {
@@ -183,9 +211,15 @@ public class MainDurationEventNotifier extends EventNotifierSupport {
         LOG.trace("Duration max idle check {} >= {} -> {}", seconds, maxIdleSeconds, result);
 
         if (result && shutdownStrategy.isRunAllowed()) {
-            LOG.info("Duration max idle triggering shutdown of the JVM");
-            // use thread to stop Camel as otherwise we would block current thread
-            camelContext.getExecutorServiceManager().newThread("CamelMainShutdownCamelContext", this::shutdownTask).start();
+            if ("shutdown".equals(action)) {
+                LOG.info("Duration max idle triggering shutdown of the JVM");
+                // use thread to stop Camel as otherwise we would block current thread
+                camelContext.getExecutorServiceManager().newThread("CamelMainShutdownCamelContext", this::shutdownTask).start();
+            } else if ("stop".equals(action)) {
+                LOG.info("Duration max idle triggering stopping all routes");
+                // use thread to stop Camel as otherwise we would block current thread
+                camelContext.getExecutorServiceManager().newThread("CamelMainShutdownCamelContext", this::stopTask).start();
+            }
         }
     }
 }
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java
index 6b9e13a..4d40a91 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java
@@ -281,7 +281,8 @@ public abstract class MainSupport extends BaseMainSupport {
                     mainConfigurationProperties.getDurationMaxIdleSeconds(),
                     shutdownStrategy,
                     true,
-                    mainConfigurationProperties.isRoutesReloadRestartDuration());
+                    mainConfigurationProperties.isRoutesReloadRestartDuration(),
+                    mainConfigurationProperties.getDurationMaxAction());
 
             // register our event notifier
             ServiceHelper.startService(notifier);
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 8c83bfd..4b0999b 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -142,7 +142,7 @@ class Run implements Callable<Integer> {
         if (!ResourceHelper.hasScheme(binding) && !binding.startsWith("github:")) {
             binding = "file:" + binding;
         }
-        main.addInitialProperty("camel.main.routes-include-pattern", binding);
+        main.addInitialProperty("camel.main.routesIncludePattern", binding);
 
         if (binding.startsWith("file:")) {
             // check if file exist
@@ -154,10 +154,12 @@ class Run implements Callable<Integer> {
 
             // we can only reload if file based
             if (reload) {
-                main.addInitialProperty("camel.main.routes-reload-enabled", "true");
-                main.addInitialProperty("camel.main.routes-reload-directory", ".");
+                main.addInitialProperty("camel.main.routesReloadEnabled", "true");
+                main.addInitialProperty("camel.main.routesReloadDirectory", ".");
                 // skip file: as prefix
-                main.addInitialProperty("camel.main.routes-reload-pattern", binding.substring(5));
+                main.addInitialProperty("camel.main.routesReloadPattern", binding.substring(5));
+                // do not shutdown the JVM but stop routes when max duration is triggered
+                main.addInitialProperty("camel.main.durationMaxAction", "stop");
             }
         }