Class SessionBuilder


  • @Beta
    public class SessionBuilder
    extends java.lang.Object
    Session 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 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();
    Author:
    Pavel Bucek
    • Constructor Summary

      Constructors 
      Constructor Description
      SessionBuilder()
      Create new SessionBuilder instance.
      SessionBuilder​(jakarta.websocket.WebSocketContainer container)
      Create SessionBuilder with provided WebSocketContainer.
      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
      SessionBuilder clientEndpointConfig​(jakarta.websocket.ClientEndpointConfig clientEndpointConfig)
      Set ClientEndpointConfig.
      jakarta.websocket.Session connect()
      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> SessionBuilder messageHandler​(java.lang.Class<T> clazz, jakarta.websocket.MessageHandler.Whole<T> messageHandler)
      Add whole message handler.
      <T> SessionBuilder messageHandlerPartial​(java.lang.Class<T> clazz, jakarta.websocket.MessageHandler.Partial<T> messageHandler)
      Add partial message handler.
      SessionBuilder onClose​(java.util.function.BiConsumer<jakarta.websocket.Session,​jakarta.websocket.CloseReason> onClose)
      Set method reference which will be invoked when a Session is closed.
      SessionBuilder onError​(java.util.function.BiConsumer<jakarta.websocket.Session,​java.lang.Throwable> onError)
      Set method reference which will be invoked when OnError method is invoked.
      SessionBuilder onOpen​(java.util.function.BiConsumer<jakarta.websocket.Session,​jakarta.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SessionBuilder

        public SessionBuilder​(jakarta.websocket.WebSocketContainer container)
        Create SessionBuilder with provided WebSocketContainer.
        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)
        Set ClientEndpointConfig.
        Parameters:
        clientEndpointConfig - ClientEndpointConfig to be set.
        Returns:
        updated SessionBuilder instance.
      • uri

        public SessionBuilder uri​(java.net.URI uri)
        Set URI of 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 a Session is opened.
        Parameters:
        onOpen - method invoked when a Session is 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 when OnError method is invoked.
        Parameters:
        onError - method invoked when OnError method 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 a Session is closed.
        Parameters:
        onClose - method invoked when a Session is closed.
        Returns:
        updated SessionBuilder instance.
        See Also:
        OnClose
      • connect

        public jakarta.websocket.Session connect()
                                          throws java.io.IOException,
                                                 jakarta.websocket.DeploymentException
        Connect 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 as CompletableFuture.

        ForkJoinPool.commonPool() is used for executing the connection phase.

        Returns:
        completable future returning Session when 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 as CompletableFuture.

        Provided ExecutorService is used for executing the connection phase.

        Parameters:
        executorService - executor service used for executing the connect() method.
        Returns:
        completable future returning Session when created.