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

nfilotto pushed a commit to branch CAMEL-18217/suspend-messages-with-system-prop
in repository https://gitbox.apache.org/repos/asf/camel.git

commit f657010b5b6f96bf80557b58ae8cf32bdab255da
Author: Nicolas Filotto <[email protected]>
AuthorDate: Fri Jun 24 17:05:34 2022 +0200

    CAMEL-18217: debugger - Allow to suspend messages using a system property
---
 .../camel/impl/debugger/BacklogDebugger.java       | 24 +++++++++++++++---
 .../camel/management/BacklogDebuggerTest.java      | 29 ++++++++++++++++++++--
 docs/user-manual/modules/ROOT/pages/debugger.adoc  |  5 ++--
 3 files changed, 51 insertions(+), 7 deletions(-)

diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/BacklogDebugger.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/BacklogDebugger.java
index 246bb05bd27..c294e04f84d 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/BacklogDebugger.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/BacklogDebugger.java
@@ -68,6 +68,11 @@ public final class BacklogDebugger extends ServiceSupport {
      * {@code BacklogDebugger} should suspend processing the messages and wait 
for a debugger to attach or not.
      */
     public static final String SUSPEND_MODE_ENV_VAR_NAME = 
"CAMEL_DEBUGGER_SUSPEND";
+    /**
+     * The name of the system property that contains the value of the flag 
indicating whether the
+     * {@code BacklogDebugger} should suspend processing the messages and wait 
for a debugger to attach or not.
+     */
+    public static final String SUSPEND_MODE_SYSTEM_PROP_NAME = 
"org.apache.camel.debugger.suspend";
     private static final Logger LOG = 
LoggerFactory.getLogger(BacklogDebugger.class);
 
     private long fallbackTimeout = 300;
@@ -136,14 +141,15 @@ public final class BacklogDebugger extends ServiceSupport 
{
     /**
      * Creates a new backlog debugger.
      * <p>
-     * In case the environment variable {@link #SUSPEND_MODE_ENV_VAR_NAME} has 
been set to {@code true}, the message
-     * processing is directly suspended.
+     * In case the environment variable {@link #SUSPEND_MODE_ENV_VAR_NAME} or 
the system property
+     * {@link #SUSPEND_MODE_SYSTEM_PROP_NAME} has been set to {@code true}, 
the message processing is directly
+     * suspended.
      *
      * @param  context Camel context
      * @return         a new backlog debugger
      */
     public static BacklogDebugger createDebugger(CamelContext context) {
-        return new BacklogDebugger(context, 
Boolean.parseBoolean(System.getenv(SUSPEND_MODE_ENV_VAR_NAME)));
+        return new BacklogDebugger(context, resolveSuspendMode());
     }
 
     /**
@@ -219,6 +225,18 @@ public final class BacklogDebugger extends ServiceSupport {
         }
     }
 
+    /**
+     * Resolves the value of the flag indicating whether the {@code 
BacklogDebugger} should suspend processing the
+     * messages and wait for a debugger to attach or not.
+     *
+     * @return the value of the environment variable {@link 
#SUSPEND_MODE_ENV_VAR_NAME} if it has been set, otherwise
+     *         the value of the system property {@link 
#SUSPEND_MODE_SYSTEM_PROP_NAME}, {@code false} by default.
+     */
+    private static boolean resolveSuspendMode() {
+        final String value = System.getenv(SUSPEND_MODE_ENV_VAR_NAME);
+        return value == null ? 
Boolean.getBoolean(SUSPEND_MODE_SYSTEM_PROP_NAME) : Boolean.parseBoolean(value);
+    }
+
     /**
      * Suspend the current thread if the <i>suspend mode</i> is enabled as 
long as the method {@link #attach()} is not
      * called. Do nothing otherwise.
diff --git 
a/core/camel-management/src/test/java/org/apache/camel/management/BacklogDebuggerTest.java
 
b/core/camel-management/src/test/java/org/apache/camel/management/BacklogDebuggerTest.java
index 24a1e03f758..21386b3526c 100644
--- 
a/core/camel-management/src/test/java/org/apache/camel/management/BacklogDebuggerTest.java
+++ 
b/core/camel-management/src/test/java/org/apache/camel/management/BacklogDebuggerTest.java
@@ -29,6 +29,7 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.condition.DisabledOnOs;
 import org.junit.jupiter.api.condition.OS;
 import org.junitpioneer.jupiter.SetEnvironmentVariable;
+import org.junitpioneer.jupiter.SetSystemProperty;
 
 import static org.awaitility.Awaitility.await;
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -893,11 +894,35 @@ public class BacklogDebuggerTest extends 
ManagementTestSupport {
     }
 
     /**
-     * Ensure that the suspend mode works as expected.
+     * Ensure that the suspend mode works as expected when it is set using an 
environment variable.
      */
     @Test
     @SetEnvironmentVariable(key = BacklogDebugger.SUSPEND_MODE_ENV_VAR_NAME, 
value = "true")
-    public void testSuspendMode() throws Exception {
+    public void testSuspendModeConfiguredWithEnvVariable() throws Exception {
+        testSuspendMode();
+    }
+
+    /**
+     * Ensure that the suspend mode works as expected when it is set using a 
system property.
+     */
+    @Test
+    @SetSystemProperty(key = BacklogDebugger.SUSPEND_MODE_SYSTEM_PROP_NAME, 
value = "true")
+    public void testSuspendModeConfiguredWithSystemProperty() throws Exception 
{
+        testSuspendMode();
+    }
+
+    /**
+     * Ensure that the suspend mode works as expected when it is configured by 
relying on the precedence of the env
+     * variable over the system property.
+     */
+    @Test
+    @SetEnvironmentVariable(key = BacklogDebugger.SUSPEND_MODE_ENV_VAR_NAME, 
value = "true")
+    @SetSystemProperty(key = BacklogDebugger.SUSPEND_MODE_SYSTEM_PROP_NAME, 
value = "false")
+    public void testSuspendModeConfiguredWithBoth() throws Exception {
+        testSuspendMode();
+    }
+
+    private void testSuspendMode() throws Exception {
         MBeanServer mbeanServer = getMBeanServer();
         ObjectName on = new ObjectName(
                 "org.apache.camel:context=" + context.getManagementName() + 
",type=tracer,name=BacklogDebugger");
diff --git a/docs/user-manual/modules/ROOT/pages/debugger.adoc 
b/docs/user-manual/modules/ROOT/pages/debugger.adoc
index 845ed592305..b42c96c40fa 100644
--- a/docs/user-manual/modules/ROOT/pages/debugger.adoc
+++ b/docs/user-manual/modules/ROOT/pages/debugger.adoc
@@ -93,8 +93,9 @@ which can be used to extend for custom implementations.
 There is also a xref:backlog-debugger.adoc[Backlog Debugger] which allows 
debugging from JMX that is included into `camel-debug`.
 
 To be able to have enough time to add your breakpoints, you could need to 
suspend the message processing of Camel to make sure
-that you won't miss any messages. For this kind of need, you have to set the 
environment variable `CAMEL_DEBUGGER_SUSPEND` to `true`
-within the context of your application, then the `Backlog Debugger` suspends 
the message processing until the JMX operation `attach` is called. Calling the 
JMX operation `detach` suspends again the message processing.
+that you won't miss any messages. For this kind of need, you have to set 
either the environment variable `CAMEL_DEBUGGER_SUSPEND` or the system property 
`org.apache.camel.debugger.suspend` to `true` within the context of your 
application, then the `Backlog Debugger` suspends the message processing until 
the JMX operation `attach` is called. Calling the JMX operation `detach` 
suspends again the message processing.
+
+In case the environment variable and the system property are both set, the 
value of the environment variable is used.
 
 Several 3rd party tooling are using it:
 - https://hawt.io/[hawtio] uses this for its web based debugging functionality

Reply via email to