@@ -221,15 +221,15 @@ public void sendMessageStreaming(@Body String body, RoutingContext rc) {
221221 if (error != null ) {
222222 sendResponse (rc , error );
223223 } else if (streamingResponse != null ) {
224- final HTTPRestStreamingResponse finalStreamingResponse = streamingResponse ;
225- executor . execute (() -> {
226- // Convert Flow.Publisher<String> (JSON) to Multi<String> (SSE-formatted )
227- AtomicLong eventIdCounter = new AtomicLong ( 0 );
228- Multi < String > sseEvents = Multi . createFrom (). publisher ( finalStreamingResponse . getPublisher ())
229- . map ( json -> SseFormatter . formatJsonAsSSE ( json , eventIdCounter . getAndIncrement ()));
230- // Write SSE-formatted strings to HTTP response
231- MultiSseSupport . writeSseStrings ( sseEvents , rc , context );
232- } );
224+ // Convert Flow.Publisher<String> (JSON) to Multi<String> (SSE-formatted)
225+ // CRITICAL: Subscribe synchronously to avoid race condition where EventConsumer
226+ // starts emitting events before MultiSseSupport subscribes. The executor.execute( )
227+ // wrapper caused 100-600ms delays before subscription, causing events to be lost.
228+ AtomicLong eventIdCounter = new AtomicLong ( 0 );
229+ Multi < String > sseEvents = Multi . createFrom (). publisher ( streamingResponse . getPublisher ())
230+ . map ( json -> SseFormatter . formatJsonAsSSE ( json , eventIdCounter . getAndIncrement ()));
231+ // Write SSE-formatted strings to HTTP response
232+ MultiSseSupport . writeSseStrings ( sseEvents , rc , context );
233233 }
234234 }
235235 }
@@ -431,15 +431,15 @@ public void subscribeToTask(RoutingContext rc) {
431431 if (error != null ) {
432432 sendResponse (rc , error );
433433 } else if (streamingResponse != null ) {
434- final HTTPRestStreamingResponse finalStreamingResponse = streamingResponse ;
435- executor . execute (() -> {
436- // Convert Flow.Publisher<String> (JSON) to Multi<String> (SSE-formatted )
437- AtomicLong eventIdCounter = new AtomicLong ( 0 );
438- Multi < String > sseEvents = Multi . createFrom (). publisher ( finalStreamingResponse . getPublisher ())
439- . map ( json -> SseFormatter . formatJsonAsSSE ( json , eventIdCounter . getAndIncrement ()));
440- // Write SSE-formatted strings to HTTP response
441- MultiSseSupport . writeSseStrings ( sseEvents , rc , context );
442- } );
434+ // Convert Flow.Publisher<String> (JSON) to Multi<String> (SSE-formatted)
435+ // CRITICAL: Subscribe synchronously to avoid race condition where EventConsumer
436+ // starts emitting events before MultiSseSupport subscribes. The executor.execute( )
437+ // wrapper caused 100-600ms delays before subscription, causing events to be lost.
438+ AtomicLong eventIdCounter = new AtomicLong ( 0 );
439+ Multi < String > sseEvents = Multi . createFrom (). publisher ( streamingResponse . getPublisher ())
440+ . map ( json -> SseFormatter . formatJsonAsSSE ( json , eventIdCounter . getAndIncrement ()));
441+ // Write SSE-formatted strings to HTTP response
442+ MultiSseSupport . writeSseStrings ( sseEvents , rc , context );
443443 }
444444 }
445445 }
0 commit comments