You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/10/19 06:30:38 UTC

[camel] branch main updated: (chores) camel-main: cleanup shutdown action check

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

orpiske 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 acbc13f3562 (chores) camel-main: cleanup shutdown action check
acbc13f3562 is described below

commit acbc13f35628af41fac5f02db76b6dd0d7fa1fa0
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Wed Oct 18 17:02:46 2023 +0200

    (chores) camel-main: cleanup shutdown action check
---
 .../camel/main/MainDurationEventNotifier.java      | 54 ++++++++++++++++------
 1 file changed, 39 insertions(+), 15 deletions(-)

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 1a3dacc4901..1da8330822c 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,19 +33,39 @@ import org.slf4j.LoggerFactory;
  * maximum number of messages has been processed.
  */
 public class MainDurationEventNotifier extends EventNotifierSupport {
-
     private static final Logger LOG = LoggerFactory.getLogger(MainLifecycleStrategy.class);
+
+    private enum Action {
+        SHUTDOWN,
+        STOP;
+
+        static Action toAction(String action) {
+            if ("shutdown".equals(action)) {
+                return SHUTDOWN;
+            }
+
+            if ("stop".equals(action)) {
+                return STOP;
+            }
+
+            LOG.warn("Invalid action: {}. Main execution will be aborted during initialization", action);
+            return null;
+        }
+    }
+
+
     private final CamelContext camelContext;
     private final int maxMessages;
     private final long maxIdleSeconds;
     private final MainShutdownStrategy shutdownStrategy;
     private final boolean stopCamelContext;
     private final boolean restartDuration;
-    private final String action;
+    private final Action action;
     private final LongAdder doneMessages;
     private volatile StopWatch watch;
     private volatile ScheduledExecutorService idleExecutorService;
 
+
     public MainDurationEventNotifier(CamelContext camelContext, int maxMessages, long maxIdleSeconds,
                                      MainShutdownStrategy shutdownStrategy, boolean stopCamelContext,
                                      boolean restartDuration, String action) {
@@ -55,7 +75,7 @@ public class MainDurationEventNotifier extends EventNotifierSupport {
         this.shutdownStrategy = shutdownStrategy;
         this.stopCamelContext = stopCamelContext;
         this.restartDuration = restartDuration;
-        this.action = action.toLowerCase();
+        this.action = Action.toAction(action);
         this.doneMessages = new LongAdder();
 
         if (maxMessages == 0 && maxIdleSeconds == 0) {
@@ -111,12 +131,12 @@ public class MainDurationEventNotifier extends EventNotifierSupport {
     }
 
     private void triggerDoneEvent() {
-        if ("shutdown".equalsIgnoreCase(action)) {
+        if (action == Action.SHUTDOWN) {
             LOG.info("Duration max messages triggering shutdown of the JVM");
             // use thread to shut down Camel as otherwise we would block current thread
             camelContext.getExecutorServiceManager().newThread("CamelMainShutdownCamelContext", this::shutdownTask)
                     .start();
-        } else if ("stop".equalsIgnoreCase(action)) {
+        } else if (action == Action.STOP) {
             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)
@@ -161,7 +181,7 @@ public class MainDurationEventNotifier extends EventNotifierSupport {
     protected void doInit() throws Exception {
         super.doInit();
 
-        if (!action.equals("shutdown") && !action.equals("stop")) {
+        if (action == null) {
             throw new IllegalArgumentException("Unknown action: " + action);
         }
     }
@@ -238,15 +258,19 @@ public class MainDurationEventNotifier extends EventNotifierSupport {
         LOG.trace("Duration max idle check {} >= {} -> {}", seconds, maxIdleSeconds, result);
 
         if (result && shutdownStrategy.isRunAllowed()) {
-            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();
-            }
+            triggerIdleEvent();
+        }
+    }
+
+    private void triggerIdleEvent() {
+        if (action == Action.SHUTDOWN) {
+            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 (action == Action.STOP) {
+            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();
         }
     }
 }