This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch fix/CAMEL-28117-netty-http-close-accepted-channels in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2c84946eb3f46ad4a7ec93fdda17d78c0557c606 Author: Claus Ibsen <[email protected]> AuthorDate: Sat Dec 5 08:16:29 2026 +0100 CAMEL-28117: camel-netty-http - close the accepted connections when the server stops The netty-http pipeline uses HttpServerMultiplexChannelHandler, which only forwards channelRead to the per-consumer handler, so the accepted channels were never added to the channel group that the server closes on stop. They were only closed because the server shuts down the worker group it created itself; with a configured workerGroup, kept-alive connections stayed open after the consumer stopped or was suspended. The multiplex handler now registers the channel with the server bootstrap factory in channelActive, as ServerChannelHandler does in camel-netty. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../HttpServerMultiplexChannelHandler.java | 33 +++++++ .../http/NettyHttpStopClosesConnectionsTest.java | 106 +++++++++++++++++++++ .../camel/component/netty/http/RawHttpClient.java | 18 ++++ 3 files changed, 157 insertions(+) diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java index 7fb34dee6db6..9acf851fca80 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java @@ -36,6 +36,7 @@ import io.netty.handler.codec.http.HttpResponse; import io.netty.util.Attribute; import io.netty.util.AttributeKey; import org.apache.camel.Exchange; +import org.apache.camel.component.netty.NettyServerBootstrapFactory; import org.apache.camel.component.netty.http.HttpServerConsumerChannelFactory; import org.apache.camel.component.netty.http.InboundStreamHttpRequest; import org.apache.camel.component.netty.http.NettyHttpConfiguration; @@ -114,6 +115,38 @@ public class HttpServerMultiplexChannelHandler extends SimpleChannelInboundHandl return this; } + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + // keep track of the accepted connection, so the server closes it when it stops, as ServerChannelHandler does in + // camel-netty. Otherwise the connection is only closed when the server shuts down a worker group it created + // itself, and a connection on a configured workerGroup stays open after the consumer has stopped + NettyServerBootstrapFactory factory = serverBootstrapFactory(); + if (factory != null) { + factory.addChannel(ctx.channel()); + } + super.channelActive(ctx); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + NettyServerBootstrapFactory factory = serverBootstrapFactory(); + if (factory != null) { + factory.removeChannel(ctx.channel()); + } + super.channelInactive(ctx); + } + + private NettyServerBootstrapFactory serverBootstrapFactory() { + // the consumers of this handler listen on the same address, so they share the same server bootstrap factory + for (HttpServerChannelHandler handler : consumers) { + NettyServerBootstrapFactory factory = handler.getConsumer().getNettyServerBootstrapFactory(); + if (factory != null) { + return factory; + } + } + return null; + } + @Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { // store request, as this channel handler is created per pipeline diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpStopClosesConnectionsTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpStopClosesConnectionsTest.java new file mode 100644 index 000000000000..f9ab5e913abd --- /dev/null +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpStopClosesConnectionsTest.java @@ -0,0 +1,106 @@ +/* + * 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.http; + +import java.util.concurrent.TimeUnit; + +import io.netty.channel.EventLoopGroup; +import org.apache.camel.BindToRegistry; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.netty.NettyWorkerPoolBuilder; +import org.apache.camel.test.AvailablePortFinder; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import static org.apache.camel.component.netty.http.RawHttpClient.get; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * The server closes the connections it accepted when it stops, also when the worker event loop group is configured, + * which the server does not shut down (CAMEL-28117). + */ +public class NettyHttpStopClosesConnectionsTest extends BaseNettyTestSupport { + + private static final int CLOSE_TIMEOUT = 20000; + + @RegisterExtension + AvailablePortFinder.Port port2 = AvailablePortFinder.find(); + + @BindToRegistry("serverWorkers") + private final EventLoopGroup serverWorkers + = new NettyWorkerPoolBuilder().withWorkerCount(2).withName("NettyHttpServer").build(); + + @AfterEach + public void shutdownWorkers() throws Exception { + // the server does not shut down a configured worker group, so stop the routes first, then the group + context.stop(); + serverWorkers.shutdownGracefully(0, 5, TimeUnit.SECONDS).await(10, TimeUnit.SECONDS); + } + + @Test + public void testStopClosesKeptAliveConnectionOnceTheLastRouteOfThePortStops() throws Exception { + try (RawHttpClient client = new RawHttpClient(getPort())) { + assertEquals("Bye foo", client.send(get("/foo")).body()); + + // another route on the same port stops: the server keeps running, and so does the connection + context.getRouteController().stopRoute("other"); + assertEquals("Bye foo", client.send(get("/foo")).body()); + + // the last route on the port stops: the server stops and closes the connection + context.getRouteController().stopRoute("foo"); + assertTrue(client.awaitClose(CLOSE_TIMEOUT), "The kept-alive connection should be closed when the server stops"); + } + } + + @Test + public void testSuspendClosesKeptAliveConnection() throws Exception { + try (RawHttpClient client = new RawHttpClient(port2.getPort())) { + assertEquals("Bye bar", client.send(get("/bar")).body()); + + context.getRouteController().suspendRoute("bar"); + assertTrue(client.awaitClose(CLOSE_TIMEOUT), + "The kept-alive connection should be closed when the consumer is suspended"); + } + + context.getRouteController().resumeRoute("bar"); + try (RawHttpClient client = new RawHttpClient(port2.getPort())) { + assertEquals("Bye bar", client.send(get("/bar")).body()); + assertFalse(client.awaitClose(100), "The connection should be kept alive while the route runs"); + } + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("netty-http:http://localhost:" + getPort() + "/foo?workerGroup=#serverWorkers").routeId("foo") + .setBody(constant("Bye foo")); + + from("netty-http:http://localhost:" + getPort() + "/other?workerGroup=#serverWorkers").routeId("other") + .setBody(constant("Bye other")); + + from("netty-http:http://localhost:" + port2.getPort() + + "/bar?workerGroup=#serverWorkers&send503whenSuspended=false").routeId("bar") + .setBody(constant("Bye bar")); + } + }; + } +} diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/RawHttpClient.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/RawHttpClient.java index 667bce27045e..c564354f5654 100644 --- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/RawHttpClient.java +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/RawHttpClient.java @@ -79,6 +79,24 @@ final class RawHttpClient implements AutoCloseable { } } + /** + * Reads until the connection is closed. Returns false when the connection is not closed within the timeout. + */ + boolean awaitClose(int timeoutMillis) throws IOException { + socket.setSoTimeout(timeoutMillis); + try { + in.readAllBytes(); + return true; + } catch (SocketTimeoutException e) { + return false; + } catch (SocketException e) { + // the connection was reset + return true; + } finally { + socket.setSoTimeout(TIMEOUT); + } + } + static Response send(int port, String request) throws IOException { try (RawHttpClient client = new RawHttpClient(port)) { return client.send(request);
