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

jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new 8a3c123836c Give each Camel Dev UI console stream its own options and 
lifecycle
8a3c123836c is described below

commit 8a3c123836cf298639e7c294e16105de27743d80
Author: James Netherton <[email protected]>
AuthorDate: Mon Nov 30 12:14:46 2026 +0000

    Give each Camel Dev UI console stream its own options and lifecycle
    
    getConsoleJSON went through the per-console subscription map that backs
    streaming. A one-off call started a ticker that was never cancelled, and
    later calls for that console ran with the first caller's options instead
    of their own.
    
    Streams shared the same map, so a second browser tab reused the first
    tab's options, and closing one tab stopped the other's stream. Option
    updates also cleared and refilled the map while the ticker read it
    without a lock.
    
    - getConsoleJSON calls the console directly with its own options.
    - Each streamConsole call returns an independent Multi with fixed
      options. The Dev UI JSON-RPC router cancels it when the client
      unsubscribes.
    - Remove the subscription map along with deactivateConsoleStream and
      updateConsoleOptions.
    - qwc-camel-core.js: putOption reopens the stream with the new options,
      a stale initial fetch no longer opens a stream, and the stream is
      reopened whenever the page is attached again.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../src/main/resources/dev-ui/qwc-camel-core.js    |  34 +++---
 .../devui/CamelQuarkusCoreDevUIErrorsTest.java     |  12 +-
 .../camel/quarkus/devui/CamelCoreDevUIService.java | 125 ++++-----------------
 3 files changed, 52 insertions(+), 119 deletions(-)

diff --git 
a/extensions-core/core/deployment/src/main/resources/dev-ui/qwc-camel-core.js 
b/extensions-core/core/deployment/src/main/resources/dev-ui/qwc-camel-core.js
index e6f285855a9..9d624fe9123 100644
--- 
a/extensions-core/core/deployment/src/main/resources/dev-ui/qwc-camel-core.js
+++ 
b/extensions-core/core/deployment/src/main/resources/dev-ui/qwc-camel-core.js
@@ -63,17 +63,18 @@ export class QwcCamelCore extends QwcHotReloadElement {
         super();
         this._consoleId = consoleId;
         this._consoleOptions = consoleOptions;
+        this._streamGeneration = 0;
     }
 
     connectedCallback() {
         super.connectedCallback();
-        if (!this._consoleData) {
-            this.hotReload();
-        }
+        // Leaving the page cancels its stream, so reopen it on every attach. 
Any data already shown stays until
+        // the new fetch replaces it
+        this.hotReload();
     }
 
     disconnectedCallback() {
-        this.disableConsoleUpdates();
+        this._cancelObserver();
         super.disconnectedCallback();
     }
 
@@ -88,31 +89,34 @@ export class QwcCamelCore extends QwcHotReloadElement {
             this.jsonRpc._setExtensionName('camel-quarkus-core');
         }
 
+        // Each stream is bound to the options it was opened with, so take a 
copy
+        const options = {...this._consoleOptions};
+        const generation = this._streamGeneration;
+
         // Fetch initial data
-        this.jsonRpc.getConsoleJSON({'id': this._consoleId, 'options': 
this._consoleOptions}).then((consoleData) => {
+        this.jsonRpc.getConsoleJSON({'id': this._consoleId, 'options': 
options}).then((consoleData) => {
+            // Skip if the page was disconnected, or the options changed, 
while this fetch was in flight
+            if (generation !== this._streamGeneration || !this.isConnected) {
+                return;
+            }
             this._consoleData = JSON.parse(consoleData.result);
 
             // Set up live data streaming
-            this._observer = this.jsonRpc.streamConsole({'id': 
this._consoleId, 'options': this._consoleOptions}).onNext(consoleData => {
+            this._observer = this.jsonRpc.streamConsole({'id': 
this._consoleId, 'options': options}).onNext(consoleData => {
                 this._consoleData = JSON.parse(consoleData.result);
             });
         });
     }
 
-    disableConsoleUpdates() {
-        // Cancel live updates
-        this.jsonRpc.deactivateConsoleStream({'id': 
this._consoleId}).then((response) => {
-            this._cancelObserver();
-        });
-    }
-
     putOption(key, value) {
         if (value) {
             this._consoleOptions[key] = value;
         } else {
             delete this._consoleOptions[key];
         }
-        this.jsonRpc.updateConsoleOptions({'id': this._consoleId, 'options': 
this._consoleOptions});
+        // Reopen the stream with the new options
+        this._cancelObserver();
+        this.enableConsoleUpdates();
     }
 
     consoleData() {
@@ -165,8 +169,10 @@ export class QwcCamelCore extends QwcHotReloadElement {
     }
 
     _cancelObserver(){
+        this._streamGeneration++;
         if(this._observer){
             this._observer.cancel();
+            this._observer = null;
         }
     }
 }
diff --git 
a/extensions-core/core/deployment/src/test/java/org/apache/camel/quarkus/core/deployment/devui/CamelQuarkusCoreDevUIErrorsTest.java
 
b/extensions-core/core/deployment/src/test/java/org/apache/camel/quarkus/core/deployment/devui/CamelQuarkusCoreDevUIErrorsTest.java
index 97d1cc2d4fe..06b293f452c 100644
--- 
a/extensions-core/core/deployment/src/test/java/org/apache/camel/quarkus/core/deployment/devui/CamelQuarkusCoreDevUIErrorsTest.java
+++ 
b/extensions-core/core/deployment/src/test/java/org/apache/camel/quarkus/core/deployment/devui/CamelQuarkusCoreDevUIErrorsTest.java
@@ -43,9 +43,9 @@ public class CamelQuarkusCoreDevUIErrorsTest extends 
DevUIJsonRPCTest {
     }
 
     @Test
-    void errorsConsoleCapturesRoutingErrors() {
+    void errorsConsoleCapturesRoutingErrors() throws Exception {
         JsonObject failure = await().atMost(Duration.ofSeconds(10)).until(() 
-> {
-            JsonArray errors = getErrors();
+            JsonArray errors = getErrors(true);
             return errors.isEmpty() ? null : errors.getJsonObject(0);
         }, error -> error != null);
 
@@ -57,11 +57,15 @@ public class CamelQuarkusCoreDevUIErrorsTest extends 
DevUIJsonRPCTest {
         assertEquals(IllegalStateException.class.getName(), 
exception.getString("type"));
         assertEquals("Forced failure", exception.getString("message"));
         
assertTrue(exception.getString("stackTrace").contains("IllegalStateException"));
+
+        // Each one-off call is made with its own options, not those of an 
earlier call
+        JsonObject withoutStackTrace = 
getErrors(false).getJsonObject(0).getJsonObject("exception");
+        assertFalse(withoutStackTrace.containsKey("stackTrace"));
     }
 
-    private JsonArray getErrors() throws Exception {
+    private JsonArray getErrors(boolean stackTrace) throws Exception {
         String result = super.executeJsonRPCMethod(String.class, 
"getConsoleJSON",
-                Map.of("id", "errors", "options", Map.of("stackTrace", 
"true")));
+                Map.of("id", "errors", "options", Map.of("stackTrace", 
String.valueOf(stackTrace))));
         JsonObject json = (JsonObject) Json.decodeValue(result);
         assertTrue(json.getBoolean("enabled"));
         return json.getJsonArray("errors");
diff --git 
a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/devui/CamelCoreDevUIService.java
 
b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/devui/CamelCoreDevUIService.java
index 339903f0523..00bd41d7a45 100644
--- 
a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/devui/CamelCoreDevUIService.java
+++ 
b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/devui/CamelCoreDevUIService.java
@@ -25,15 +25,12 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
 
 import io.quarkus.arc.Arc;
 import io.quarkus.arc.ArcContainer;
 import io.quarkus.runtime.annotations.DevMCPEnableByDefault;
 import io.quarkus.runtime.annotations.JsonRpcDescription;
 import io.smallrye.mutiny.Multi;
-import io.smallrye.mutiny.operators.multi.processors.BroadcastProcessor;
-import io.smallrye.mutiny.subscription.Cancellable;
 import jakarta.enterprise.context.ApplicationScoped;
 import org.apache.camel.CamelContext;
 import org.apache.camel.catalog.EndpointValidationResult;
@@ -56,7 +53,6 @@ public class CamelCoreDevUIService {
     private static final Logger LOG = 
Logger.getLogger(CamelCoreDevUIService.class);
     private static volatile Set<String> ALLOWED_CONSOLE_IDS = 
Collections.emptySet();
     private static volatile Map<String, Map<String, Set<String>>> 
ALLOWED_CONSOLE_OPTIONS = Collections.emptyMap();
-    private final Map<String, ConsoleSubscription> PROCESSORS = new 
ConcurrentHashMap<>();
 
     static void setAllowedConsoleIds(Set<String> consoleIds) {
         ALLOWED_CONSOLE_IDS = Set.copyOf(consoleIds);
@@ -86,23 +82,6 @@ public class CamelCoreDevUIService {
         ALLOWED_CONSOLE_OPTIONS = Collections.unmodifiableMap(parsed);
     }
 
-    /**
-     * Deactivates a Dev UI subscription for the given Camel console id.
-     *
-     * @param  id The id of the Camel console
-     * @return    {@code true} if the console was deactivated, else {@code 
false}
-     */
-    public boolean deactivateConsoleStream(String id) {
-        ConsoleSubscription subscription = PROCESSORS.remove(id);
-        if (subscription != null) {
-            synchronized (subscription) {
-                subscription.cancel();
-            }
-            return true;
-        }
-        return false;
-    }
-
     /**
      * Gets a JSON String representation of a Camel console.
      *
@@ -115,11 +94,12 @@ public class CamelCoreDevUIService {
             LOG.debugf("Rejected Dev UI request for console '%s' — not in the 
allowlist", id);
             return "{}";
         }
-        return getOrCreateConsoleSubscription(id, sanitizeOptions(id, 
options)).callDevConsole();
+        return callDevConsole(id, sanitizeOptions(id, options));
     }
 
     /**
-     * Streams a JSON String representation of a Camel console.
+     * Streams a JSON String representation of a Camel console, refreshed at 
the configured Dev UI update interval.
+     * Each call returns an independent stream with its own options, which 
ends when the caller cancels it.
      *
      * @param  id      The id of the Camel console
      * @param  options The map of options to influence the JSON output
@@ -130,26 +110,13 @@ public class CamelCoreDevUIService {
             LOG.debugf("Rejected Dev UI stream request for console '%s' — not 
in the allowlist", id);
             return Multi.createFrom().empty();
         }
-        return getOrCreateConsoleSubscription(id, sanitizeOptions(id, 
options)).getBroadcaster();
-    }
-
-    /**
-     * Updates the map of options for a given console subscription.
-     *
-     * @param  id      The id of the Camel console
-     * @param  options The updated map of options to influence the JSON output
-     * @return         {@code true} if the options were updated, else {@code 
false}
-     */
-    public boolean updateConsoleOptions(String id, Map<String, Object> 
options) {
-        if (!isConsoleAllowed(id)) {
-            LOG.debugf("Rejected Dev UI options update for console '%s' — not 
in the allowlist", id);
-            return false;
-        }
         Map<String, Object> sanitized = sanitizeOptions(id, options);
-        return PROCESSORS.computeIfPresent(id, (key, consoleSubscription) -> {
-            consoleSubscription.setOptions(sanitized);
-            return consoleSubscription;
-        }) != null;
+        Duration interval = 
ConfigProvider.getConfig().getValue("quarkus.camel.dev-ui.update-interval", 
Duration.class);
+        return Multi.createFrom()
+                .ticks()
+                .every(interval)
+                .onOverflow().drop()
+                .map(tick -> callDevConsole(id, sanitized));
     }
 
     /**
@@ -419,68 +386,24 @@ public class CamelCoreDevUIService {
         return sanitized;
     }
 
-    ConsoleSubscription getOrCreateConsoleSubscription(String id, Map<String, 
Object> options) {
-        return PROCESSORS.computeIfAbsent(id, consoleId -> new 
ConsoleSubscription(consoleId, options));
-    }
-
-    /**
-     * Enables any Camel console to have its data streamed and updated within 
a specified period.
-     */
-    static final class ConsoleSubscription {
-        private final String id;
-        private final Map<String, Object> options;
-        private final Cancellable cancellable;
-        private final BroadcastProcessor<String> broadcaster;
-
-        ConsoleSubscription(String id, Map<String, Object> options) {
-            this.id = id;
-            this.options = new ConcurrentHashMap<>(options);
-            this.broadcaster = BroadcastProcessor.create();
-            this.cancellable = Multi.createFrom()
-                    .ticks()
-                    
.every(ConfigProvider.getConfig().getValue("quarkus.camel.dev-ui.update-interval",
 Duration.class))
-                    .subscribe()
-                    .with(this::broadcast);
-        }
-
-        void broadcast(Long invocationCount) {
-            broadcaster.onNext(callDevConsole());
-        }
-
-        void cancel() {
-            cancellable.cancel();
-        }
-
-        BroadcastProcessor<String> getBroadcaster() {
-            return broadcaster;
-        }
-
-        String callDevConsole() {
-            String result = "{}";
-            DevConsoleRegistry devConsoleRegistry = 
getContextPlugin(DevConsoleRegistry.class);
-            if (devConsoleRegistry != null) {
-                Optional<DevConsole> devConsoleOptional = 
devConsoleRegistry.getConsole(id);
-                if (devConsoleOptional.isPresent()) {
-                    DevConsole devConsole = devConsoleOptional.get();
-                    try {
-                        JsonObject json = (JsonObject) 
devConsole.call(DevConsole.MediaType.JSON, options);
-                        if (json != null) {
-                            result = json.toJson();
-                            LOG.debug(result);
-                        }
-                    } catch (Exception e) {
-                        LOG.debugf(e, "Error calling DevConsole %s", id);
+    private static String callDevConsole(String id, Map<String, Object> 
options) {
+        String result = "{}";
+        DevConsoleRegistry devConsoleRegistry = 
getContextPlugin(DevConsoleRegistry.class);
+        if (devConsoleRegistry != null) {
+            Optional<DevConsole> devConsoleOptional = 
devConsoleRegistry.getConsole(id);
+            if (devConsoleOptional.isPresent()) {
+                DevConsole devConsole = devConsoleOptional.get();
+                try {
+                    JsonObject json = (JsonObject) 
devConsole.call(DevConsole.MediaType.JSON, options);
+                    if (json != null) {
+                        result = json.toJson();
+                        LOG.debug(result);
                     }
+                } catch (Exception e) {
+                    LOG.debugf(e, "Error calling DevConsole %s", id);
                 }
             }
-            return result;
-        }
-
-        void setOptions(Map<String, Object> options) {
-            synchronized (this.options) {
-                this.options.clear();
-                this.options.putAll(options);
-            }
         }
+        return result;
     }
 }

Reply via email to