Class SessionBuilder
- java.lang.Object
-
- org.glassfish.tyrus.ext.client.java8.SessionBuilder
-
@Beta public class SessionBuilder extends java.lang.ObjectSession builder removed the need for having client endpoint.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
Sessioncan be opened even without it and used only for sending messages.Encodersanddecoderscan be registered by creatingClientEndpointConfigand registering it to SessionBuilder viaclientEndpointConfigmethod 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();- Author:
- Pavel Bucek
-
-
Constructor Summary
Constructors Constructor Description SessionBuilder()Create new SessionBuilder instance.SessionBuilder(jakarta.websocket.WebSocketContainer container)Create SessionBuilder with providedWebSocketContainer.SessionBuilder(java.lang.String containerProviderClassName)Create SessionBuilder with provided container provider class name.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SessionBuilderclientEndpointConfig(jakarta.websocket.ClientEndpointConfig clientEndpointConfig)SetClientEndpointConfig.jakarta.websocket.Sessionconnect()Connect to the remote (server) endpoint.java.util.concurrent.CompletableFuture<jakarta.websocket.Session>connectAsync()Connect to the remote (server) endpoint asynchronously.java.util.concurrent.CompletableFuture<jakarta.websocket.Session>connectAsync(java.util.concurrent.ExecutorService executorService)Connect to the remote (server) endpoint asynchronously.<T> SessionBuildermessageHandler(java.lang.Class<T> clazz, jakarta.websocket.MessageHandler.Whole<T> messageHandler)Add whole message handler.<T> SessionBuildermessageHandlerPartial(java.lang.Class<T> clazz, jakarta.websocket.MessageHandler.Partial<T> messageHandler)Add partial message handler.SessionBuilderonClose(java.util.function.BiConsumer<jakarta.websocket.Session,jakarta.websocket.CloseReason> onClose)Set method reference which will be invoked when aSessionis closed.SessionBuilderonError(java.util.function.BiConsumer<jakarta.websocket.Session,java.lang.Throwable> onError)Set method reference which will be invoked whenOnErrormethod is invoked.SessionBuilderonOpen(java.util.function.BiConsumer<jakarta.websocket.Session,jakarta.websocket.EndpointConfig> onOpen)Set method reference which will be invoked when aSessionis opened.SessionBuilderuri(java.net.URI uri)SetURIof the server endpoint.
-
-
-
Constructor Detail
-
SessionBuilder
public SessionBuilder(jakarta.websocket.WebSocketContainer container)
Create SessionBuilder with providedWebSocketContainer.- Parameters:
container- provided websocket container.
-
SessionBuilder
public SessionBuilder(java.lang.String containerProviderClassName)
Create SessionBuilder with provided container provider class name.Generally, this is used only when you want to have fine-grained control about used container.
- Parameters:
containerProviderClassName- container provider class name.
-
SessionBuilder
public SessionBuilder()
Create new SessionBuilder instance.
-
-
Method Detail
-
clientEndpointConfig
public SessionBuilder clientEndpointConfig(jakarta.websocket.ClientEndpointConfig clientEndpointConfig)
SetClientEndpointConfig.- Parameters:
clientEndpointConfig-ClientEndpointConfigto be set.- Returns:
- updated SessionBuilder instance.
-
uri
public SessionBuilder uri(java.net.URI uri)
SetURIof the server endpoint.- Parameters:
uri- server endpoint address.- Returns:
- updated SessionBuilder instance.
-
messageHandler
public <T> SessionBuilder messageHandler(java.lang.Class<T> clazz, jakarta.websocket.MessageHandler.Whole<T> messageHandler)
Add whole message handler.- Type Parameters:
T- handled message type.- Parameters:
clazz- handled message type class.messageHandler- message handler.- Returns:
- updated SessionBuilder instance.
-
messageHandlerPartial
public <T> SessionBuilder messageHandlerPartial(java.lang.Class<T> clazz, jakarta.websocket.MessageHandler.Partial<T> messageHandler)
Add partial message handler.- Type Parameters:
T- handled message type.- Parameters:
clazz- handled message type class.messageHandler- message handler.- Returns:
- updated SessionBuilder instance.
-
onOpen
public SessionBuilder onOpen(java.util.function.BiConsumer<jakarta.websocket.Session,jakarta.websocket.EndpointConfig> onOpen)
Set method reference which will be invoked when aSessionis opened.- Parameters:
onOpen- method invoked when aSessionis opened.- Returns:
- updated SessionBuilder instance.
- See Also:
OnOpen
-
onError
public SessionBuilder onError(java.util.function.BiConsumer<jakarta.websocket.Session,java.lang.Throwable> onError)
Set method reference which will be invoked whenOnErrormethod is invoked.- Parameters:
onError- method invoked whenOnErrormethod is invoked.- Returns:
- updated SessionBuilder instance.
- See Also:
OnError
-
onClose
public SessionBuilder onClose(java.util.function.BiConsumer<jakarta.websocket.Session,jakarta.websocket.CloseReason> onClose)
Set method reference which will be invoked when aSessionis closed.- Parameters:
onClose- method invoked when aSessionis closed.- Returns:
- updated SessionBuilder instance.
- See Also:
OnClose
-
connect
public jakarta.websocket.Session connect() throws java.io.IOException, jakarta.websocket.DeploymentExceptionConnect to the remote (server) endpoint.This method can be called multiple times, each invocation will result in new
Session(new TCP connection to the server).- Returns:
- created session.
- Throws:
java.io.IOException- when there is a problem with connecting to the server endpoint.jakarta.websocket.DeploymentException- when there is a problem with provided settings or there is other, non IO connection issue.
-
connectAsync
public java.util.concurrent.CompletableFuture<jakarta.websocket.Session> connectAsync()
Connect to the remote (server) endpoint asynchronously.Same statements as at
connect()do apply here, only the returned value and possible exceptions are returned asCompletableFuture.ForkJoinPool.commonPool()is used for executing the connection phase.- Returns:
- completable future returning
Sessionwhen created.
-
connectAsync
public java.util.concurrent.CompletableFuture<jakarta.websocket.Session> connectAsync(java.util.concurrent.ExecutorService executorService)
Connect to the remote (server) endpoint asynchronously.Same statements as at
connect()do apply here, only the returned value and possible exceptions are returned asCompletableFuture.Provided
ExecutorServiceis used for executing the connection phase.- Parameters:
executorService- executor service used for executing theconnect()method.- Returns:
- completable future returning
Sessionwhen created.
-
-