From 0d460a573b44767c4b6c5f21e2f49420e1127aa9 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sat, 12 Oct 2024 02:03:58 +0000 Subject: [PATCH] Sync documentation of main branch --- _versions/main/guides/cdi-integration.adoc | 15 ++++ .../guides/websockets-next-reference.adoc | 69 +++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/_versions/main/guides/cdi-integration.adoc b/_versions/main/guides/cdi-integration.adoc index fe92dd294b..a68baa17ee 100644 --- a/_versions/main/guides/cdi-integration.adoc +++ b/_versions/main/guides/cdi-integration.adoc @@ -486,6 +486,21 @@ if (foo.getHandle().getBean().isActive()) { } ---- +If you want to consume only active beans, you can inject an `InjectableInstance<>` and call `getActive()` to get the single instance or `listActive()` to get all instances: + +[source,java] +---- +import io.quarkus.arc.InjectableInstance; + +@Inject +@Any +InjectableInstance foos; + +for (Foo foo : foos.listActive()) + ... +} +---- + [[synthetic_observers]] == Use Case - Synthetic Observers diff --git a/_versions/main/guides/websockets-next-reference.adoc b/_versions/main/guides/websockets-next-reference.adoc index e36ea29734..36d7a9b98f 100644 --- a/_versions/main/guides/websockets-next-reference.adoc +++ b/_versions/main/guides/websockets-next-reference.adoc @@ -640,6 +640,40 @@ class MyBean { There are also other convenient methods. For example, `OpenConnections#findByEndpointId(String)` makes it easy to find connections for a specific endpoint. +==== User data + +It is also possible to associate arbitrary user data with a specific connection. +The `io.quarkus.websockets.next.UserData` object obtained by the `WebSocketConnection#userData()` method represents mutable user data associated with a connection. + +[source, java] +---- +import io.quarkus.websockets.next.WebSocketConnection; +import io.quarkus.websockets.next.UserData.TypedKey; + +@WebSocket(path = "/endpoint/{username}") +class MyEndpoint { + + @Inject + CoolService service; + + @OnOpen + void open(WebSocketConnection connection) { + connection.userData().put(TypedKey.forBoolean("isCool"), service.isCool(connection.pathParam("username"))); <1> + } + + @OnTextMessage + String process(String message) { + if (connection.userData().get(TypedKey.forBoolean("isCool"))) { <2> + return "Cool message processed!"; + } else { + return "Message processed!"; + } + } +} +---- +<1> `CoolService#isCool()` returns `Boolean` that is associated with the current connection. +<2> The `TypedKey.forBoolean("isCool")` is the key used to obtain the data stored when the connection was created. + [[server-cdi-events]] ==== CDI events @@ -997,6 +1031,41 @@ class MyBean { There are also other convenient methods. For example, `OpenClientConnections#findByClientId(String)` makes it easy to find connections for a specific endpoint. +==== User data + +It is also possible to associate arbitrary user data with a specific connection. +The `io.quarkus.websockets.next.UserData` object obtained by the `WebSocketClientConnection#userData()` method represents mutable user data associated with a connection. + +[source, java] +---- +import io.quarkus.websockets.next.WebSocketClientConnection; +import io.quarkus.websockets.next.UserData.TypedKey; + +@WebSocketClient(path = "/endpoint/{username}") +class MyEndpoint { + + @Inject + CoolService service; + + @OnOpen + void open(WebSocketClientConnection connection) { + connection.userData().put(TypedKey.forBoolean("isCool"), service.isCool(connection.pathParam("username"))); <1> + } + + @OnTextMessage + String process(String message) { + if (connection.userData().get(TypedKey.forBoolean("isCool"))) { <2> + return "Cool message processed!"; + } else { + return "Message processed!"; + } + } +} +---- +<1> `CoolService#isCool()` returns `Boolean` that is associated with the current connection. +<2> The `TypedKey.forBoolean("isCool")` is the key used to obtain the data stored when the connection was created. + + [[client-cdi-events]] ==== CDI events