@Beta public class SessionBuilder extends Object
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(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.
|
CompletableFuture<javax.websocket.Session> |
connectAsync()
Connect to the remote (server) endpoint asynchronously.
|
CompletableFuture<javax.websocket.Session> |
connectAsync(ExecutorService executorService)
Connect to the remote (server) endpoint asynchronously.
|
<T> SessionBuilder |
messageHandler(Class<T> clazz,
javax.websocket.MessageHandler.Whole<T> messageHandler)
Add whole message handler.
|
<T> SessionBuilder |
messageHandlerPartial(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,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(URI uri)
Set
URI of the server endpoint. |
public SessionBuilder(javax.websocket.WebSocketContainer container)
WebSocketContainer
.container
- provided websocket container.public SessionBuilder(String containerProviderClassName)
containerProviderClassName
- container provider class name.public SessionBuilder()
public SessionBuilder clientEndpointConfig(javax.websocket.ClientEndpointConfig clientEndpointConfig)
ClientEndpointConfig
.clientEndpointConfig
- ClientEndpointConfig
to be set.public SessionBuilder uri(URI uri)
URI
of the server endpoint.uri
- server endpoint address.public <T> SessionBuilder messageHandler(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(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.OnOpen
public SessionBuilder onError(java.util.function.BiConsumer<javax.websocket.Session,Throwable> onError)
OnError
method is invoked.onError
- method invoked when OnError
method is invoked.OnError
public SessionBuilder onClose(java.util.function.BiConsumer<javax.websocket.Session,javax.websocket.CloseReason> onClose)
Session
is closed.onClose
- method invoked when a Session
is closed.OnClose
public javax.websocket.Session connect() throws IOException, javax.websocket.DeploymentException
Session
(new TCP connection
to the server).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 CompletableFuture<javax.websocket.Session> connectAsync()
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 CompletableFuture<javax.websocket.Session> connectAsync(ExecutorService executorService)
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-2015, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.