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


The following commit(s) were added to refs/heads/main by this push:
     new 1dbce115a82f CAMEL-26303: camel-browse - stopping or removing the 
route that consumes from a browse endpoint no longer breaks the routes that 
send to it (#28759)
1dbce115a82f is described below

commit 1dbce115a82fa9369cb4d7fcc8c647cfaba8b771
Author: allthingssecurity <[email protected]>
AuthorDate: Sun Nov 1 00:46:47 2026 +0530

    CAMEL-26303: camel-browse - stopping or removing the route that consumes 
from a browse endpoint no longer breaks the routes that send to it (#28759)
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/component/browse/BrowseEndpoint.java     |  61 +++++++---
 .../component/browse/BrowseRemoveRouteTest.java    | 123 +++++++++++++++++++++
 2 files changed, 170 insertions(+), 14 deletions(-)

diff --git 
a/components/camel-browse/src/main/java/org/apache/camel/component/browse/BrowseEndpoint.java
 
b/components/camel-browse/src/main/java/org/apache/camel/component/browse/BrowseEndpoint.java
index d76af504e63e..e3383541cbd1 100644
--- 
a/components/camel-browse/src/main/java/org/apache/camel/component/browse/BrowseEndpoint.java
+++ 
b/components/camel-browse/src/main/java/org/apache/camel/component/browse/BrowseEndpoint.java
@@ -68,7 +68,8 @@ public class BrowseEndpoint extends DefaultEndpoint 
implements BrowsableEndpoint
     private Predicate<Exchange> filter;
 
     private final Lock limitLock = new ReentrantLock();
-    private List<Exchange> exchanges;
+    // volatile: doInit or getExchanges can set the list while producer 
threads add to it
+    private volatile List<Exchange> exchanges;
     private volatile Processor onExchangeProcessor;
 
     public BrowseEndpoint() {
@@ -118,9 +119,7 @@ public class BrowseEndpoint extends DefaultEndpoint 
implements BrowsableEndpoint
 
     @Override
     public Consumer createConsumer(Processor processor) throws Exception {
-        this.onExchangeProcessor = processor;
-
-        Consumer answer = new DefaultConsumer(this, processor);
+        Consumer answer = new BrowseConsumer(processor);
         configureConsumer(answer);
         return answer;
     }
@@ -152,41 +151,75 @@ public class BrowseEndpoint extends DefaultEndpoint 
implements BrowsableEndpoint
      * @throws Exception is thrown if failed to process the exchange
      */
     protected void onExchange(Exchange exchange) throws Exception {
+        List<Exchange> list = exchanges;
         if (browseLimit > 0) {
             // add first and then remove the oldest from the list itself: a 
sub list is only a view that keeps them
             // all in memory, and a reader never sees fewer than the limit 
once it was reached. Under a lock, as
             // concurrent producers would otherwise remove too many or 
overtake the limit
             limitLock.lock();
             try {
-                exchanges.add(exchange);
-                int excess = exchanges.size() - browseLimit;
+                list.add(exchange);
+                int excess = list.size() - browseLimit;
                 if (excess > 0) {
-                    exchanges.subList(0, excess).clear();
+                    list.subList(0, excess).clear();
                 }
             } finally {
                 limitLock.unlock();
             }
         } else {
-            exchanges.add(exchange);
+            list.add(exchange);
         }
 
         // now fire the consumer
-        if (onExchangeProcessor != null) {
-            onExchangeProcessor.process(exchange);
+        Processor processor = onExchangeProcessor;
+        if (processor != null) {
+            processor.process(exchange);
+        }
+    }
+
+    /**
+     * The route of the consumer gets the exchanges only while the consumer is 
started: a stopped or removed route no
+     * longer gets them, and the producers that send to this endpoint keep 
working.
+     */
+    private final class BrowseConsumer extends DefaultConsumer {
+
+        BrowseConsumer(Processor processor) {
+            super(BrowseEndpoint.this, processor);
+        }
+
+        @Override
+        protected void doStart() throws Exception {
+            super.doStart();
+            onExchangeProcessor = getProcessor();
+        }
+
+        @Override
+        protected void doStop() throws Exception {
+            if (onExchangeProcessor == getProcessor()) {
+                onExchangeProcessor = null;
+            }
+            super.doStop();
         }
     }
 
     @Override
     protected void doInit() throws Exception {
         super.doInit();
-        exchanges = createExchangeList();
+        // the endpoint is initialized again when it is started after a stop, 
such as when the route that consumes from
+        // it is added again: keep the exchanges the routes that send to it 
browsed meanwhile
+        if (exchanges == null) {
+            exchanges = createExchangeList();
+        }
     }
 
     @Override
     protected void doStop() throws Exception {
-        if (exchanges != null) {
-            exchanges.clear();
-            exchanges = null;
+        // the endpoint is also stopped when the route that consumes from it 
is removed (such as on a reload in dev
+        // mode), while the routes that send to it keep sending: their 
exchanges stay available for browsing, and are
+        // only cleared when CamelContext is stopping
+        List<Exchange> list = exchanges;
+        if (list != null && getCamelContext().isStopping()) {
+            list.clear();
         }
         super.doStop();
     }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/browse/BrowseRemoveRouteTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/browse/BrowseRemoveRouteTest.java
new file mode 100644
index 000000000000..cdf768cac3fd
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/browse/BrowseRemoveRouteTest.java
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.browse;
+
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.BrowsableEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * The route that sends to a browse endpoint keeps working when the route that 
consumes from it is stopped or removed,
+ * and a stopped consumer gets no message. The browsed exchanges are kept 
until CamelContext is stopping.
+ */
+class BrowseRemoveRouteTest extends ContextTestSupport {
+
+    @Test
+    void removeConsumerRoute() throws Exception {
+        MockEndpoint consumed = getMockEndpoint("mock:consumed");
+        consumed.expectedBodiesReceived("a");
+        template.sendBody("direct:start", "a");
+        assertMockEndpointsSatisfied();
+
+        context.getRouteController().stopRoute("consumer");
+        context.removeRoute("consumer");
+
+        MockEndpoint result = getMockEndpoint("mock:result");
+        result.reset();
+        result.expectedBodiesReceived("b");
+        template.sendBody("direct:start", "b");
+        assertMockEndpointsSatisfied();
+
+        List<Exchange> exchanges = context.getEndpoint("browse:orders", 
BrowsableEndpoint.class).getExchanges();
+        assertEquals("b", exchanges.get(exchanges.size() - 
1).getMessage().getBody());
+    }
+
+    @Test
+    void removeAndAddConsumerRouteKeepsTheBrowsedExchanges() throws Exception {
+        template.sendBody("direct:start", "a");
+        context.getRouteController().stopRoute("consumer");
+        context.removeRoute("consumer");
+        template.sendBody("direct:start", "b");
+        assertEquals(List.of("a", "b"), browsedBodies());
+
+        // such as a reload in dev mode
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                from("browse:orders").routeId("consumer").to("mock:consumed");
+            }
+        });
+        MockEndpoint consumed = getMockEndpoint("mock:consumed");
+        consumed.reset();
+        consumed.expectedBodiesReceived("c");
+        template.sendBody("direct:start", "c");
+        consumed.assertIsSatisfied();
+        assertEquals(List.of("a", "b", "c"), browsedBodies());
+    }
+
+    @Test
+    void stoppingCamelContextClearsTheBrowsedExchanges() {
+        template.sendBody("direct:start", "a");
+        BrowsableEndpoint endpoint = context.getEndpoint("browse:orders", 
BrowsableEndpoint.class);
+        assertEquals(1, endpoint.getExchanges().size());
+
+        context.stop();
+
+        assertEquals(0, endpoint.getExchanges().size());
+    }
+
+    @Test
+    void stoppedConsumerGetsNoMessage() throws Exception {
+        context.getRouteController().stopRoute("consumer");
+
+        MockEndpoint consumed = getMockEndpoint("mock:consumed");
+        consumed.expectedMessageCount(0);
+        MockEndpoint result = getMockEndpoint("mock:result");
+        result.expectedBodiesReceived("a");
+        template.sendBody("direct:start", "a");
+        assertMockEndpointsSatisfied();
+
+        context.getRouteController().startRoute("consumer");
+        consumed.reset();
+        consumed.expectedBodiesReceived("b");
+        template.sendBody("direct:start", "b");
+        consumed.assertIsSatisfied();
+    }
+
+    private List<Object> browsedBodies() {
+        return context.getEndpoint("browse:orders", 
BrowsableEndpoint.class).getExchanges().stream()
+                .map(e -> e.getMessage().getBody())
+                .toList();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start").to("browse:orders").to("mock:result");
+
+                from("browse:orders").routeId("consumer").to("mock:consumed");
+            }
+        };
+    }
+}

Reply via email to