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 3dd104b60a51 CAMEL-27354: camel-netty - with reuseChannel a failed
exchange gives its channel back to the producer pool (#30696)
3dd104b60a51 is described below
commit 3dd104b60a51a94347df62f6c7d13bcc3588b87f
Author: allthingssecurity <[email protected]>
AuthorDate: Tue Nov 24 12:15:17 2026 +0530
CAMEL-27354: camel-netty - with reuseChannel a failed exchange gives its
channel back to the producer pool (#30696)
With reuseChannel=true the channel is given back to the producer pool by
an on-completion of the exchange, which only overrode onComplete. A
failed exchange (a request that timed out, or a failure later in the
route) never gave its channel back, so a bounded pool ran out of
channels and the next requests failed with "Timeout waiting for idle
object".
The on-completion now overrides onDone, so it runs for a failed exchange
too; releaseChannel invalidates a channel that was closed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../camel/component/netty/NettyProducer.java | 5 +-
.../netty/NettyReuseChannelFailedExchangeTest.java | 114 +++++++++++++++++++++
2 files changed, 117 insertions(+), 2 deletions(-)
diff --git
a/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
b/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
index fc45bdd5ef66..86caccb10c62 100644
---
a/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
+++
b/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
@@ -298,10 +298,11 @@ public class NettyProducer extends DefaultAsyncProducer {
// for use when sending subsequent messages reusing this channel
channel.attr(CORRELATION_MANAGER_ATTR).set(correlationManager);
exchange.setProperty(NettyConstants.NETTY_CHANNEL, channel);
- // and defer closing the channel until we are done routing the
exchange
+ // and defer closing the channel until we are done routing the
exchange, also when the exchange failed,
+ // or the channel is never given back to the producer pool
exchange.getExchangeExtension().addOnCompletion(new
SynchronizationAdapter() {
@Override
- public void onComplete(Exchange exchange) {
+ public void onDone(Exchange exchange) {
// should channel be closed after complete?
Boolean close
=
exchange.getMessage().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE,
Boolean.class);
diff --git
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyReuseChannelFailedExchangeTest.java
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyReuseChannelFailedExchangeTest.java
new file mode 100644
index 000000000000..a9d3743f089d
--- /dev/null
+++
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyReuseChannelFailedExchangeTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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.netty;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import io.netty.handler.timeout.ReadTimeoutException;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+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.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * With reuseChannel=true the channel is given back to the producer pool when
the exchange is done: also when the
+ * exchange failed, or a bounded pool runs out of channels.
+ */
+class NettyReuseChannelFailedExchangeTest extends BaseNettyTest {
+
+ private static final String PRODUCER_OPTIONS
+ =
"textline=true&sync=true&reuseChannel=true&producerPoolMaxTotal=1&producerPoolMaxWait=2000";
+
+ private final CountDownLatch releaseSlowReply = new CountDownLatch(1);
+
+ @AfterEach
+ void releaseServer() {
+ releaseSlowReply.countDown();
+ }
+
+ @Test
+ void testFailedExchangeGivesBackItsChannel() {
+ // the reply is received, and then the exchange fails further on in
the route
+ Exchange failed = template.request("direct:start", e -> {
+ e.getMessage().setBody("World");
+ e.getMessage().setHeader("fail", true);
+ });
+ assertInstanceOf(IllegalStateException.class, failed.getException());
+
+ Exchange next = template.request("direct:start", e ->
e.getMessage().setBody("World"));
+ assertNull(next.getException(), "The next exchange should get a
channel from the pool");
+ assertEquals("Hello World", next.getMessage().getBody(String.class));
+ }
+
+ @Test
+ void testTimedOutRequestGivesBackItsChannel() {
+ // the request times out, which closes its channel: the closed channel
must be removed from the pool
+ Exchange failed = template.request("direct:timeout", e ->
e.getMessage().setBody("slow"));
+ assertInstanceOf(ReadTimeoutException.class, failed.getException());
+
+ Exchange next = template.request("direct:timeout", e ->
e.getMessage().setBody("World"));
+ assertNull(next.getException(), "The next exchange should get a
channel from the pool");
+ assertEquals("Hello World", next.getMessage().getBody(String.class));
+ }
+
+ @Test
+ void testRequestFailingWhileSetUpGivesBackItsChannel() {
+ // the request fails on its channel before it is sent (an invalid
request timeout header)
+ Exchange failed = template.request("direct:start", e -> {
+ e.getMessage().setBody("World");
+ e.getMessage().setHeader(NettyConstants.NETTY_REQUEST_TIMEOUT,
"not a number");
+ });
+ assertNotNull(failed.getException(), "The request with an invalid
timeout should fail");
+
+ Exchange next = template.request("direct:start", e ->
e.getMessage().setBody("World"));
+ assertNull(next.getException(), "The next exchange should get a
channel from the pool");
+ assertEquals("Hello World", next.getMessage().getBody(String.class));
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("netty:tcp://localhost:{{port}}?textline=true&sync=true")
+ .process(e -> {
+ if
("slow".equals(e.getMessage().getBody(String.class))) {
+ releaseSlowReply.await(5, TimeUnit.SECONDS);
+ }
+ })
+ .transform(body().prepend("Hello "));
+
+ from("direct:start")
+ .to("netty:tcp://localhost:{{port}}?" +
PRODUCER_OPTIONS)
+ .process(e -> {
+ if (e.getMessage().getHeader("fail", false,
Boolean.class)) {
+ throw new IllegalStateException("Forced");
+ }
+ });
+
+ from("direct:timeout")
+ .to("netty:tcp://localhost:{{port}}?" +
PRODUCER_OPTIONS + "&requestTimeout=500");
+ }
+ };
+ }
+}