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 5751d7dc6b95 CAMEL-27776: camel-seda - retain QueueReference when 
queue holds messages on shutdown (#31130)
5751d7dc6b95 is described below

commit 5751d7dc6b95146ab6be28299e8cf80b1b0d46a3
Author: Guillaume Nodet <[email protected]>
AuthorDate: Mon Nov 30 22:13:36 2026 +0100

    CAMEL-27776: camel-seda - retain QueueReference when queue holds messages 
on shutdown (#31130)
    
    A message sent to a SEDA queue before its consumer route starts was lost 
when the producer stopped first:
    SedaComponent.onShutdownEndpoint() removed the QueueReference even when the 
queue still held messages, and the
    consumer then got a new, empty queue. The reference is now kept while the 
queue holds messages, so a consumer that
    starts later drains them. Tests cover a producer route and a 
ProducerTemplate, and the
    MasterRoutePolicyRouteLifecycleTest workaround is reverted. The 4.25 
upgrade guide describes the change.
    
    Hermes Agent (Claude Sonnet 4.6) on behalf of Guillaume Nodet.
    
    Co-authored-by: Claude Sonnet 4.6 <[email protected]>
---
 .../apache/camel/component/seda/SedaComponent.java |   6 +-
 .../MasterRoutePolicyRouteLifecycleTest.java       |   9 +-
 .../SedaMessageSentBeforeConsumerStartsTest.java   | 119 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc    |  16 +++
 4 files changed, 140 insertions(+), 10 deletions(-)

diff --git 
a/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaComponent.java
 
b/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaComponent.java
index 24d834de4c4c..837acbe0406e 100644
--- 
a/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaComponent.java
+++ 
b/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaComponent.java
@@ -328,8 +328,10 @@ public class SedaComponent extends DefaultComponent {
             if (ref != null && endpoint.getConsumers().isEmpty() && 
endpoint.getProducers().isEmpty()) {
                 // only remove the endpoint when both consumers and producers 
are removed
                 ref.removeReference(endpoint);
-                if (ref.getCount() <= 0 && !ref.hasConsumers() && 
!ref.hasProducers()) {
-                    // reference no longer needed so remove from queues
+                if (ref.getCount() <= 0 && !ref.hasConsumers() && 
!ref.hasProducers()
+                        && ref.getQueue().isEmpty()) {
+                    // reference no longer needed so remove from queues; keep 
it when the queue
+                    // still holds messages so a consumer that starts later 
can drain them
                     getQueues().remove(key);
                 }
             }
diff --git 
a/components/camel-zookeeper-master/src/test/java/org/apache/camel/component/zookeepermaster/policy/MasterRoutePolicyRouteLifecycleTest.java
 
b/components/camel-zookeeper-master/src/test/java/org/apache/camel/component/zookeepermaster/policy/MasterRoutePolicyRouteLifecycleTest.java
index 8bd7a90b4574..372743d8d645 100644
--- 
a/components/camel-zookeeper-master/src/test/java/org/apache/camel/component/zookeepermaster/policy/MasterRoutePolicyRouteLifecycleTest.java
+++ 
b/components/camel-zookeeper-master/src/test/java/org/apache/camel/component/zookeepermaster/policy/MasterRoutePolicyRouteLifecycleTest.java
@@ -30,7 +30,6 @@ import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.ProducerTemplate;
-import org.apache.camel.StatefulService;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.component.zookeepermaster.ZookeeperGroupSupport;
@@ -391,15 +390,9 @@ class MasterRoutePolicyRouteLifecycleTest {
         return ServiceHelper.isStarted(consumer);
     }
 
-    // a consumer that is starting (ServiceHelper.isStarted) may not be ready 
yet: the seda consumer replaces the stale
-    // queue of its endpoint when it starts, so a message sent before it has 
started may be lost
-    private static boolean isConsumerFullyStarted(CamelContext context) {
-        return context.getRoute(ROUTE).getConsumer() instanceof 
StatefulService consumer && consumer.isStarted();
-    }
-
     private static void awaitConsuming(CamelContext context) {
         await().atMost(30, TimeUnit.SECONDS).untilAsserted(
-                () -> assertTrue(policyOf(context).isMaster() && 
isConsumerFullyStarted(context),
+                () -> assertTrue(policyOf(context).isMaster() && 
isConsumerStarted(context),
                         "The node must become the master and start its 
consumer"));
     }
 
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/seda/SedaMessageSentBeforeConsumerStartsTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/seda/SedaMessageSentBeforeConsumerStartsTest.java
new file mode 100644
index 000000000000..a02ad0501c3a
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/seda/SedaMessageSentBeforeConsumerStartsTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.seda;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Regression test for CAMEL-27776: a message buffered in a seda queue before 
its consumer starts must not be lost when
+ * the producer route that sent it is stopped before the consumer starts.
+ * <p>
+ * Root cause: {@link SedaComponent#onShutdownEndpoint(SedaEndpoint)} released 
the {@link QueueReference} from the
+ * component's queue map even when the queue still held messages. When the 
consumer route started later,
+ * {@link SedaEndpoint#registerQueueIfStale()} found no reference in the map 
and created a brand-new, empty queue —
+ * silently discarding the buffered messages.
+ * <p>
+ * Fix: retain the {@link QueueReference} as long as {@link 
java.util.concurrent.BlockingQueue#isEmpty()} returns
+ * {@code false}.
+ */
+class SedaMessageSentBeforeConsumerStartsTest extends ContextTestSupport {
+
+    /**
+     * Producer route is started, sends a message to seda:queue, then is 
stopped — triggering
+     * {@link SedaComponent#onShutdownEndpoint} with no active consumer. The 
consumer route then starts and must receive
+     * the previously buffered message.
+     */
+    @Test
+    void testMessageBufferedByProducerRouteBeforeConsumerStartIsNotLost() 
throws Exception {
+        context.getRouteController().startRoute("producer");
+
+        template.sendBody("direct:start", "buffered-message");
+
+        // stop the producer route: onShutdownEndpoint fires for seda:queue 
with no active consumer;
+        // the bug would remove the QueueReference from the component map 
here, losing the message
+        context.getRouteController().stopRoute("producer");
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("buffered-message");
+
+        // with the bug, startRoute creates a fresh empty queue for seda:queue 
— message is lost
+        context.getRouteController().startRoute("consumer");
+
+        mock.assertIsSatisfied();
+    }
+
+    /**
+     * Same scenario with multiple messages buffered before the consumer 
starts.
+     */
+    @Test
+    void testMultipleMessagesBufferedBeforeConsumerStartAreNotLost() throws 
Exception {
+        context.getRouteController().startRoute("producer");
+
+        template.sendBody("direct:start", "first");
+        template.sendBody("direct:start", "second");
+        template.sendBody("direct:start", "third");
+
+        context.getRouteController().stopRoute("producer");
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("first", "second", "third");
+
+        context.getRouteController().startRoute("consumer");
+
+        mock.assertIsSatisfied();
+    }
+
+    /**
+     * ProducerTemplate variant: a message sent directly via {@link 
ProducerTemplate} to {@code seda:queue} and then the
+     * template is closed, triggering {@link SedaComponent#onShutdownEndpoint} 
with no active consumer. The consumer
+     * route then starts and must receive the buffered message. This is the 
path that
+     * {@code MasterRoutePolicyRouteLifecycleTest} exercises.
+     */
+    @Test
+    void testMessageSentViaProducerTemplateBeforeConsumerStartIsNotLost() 
throws Exception {
+        try (ProducerTemplate producerTemplate = 
context.createProducerTemplate()) {
+            producerTemplate.sendBody("seda:queue", "template-message");
+            // closing the template shuts down its producer for seda:queue, 
triggering onShutdownEndpoint
+        }
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("template-message");
+
+        // with the bug, startRoute creates a fresh empty queue — message is 
lost
+        context.getRouteController().startRoute("consumer");
+
+        mock.assertIsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                // producer route: feeds seda:queue — not started at context 
start
+                
from("direct:start").routeId("producer").autoStartup(false).to("seda:queue");
+
+                // consumer route: drains seda:queue — not started at context 
start
+                
from("seda:queue").routeId("consumer").autoStartup(false).to("mock:result");
+            }
+        };
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
index cf32bd6f167f..4f39a46b04c9 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
@@ -84,3 +84,19 @@ When `XMLSecurityDataFormat` is configured with an AES-GCM 
`xmlCipherAlgorithm`
 
 Routes that intentionally decrypt non-GCM payloads must explicitly configure a 
non-GCM `xmlCipherAlgorithm`
 (for example `AES_128` or `AES_256`) on the receiving `XMLSecurityDataFormat`.
+
+=== camel-seda - a non-empty queue is retained when its last producer stops
+
+When all producers of a SEDA endpoint stop, the endpoint's queue reference is 
now kept as long as it still holds
+messages — it is only removed when the queue is empty. Previously, the queue 
reference was always removed on the last
+producer stop, so a consumer that started afterwards received a fresh empty 
queue and lost the buffered messages.
+The empty-queue case is unaffected: a queue that holds no messages when its 
last producer stops is still removed as
+before.
+
+A retained queue is released once a consumer has drained it and its endpoint 
shuts down, or when the SEDA component
+(or the CamelContext) itself stops. While the queue holds undelivered 
messages, any attempt to create a new SEDA
+endpoint for the same key with different queue parameters (for example a 
different `size` or `multipleConsumers`
+setting) will raise an `IllegalArgumentException` ("Cannot use existing queue 
...") when its producer or consumer
+starts, the same error that was already raised when the endpoint was still 
active. Before this change, the released
+queue was silently replaced with a fresh one,
+which caused the buffered messages to be discarded.

Reply via email to