@Beta
public class SessionBuilder
extends java.lang.Object
Client endpoint is replaced by references to onOpen, onError and onClose methods plus message handlers. Same rules
which do apply or limit message handlers are forced here as well. None of the methods is required, so Session
can be opened even without it and used only for sending messages.
Encoders and decoders can be registered by creating
ClientEndpointConfig and registering it to SessionBuilder via
clientEndpointConfig method call.
Code example:
Session session = new SessionBuilder()
.uri(getURI(SessionBuilderEncDecTestEndpoint.class))
.clientEndpointConfig(clientEndpointConfig)
.messageHandler(AClass.class,aClass -> messageLatch.countDown())
.onOpen((session1, endpointConfig) -> onOpenLatch.countDown())
.onError((session1, throwable) -> onErrorLatch.countDown())
.onClose((session1, closeReason) -> onCloseLatch.countDown())
.connect();| Constructor and Description |
|---|
SessionBuilder()
Create new SessionBuilder instance.
|
SessionBuilder(java.lang.String containerProviderClassName)
Create SessionBuilder with provided container provider class name.
|
SessionBuilder(javax.websocket.WebSocketContainer container)
Create SessionBuilder with provided
WebSocketContainer. |
| Modifier and Type | Method and Description |
|---|---|
SessionBuilder |
clientEndpointConfig(javax.websocket.ClientEndpointConfig clientEndpointConfig)
Set
ClientEndpointConfig. |
javax.websocket.Session |
connect()
Connect to the remote (server) endpoint.
|
java.util.concurrent.CompletableFuture<javax.websocket.Session> |
connectAsync()
Connect to the remote (server) endpoint asynchronously.
|
java.util.concurrent.CompletableFuture<javax.websocket.Session> |
connectAsync(java.util.concurrent.ExecutorService executorService)
Connect to the remote (server) endpoint asynchronously.
|
<T> SessionBuilder |
messageHandler(java.lang.Class<T> clazz,
javax.websocket.MessageHandler.Whole<T> messageHandler)
Add whole message handler.
|
<T> SessionBuilder |
messageHandlerPartial(java.lang.Class<T> clazz,
javax.websocket.MessageHandler.Partial<T> messageHandler)
Add partial message handler.
|
SessionBuilder |
onClose(java.util.function.BiConsumer<javax.websocket.Session,javax.websocket.CloseReason> onClose)
Set method reference which will be invoked when a
Session is closed. |
SessionBuilder |
onError(java.util.function.BiConsumer<javax.websocket.Session,java.lang.Throwable> onError)
Set method reference which will be invoked when
OnError method is invoked. |
SessionBuilder |
onOpen(java.util.function.BiConsumer<javax.websocket.Session,javax.websocket.EndpointConfig> onOpen)
Set method reference which will be invoked when a
Session is opened. |
SessionBuilder |
uri(java.net.URI uri)
Set
URI of the server endpoint. |
public SessionBuilder(javax.websocket.WebSocketContainer container)
WebSocketContainer.container - provided websocket container.public SessionBuilder(java.lang.String containerProviderClassName)
Generally, this is used only when you want to have fine-grained control about used container.
containerProviderClassName - container provider class name.public SessionBuilder()
public SessionBuilder clientEndpointConfig(javax.websocket.ClientEndpointConfig clientEndpointConfig)
ClientEndpointConfig.clientEndpointConfig - ClientEndpointConfig to be set.public SessionBuilder uri(java.net.URI uri)
URI of the server endpoint.uri - server endpoint address.public <T> SessionBuilder messageHandler(java.lang.Class<T> clazz, javax.websocket.MessageHandler.Whole<T> messageHandler)
T - handled message type.clazz - handled message type class.messageHandler - message handler.public <T> SessionBuilder messageHandlerPartial(java.lang.Class<T> clazz, javax.websocket.MessageHandler.Partial<T> messageHandler)
T - handled message type.clazz - handled message type class.messageHandler - message handler.public SessionBuilder onOpen(java.util.function.BiConsumer<javax.websocket.Session,javax.websocket.EndpointConfig> onOpen)
Session is opened.onOpen - method invoked when a Session is opened.OnOpenpublic SessionBuilder onError(java.util.function.BiConsumer<javax.websocket.Session,java.lang.Throwable> onError)
OnError method is invoked.onError - method invoked when OnError method is invoked.OnErrorpublic SessionBuilder onClose(java.util.function.BiConsumer<javax.websocket.Session,javax.websocket.CloseReason> onClose)
Session is closed.onClose - method invoked when a Session is closed.OnClosepublic javax.websocket.Session connect()
throws java.io.IOException,
javax.websocket.DeploymentException
This method can be called multiple times, each invocation will result in new Session (new TCP connection
to the server).
java.io.IOException - when there is a problem with connecting to the server endpoint.javax.websocket.DeploymentException - when there is a problem with provided settings or there is other, non IO connection
issue.public java.util.concurrent.CompletableFuture<javax.websocket.Session> connectAsync()
Same statements as at connect() do apply here, only the returned value and possible
exceptions are returned as CompletableFuture.
ForkJoinPool.commonPool() is used for executing the connection phase.
Session when created.public java.util.concurrent.CompletableFuture<javax.websocket.Session> connectAsync(java.util.concurrent.ExecutorService executorService)
Same statements as at connect() do apply here, only the returned value and possible
exceptions are returned as CompletableFuture.
Provided ExecutorService is used for executing the connection phase.
executorService - executor service used for executing the connect() method.Session when created.Copyright © 2012–2025 Oracle Corporation. All rights reserved.