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 26cb307402c3 CAMEL-27213: camel-seda - a discarded request-reply
message fails at once (#30461)
26cb307402c3 is described below
commit 26cb307402c3639b175888b766a278fe621a5a7e
Author: allthingssecurity <[email protected]>
AuthorDate: Fri Nov 20 13:02:37 2026 +0530
CAMEL-27213: camel-seda - a discarded request-reply message fails at once
(#30461)
When the producer waits for the reply (InOut, or
waitForTaskToComplete=Always) and the
copy it waits for is discarded (discardIfNoConsumers without consumer,
discardWhenFull
with a full queue), nothing released the producer: it waited until the
timeout, or for
ever with timeout=0.
The discarded copy now gets a RejectedExecutionException before its on
completions run,
as SedaEndpoint.purgeQueue does for the exchanges it discards, so the
caller fails at
once with that exception. A discarded InOnly message is still dropped
without an error.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../apache/camel/component/seda/SedaProducer.java | 18 ++++
.../camel/component/seda/SedaInOutDiscardTest.java | 110 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 9 ++
3 files changed, 137 insertions(+)
diff --git
a/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaProducer.java
b/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaProducer.java
index 10f6898950d7..30da532042d4 100644
---
a/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaProducer.java
+++
b/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaProducer.java
@@ -33,6 +33,7 @@ import org.apache.camel.WaitForTaskToComplete;
import org.apache.camel.support.DefaultAsyncProducer;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.support.SynchronizationAdapter;
+import org.apache.camel.support.UnitOfWorkHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -246,6 +247,9 @@ public class SedaProducer extends DefaultAsyncProducer {
throw new SedaConsumerNotAvailableException("No consumers
available on endpoint: " + endpoint, exchange);
} else if (endpoint.isDiscardIfNoConsumers()) {
LOG.debug("Discard message as no active consumers on endpoint:
{}", endpoint);
+ if (!copy) {
+ discardedWhileWaiting(exchange, "Exchange discarded as the
SEDA queue has no consumers");
+ }
return;
}
}
@@ -280,6 +284,20 @@ public class SedaProducer extends DefaultAsyncProducer {
target.getExchangeExtension().handoverCompletions(exchange);
}
}
+ if (!added && !copy) {
+ // discarded as the queue is full
+ discardedWhileWaiting(target, "Exchange discarded as the SEDA
queue is full");
+ }
+ }
+
+ /**
+ * The producer waits for the reply of the given copy, which is discarded:
the copy fails and its on completions are
+ * run (as {@link SedaEndpoint#purgeQueue()} does), so the producer fails
at once, instead of waiting for a reply
+ * that never comes, until the timeout, or for ever when there is no
timeout.
+ */
+ private static void discardedWhileWaiting(Exchange copy, String reason) {
+ copy.setException(new RejectedExecutionException(reason));
+ UnitOfWorkHelper.doneSynchronizations(copy,
copy.getExchangeExtension().handoverCompletions());
}
/**
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/seda/SedaInOutDiscardTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/seda/SedaInOutDiscardTest.java
new file mode 100644
index 000000000000..de29a501f088
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/component/seda/SedaInOutDiscardTest.java
@@ -0,0 +1,110 @@
+/*
+ * 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 java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Future;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A request-reply message that the seda producer discards fails at once with
a RejectedExecutionException, as a message
+ * discarded by SedaEndpoint.purgeQueue: the producer does not wait for a
reply that never comes. A discarded InOnly
+ * message is still dropped without failure.
+ */
+class SedaInOutDiscardTest extends ContextTestSupport {
+
+ // a timeout longer than the test waits: the request must not end with the
timeout
+ private static final String FULL =
"seda:full?size=1&discardWhenFull=true&timeout=20000";
+
+ private final CountDownLatch taken = new CountDownLatch(1);
+ private final CountDownLatch release = new CountDownLatch(1);
+
+ @AfterEach
+ void releaseConsumer() {
+ release.countDown();
+ }
+
+ @Test
+ void testDiscardIfNoConsumers() throws Exception {
+ String uri = "seda:none?discardIfNoConsumers=true&timeout=20000";
+
+ Exchange reply = request(uri, "Hello World").get(5, TimeUnit.SECONDS);
+ assertRejected("Exchange discarded as the SEDA queue has no
consumers", reply);
+
+ // InOnly is not changed: the message is dropped, the exchange does
not fail
+ Exchange inOnly = template.send(uri, e -> e.getMessage().setBody("Bye
World"));
+ assertNull(inOnly.getException());
+ }
+
+ @Test
+ void testDiscardWhenFull() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:full");
+ mock.expectedBodiesReceived("A", "B");
+
+ // A is taken by the consumer, which waits, and B fills the queue
+ template.sendBody(FULL, "A");
+ assertTrue(taken.await(5, TimeUnit.SECONDS));
+ template.sendBody(FULL, "B");
+
+ // the queue is full, so the request is discarded
+ Exchange reply = request(FULL, "C").get(5, TimeUnit.SECONDS);
+ assertRejected("Exchange discarded as the SEDA queue is full", reply);
+
+ release.countDown();
+ assertMockEndpointsSatisfied();
+ }
+
+ private Future<Exchange> request(String uri, String body) {
+ return template.asyncSend(uri, e -> {
+ e.setPattern(ExchangePattern.InOut);
+ e.getMessage().setBody(body);
+ });
+ }
+
+ private static void assertRejected(String message, Exchange reply) {
+ RejectedExecutionException e =
assertIsInstanceOf(RejectedExecutionException.class, reply.getException());
+ assertEquals(message, e.getMessage());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from(FULL)
+ .process(e -> {
+ taken.countDown();
+ release.await(10, TimeUnit.SECONDS);
+ })
+ .to("mock:full");
+ }
+ };
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 7b9632631797..87edb8af2e57 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -277,6 +277,15 @@ a route template now has its own copy of the expression,
so a template parameter
route created from the template. Route dumps and the JMX attributes of the
aggregator (`CompletionSizeExpression`,
`CompletionSizeExpressionLanguage` and the others) now show the language and
the expression.
+=== camel-seda - a discarded request-reply message fails at once
+
+When the SEDA producer waits for the reply (`InOut` messages, or
`waitForTaskToComplete=Always`) and discards the
+message, because of `discardIfNoConsumers` with no consumer, or
`discardWhenFull` with a full queue, the exchange now
+fails at once with a `java.util.concurrent.RejectedExecutionException` (the
same exception as for a message discarded
+when the queue is purged). Before, the producer waited for a reply that never
came: until the `timeout` (30 seconds by
+default), and then failed with an `ExchangeTimedOutException`, or for ever
when the `timeout` was 0 or less. A discarded
+`InOnly` message is still dropped without an error.
+
=== camel-ehcache - shared CacheManager and changed defaults
The following user-visible changes take effect in 4.24: