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

apupier pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 417b61c9d6f569b1a9d855a249d61ae4cc36a550
Author: AurĂ©lien Pupier <[email protected]>
AuthorDate: Mon Nov 30 11:50:34 2026 +0100

    CAMEL-27774: camel-rxjava - Fix lost completion of to() requests too
    
    RxJavaStreamsService.doRequest() has the same pattern as the reactor one:
    Single.create(...).doOnSubscribe(... consumer.process(...)). RxJava also
    runs doOnSubscribe before the create callback, so the exchange could be
    completed by the consumer thread pool before its completion callback was
    registered, and the Single never emitted.
    
    Register the completion callback and process the exchange inside the
    Single.create callback, and add the same regression test as camel-reactor
    (it timed out after 30 seconds without the fix).
    
    Also clarify the comment in the camel-reactor fix.
    
    Found thanks to the flaky test ReactorStreamsServiceTest, see CAMEL-26778.
    
    Co-authored-by: IBM Bob <[email protected]>
---
 .../reactor/engine/ReactorStreamsService.java      |  4 +--
 .../rxjava/engine/RxJavaStreamsService.java        | 34 ++++++++++++----------
 .../rxjava/engine/RxJavaStreamsServiceTest.java    | 15 ++++++++++
 3 files changed, 36 insertions(+), 17 deletions(-)

diff --git 
a/components/camel-reactor/src/main/java/org/apache/camel/component/reactor/engine/ReactorStreamsService.java
 
b/components/camel-reactor/src/main/java/org/apache/camel/component/reactor/engine/ReactorStreamsService.java
index 05427c04e645..f081e2994863 100644
--- 
a/components/camel-reactor/src/main/java/org/apache/camel/component/reactor/engine/ReactorStreamsService.java
+++ 
b/components/camel-reactor/src/main/java/org/apache/camel/component/reactor/engine/ReactorStreamsService.java
@@ -332,8 +332,8 @@ final class ReactorStreamsService extends ServiceSupport 
implements CamelReactiv
                 }
             });
 
-            // process the exchange once the completion callback is 
registered: the create callback runs after
-            // doOnSubscribe, so processing from there could complete the 
exchange before it is registered
+            // process the exchange once the completion callback is 
registered: Reactor runs doOnSubscribe before the
+            // create callback, so processing from doOnSubscribe could 
complete the exchange before it is registered
             consumer.process(data, 
ReactorStreamsConstants.EMPTY_ASYNC_CALLBACK);
         });
     }
diff --git 
a/components/camel-rxjava/src/main/java/org/apache/camel/component/rxjava/engine/RxJavaStreamsService.java
 
b/components/camel-rxjava/src/main/java/org/apache/camel/component/rxjava/engine/RxJavaStreamsService.java
index 7dcf1a5df232..4a337929b905 100644
--- 
a/components/camel-rxjava/src/main/java/org/apache/camel/component/rxjava/engine/RxJavaStreamsService.java
+++ 
b/components/camel-rxjava/src/main/java/org/apache/camel/component/rxjava/engine/RxJavaStreamsService.java
@@ -302,24 +302,28 @@ final class RxJavaStreamsService extends ServiceSupport 
implements CamelReactive
             throw new IllegalStateException("No consumers attached to the 
stream " + name);
         }
 
-        Single<Exchange> source = Single.<Exchange> create(
-                emitter -> data.getExchangeExtension().addOnCompletion(new 
Synchronization() {
-                    @Override
-                    public void onComplete(Exchange exchange) {
-                        emitter.onSuccess(exchange);
+        Single<Exchange> source = Single.<Exchange> create(emitter -> {
+            data.getExchangeExtension().addOnCompletion(new Synchronization() {
+                @Override
+                public void onComplete(Exchange exchange) {
+                    emitter.onSuccess(exchange);
+                }
+
+                @Override
+                public void onFailure(Exchange exchange) {
+                    Throwable throwable = exchange.getException();
+                    if (throwable == null) {
+                        throwable = new IllegalStateException("Unknown 
Exception");
                     }
 
-                    @Override
-                    public void onFailure(Exchange exchange) {
-                        Throwable throwable = exchange.getException();
-                        if (throwable == null) {
-                            throwable = new IllegalStateException("Unknown 
Exception");
-                        }
+                    emitter.onError(throwable);
+                }
+            });
 
-                        emitter.onError(throwable);
-                    }
-                })).doOnSubscribe(
-                        subs -> consumer.process(data, 
RxJavaStreamsConstants.EMPTY_ASYNC_CALLBACK));
+            // process the exchange once the completion callback is 
registered: RxJava runs doOnSubscribe before the
+            // create callback, so processing from doOnSubscribe could 
complete the exchange before it is registered
+            consumer.process(data, 
RxJavaStreamsConstants.EMPTY_ASYNC_CALLBACK);
+        });
 
         return source.toFlowable();
     }
diff --git 
a/components/camel-rxjava/src/test/java/org/apache/camel/component/rxjava/engine/RxJavaStreamsServiceTest.java
 
b/components/camel-rxjava/src/test/java/org/apache/camel/component/rxjava/engine/RxJavaStreamsServiceTest.java
index ad460f238d99..33498282aa77 100644
--- 
a/components/camel-rxjava/src/test/java/org/apache/camel/component/rxjava/engine/RxJavaStreamsServiceTest.java
+++ 
b/components/camel-rxjava/src/test/java/org/apache/camel/component/rxjava/engine/RxJavaStreamsServiceTest.java
@@ -262,6 +262,21 @@ public class RxJavaStreamsServiceTest extends 
RxJavaStreamsServiceTestSupport {
         assertTrue(latch.await(2, TimeUnit.SECONDS));
     }
 
+    @Test
+    public void testToCompletesWhenRouteIsFasterThanSubscription() throws 
Exception {
+        context.start();
+
+        // the exchange is routed by another thread: its completion must not 
be missed when it is faster than the
+        // subscriber that requested it
+        for (int i = 0; i < 2000; i++) {
+            String value = Flowable.fromPublisher(crs.to("bean:hello", i, 
String.class))
+                    .timeout(30, TimeUnit.SECONDS)
+                    .blockingFirst();
+
+            assertEquals("Hello " + i, value);
+        }
+    }
+
     @Test
     public void testToWithExchange() throws Exception {
         context.start();

Reply via email to