This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch CAMEL-27507-vertx-websocket-event-loop
in repository https://gitbox.apache.org/repos/asf/camel.git

commit a1c87a0773091afaf522b220c10921d491267d0f
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Nov 24 09:49:52 2026 +0100

    CAMEL-27507: camel-vertx-websocket - the route continues on a Vert.x worker 
thread after a send
    
    A send completes on the Vert.x event loop, and the route continued there. A 
later vertx-websocket producer that
    connects to another server then blocked that event loop while waiting for a 
connect that needs the same event loop,
    so the route hung until the connect timeout. Any other blocking step after 
the producer stalled the WebSocket I/O
    of that event loop.
    
    The producer now completes the exchange from the Vert.x worker pool when 
the write completes on an event loop, as
    vertx-http does; when the worker pool is already shut down, it completes on 
the current thread.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../catalog/docs/vertx-websocket-component.adoc    |  4 ++
 .../src/main/docs/vertx-websocket-component.adoc   |  4 ++
 .../vertx/websocket/VertxWebsocketProducer.java    |  2 +-
 .../websocket/VertxWebsocketResultHandler.java     | 49 ++++++++++++--
 .../VertxWebsocketProducerEventLoopTest.java       | 78 ++++++++++++++++++++++
 5 files changed, 130 insertions(+), 7 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/vertx-websocket-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/vertx-websocket-component.adoc
index 7d321ee2269c..477be310f3bd 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/vertx-websocket-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/vertx-websocket-component.adoc
@@ -232,6 +232,10 @@ YAML::
 ----
 ====
 
+A message the producer sends completes on a Vert.x event loop thread. The 
route then continues on a thread of the
+Vert.x worker pool, so a step after the producer may block (such as an HTTP 
call, or another `vertx-websocket` producer
+that connects to a server) without stalling the WebSocket I/O of that event 
loop.
+
 === Sending messages to peers connected to the vertx-websocket server consumer
 
 NOTE: This section only applies when producing messages to a WebSocket hosted 
by the camel-vertx-websocket consumer.
diff --git 
a/components/camel-vertx/camel-vertx-websocket/src/main/docs/vertx-websocket-component.adoc
 
b/components/camel-vertx/camel-vertx-websocket/src/main/docs/vertx-websocket-component.adoc
index 7d321ee2269c..477be310f3bd 100644
--- 
a/components/camel-vertx/camel-vertx-websocket/src/main/docs/vertx-websocket-component.adoc
+++ 
b/components/camel-vertx/camel-vertx-websocket/src/main/docs/vertx-websocket-component.adoc
@@ -232,6 +232,10 @@ YAML::
 ----
 ====
 
+A message the producer sends completes on a Vert.x event loop thread. The 
route then continues on a thread of the
+Vert.x worker pool, so a step after the producer may block (such as an HTTP 
call, or another `vertx-websocket` producer
+that connects to a server) without stalling the WebSocket I/O of that event 
loop.
+
 === Sending messages to peers connected to the vertx-websocket server consumer
 
 NOTE: This section only applies when producing messages to a WebSocket hosted 
by the camel-vertx-websocket consumer.
diff --git 
a/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketProducer.java
 
b/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketProducer.java
index a1475f84e46a..ce68d6352c5d 100644
--- 
a/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketProducer.java
+++ 
b/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketProducer.java
@@ -72,7 +72,7 @@ public class VertxWebsocketProducer extends 
DefaultAsyncProducer {
             }
 
             VertxWebsocketResultHandler vertxWebsocketResultHandler
-                    = new VertxWebsocketResultHandler(exchange, callback, 
connectedPeers.keySet());
+                    = new 
VertxWebsocketResultHandler(getEndpoint().getVertx(), exchange, callback, 
connectedPeers.keySet());
 
             // Send message to each peer then record and process the results 
asynchronously
             connectedPeers.forEach((connectionKey, webSocket) -> {
diff --git 
a/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketResultHandler.java
 
b/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketResultHandler.java
index c5c6115daac3..43350b4102d3 100644
--- 
a/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketResultHandler.java
+++ 
b/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketResultHandler.java
@@ -20,9 +20,14 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.Callable;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
+import io.vertx.core.Context;
+import io.vertx.core.Vertx;
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.CamelExecutionException;
 import org.apache.camel.Exchange;
@@ -31,28 +36,32 @@ import org.apache.camel.Exchange;
  * Handles the result of one or more asynchronous WebSocket write operations
  */
 class VertxWebsocketResultHandler {
+    private final Vertx vertx;
     private final Exchange exchange;
     private final AsyncCallback callback;
     private final Set<String> connectionKeys;
     private final Map<String, Throwable> errors = new HashMap<>();
     private final Lock lock = new ReentrantLock();
 
-    VertxWebsocketResultHandler(Exchange exchange, AsyncCallback callback, 
Set<String> connectionKeys) {
+    VertxWebsocketResultHandler(Vertx vertx, Exchange exchange, AsyncCallback 
callback, Set<String> connectionKeys) {
+        this.vertx = vertx;
         this.exchange = exchange;
         this.callback = callback;
         this.connectionKeys = new HashSet<>(connectionKeys);
     }
 
     void onResult(String connectionKey) {
+        boolean complete;
         lock.lock();
         try {
-            connectionKeys.remove(connectionKey);
-            if (connectionKeys.isEmpty()) {
-                onComplete();
-            }
+            // a key is removed once, so only the last result completes the 
exchange
+            complete = connectionKeys.remove(connectionKey) && 
connectionKeys.isEmpty();
         } finally {
             lock.unlock();
         }
+        if (complete) {
+            onComplete();
+        }
     }
 
     void onError(String connectionKey, Throwable cause) {
@@ -81,6 +90,34 @@ class VertxWebsocketResultHandler {
                 exchange.setException(new 
CamelExecutionException(msg.toString(), exchange));
             }
         }
-        callback.done(false);
+        if (Context.isOnEventLoopThread()) {
+            // a write completes on the Vert.x event loop: the route must not 
continue there, as a blocking step
+            // stalls the I/O of that event loop, and a producer that connects 
to another server waits for a connect
+            // that needs this very event loop (CAMEL-27507)
+            completeOnWorkerThread();
+        } else {
+            callback.done(false);
+        }
+    }
+
+    private void completeOnWorkerThread() {
+        // the exchange is completed once: by the task, or here when the 
worker pool refuses the task
+        AtomicBoolean completed = new AtomicBoolean();
+        try {
+            vertx.executeBlocking((Callable<Void>) () -> {
+                complete(completed);
+                return null;
+            }, false).onFailure(e -> complete(completed));
+        } catch (RejectedExecutionException e) {
+            // the worker pool of the Vert.x is already shut down (for example 
while the Vert.x closes): the message
+            // was sent, so complete the exchange on this thread rather than 
lose it
+            complete(completed);
+        }
+    }
+
+    private void complete(AtomicBoolean completed) {
+        if (completed.compareAndSet(false, true)) {
+            callback.done(false);
+        }
     }
 }
diff --git 
a/components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketProducerEventLoopTest.java
 
b/components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketProducerEventLoopTest.java
new file mode 100644
index 000000000000..1dbad2feaa91
--- /dev/null
+++ 
b/components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketProducerEventLoopTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.vertx.websocket;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import io.vertx.core.Context;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+/**
+ * A send to a remote WebSocket server completes on the Vert.x event loop. The 
route must continue on another thread: on
+ * the event loop, a second producer that connects to another server waits for 
a connect that needs that very event
+ * loop, and the route hangs until the connect timeout (CAMEL-27507).
+ */
+public class VertxWebsocketProducerEventLoopTest extends 
VertxWebSocketTestSupport {
+
+    private final AtomicReference<String> threadAfterSend = new 
AtomicReference<>();
+    private final AtomicReference<Boolean> onEventLoopAfterSend = new 
AtomicReference<>();
+
+    @Test
+    public void testRouteContinuesOffTheEventLoopAfterASend() throws Exception 
{
+        MockEndpoint first = getMockEndpoint("mock:first");
+        first.expectedBodiesReceived("Hello");
+        MockEndpoint second = getMockEndpoint("mock:second");
+        second.expectedBodiesReceived("Hello");
+
+        template.sendBody("direct:start", "Hello");
+
+        // the second producer connects to another server after the first 
send: it used to block the event loop
+        // until the connect timeout, and the message never arrived
+        MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS);
+        assertFalse(onEventLoopAfterSend.get(),
+                "The route continued on the Vert.x event loop after the send: 
" + threadAfterSend.get());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                fromF("vertx-websocket:localhost:%d/first", port.getPort())
+                        .to("mock:first");
+
+                fromF("vertx-websocket:localhost:%d/second", port.getPort())
+                        .to("mock:second");
+
+                // two producers to different URIs: each has its own endpoint, 
so the second one opens a new connection
+                from("direct:start")
+                        .toF("vertx-websocket:localhost:%d/first", 
port.getPort())
+                        .process(exchange -> {
+                            
threadAfterSend.set(Thread.currentThread().getName());
+                            
onEventLoopAfterSend.set(Context.isOnEventLoopThread());
+                        })
+                        .toF("vertx-websocket:localhost:%d/second", 
port.getPort());
+
+            }
+        };
+    }
+}

Reply via email to