You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2022/06/24 17:03:52 UTC

[camel] branch main updated: CAMEL-18217: debugger - Allow to suspend messages using a system property (#7875)

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

nfilotto 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 5906100ba36 CAMEL-18217: debugger - Allow to suspend messages using a system property (#7875)
5906100ba36 is described below

commit 5906100ba36309e5122ae49cf20036220fe47e37
Author: Nicolas Filotto <es...@users.noreply.github.com>
AuthorDate: Fri Jun 24 19:03:42 2022 +0200

    CAMEL-18217: debugger - Allow to suspend messages using a system property (#7875)
    
    ## Motivation
    
    What has been done in the first part of this feature is good but for the sake of simplicity it is also interesting to have the ability to configure it using a System property.
    
    ## Modifications:
    
    * Allow to use the system property `org.apache.camel.debugger.suspend` to configure the `suspend mode`
    * Ensure that the environment variable takes precedence over the system property
    * Add the corresponding test and doc.
    * Remove the log message that was for debugging purpose
    
    ## Result
    
    It is now possible to configure the `suspend mode` using the system property `org.apache.camel.debugger.suspend`.
---
 .../camel/impl/debugger/BacklogDebugger.java       | 25 ++++++++++++++++---
 .../camel/management/BacklogDebuggerTest.java      | 29 ++++++++++++++++++++--
 docs/user-manual/modules/ROOT/pages/debugger.adoc  |  5 ++--
 3 files changed, 51 insertions(+), 8 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..f95315ed42f 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.
@@ -226,7 +244,6 @@ public final class BacklogDebugger extends ServiceSupport {
     private void suspendIfNeeded() {
         final CountDownLatch countDownLatch = suspend.get();
         if (countDownLatch != null) {
-            logger.log("Incoming message suspended");
             try {
                 countDownLatch.await();
             } catch (InterruptedException e) {
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