Open andreiyusupau opened 5 months ago
Wouldn't parallelStream() solve this?
@Inject
OpenConnections connections;
...
connections.listAll().parallelStream().forEach(session -> {
session.sendText([big slow text send]);
});
Sorry if that's not relevant but I'm was just passing by pulling my hair on why some websocket clients don't connect to Websockets Next but do with Jakarta Websockets ans just saw this issue.
@PedroBatista it doesn't seem to be solution for all cases. General problem is that WebSockets Next can't execute connection.sendText(message);
in another thread, because connection is SessionScoped
and such scope can't be processed in another thread. Direct injection of connection
into method parameter (similar to Session
in Jakarta WS @OnMessage
method) would be great.
@PedroBatista it doesn't seem to be solution for all cases. General problem is that WebSockets Next can't execute
connection.sendText(message);
in another thread, because connection isSessionScoped
and such scope can't be processed in another thread. Direct injection ofconnection
into method parameter (similar toSession
in Jakarta WS@OnMessage
method) would be great.
@andreiyusupau well, this is supported for a long time: https://quarkus.io/version/main/guides/websockets-next-reference#method-parameters
When working with Quarkus WebSockets Next it seems impossible to execute async callbacks.
@PedroBatista is right in the sense that the session context will not be active when a custom ExecutorService
is used.
Also, if you make use of WebSocketConnection
parameter or @Inject OpenConnections
then you don't work with a client proxy (@SessionScoped
) and connection.sendText(message)
should just work:
@WebSocket(path = "/test-next")
public class WebsocketNext {
private final ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
@OnTextMessage
public void onMessage(String message, WebSocketConnection connection) {
executorService.execute(() -> {
try {
// emulate slow operation
TimeUnit.SECONDS.sleep(2L);
connection.sendText(message);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
}
@andreiyusupau By the way, virtual threads are supported by means of @RunOnVirtualThread
, i.e. a method with blocking signature annotated with @RunOnVirtualThread
spawns a new virtual thread. See also documentation.
@andreiyusupau well, this is supported for a long time: https://quarkus.io/version/main/guides/websockets-next-reference#method-parameters
@mkouba Great that it's already supported!
But why all the examples (https://quarkus.io/version/main/guides/websockets-next-reference, https://quarkus.io/guides/websockets-next-tutorial) use @Inject WebSocketConnection connection
instead of just passing connection as method parameter (seems far more straightforward to me)?
But why all the examples (https://quarkus.io/version/main/guides/websockets-next-reference, https://quarkus.io/guides/websockets-next-tutorial) use
@Inject WebSocketConnection connection
instead of just passing connection as method parameter (seems far more straightforward to me)?
It's a matter of taste I guess ;-). Also @Inject WebSocketConnection
was supported first so that's probably the reason :shrug:.
Description
When working with Quarkus WebSockets Next it seems impossible to execute async callbacks. Use cases are:
import io.quarkus.websockets.next.OnTextMessage; import io.quarkus.websockets.next.WebSocket; import io.quarkus.websockets.next.WebSocketConnection; import jakarta.inject.Inject;
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit;
@WebSocket(path = "/test-next") public class WebsocketNext {
}
Same example for Jakarta Websockets implemented by Quarkus works fine:
Implementation ideas
No response