Class ConnectionManagerSupport

java.lang.Object
org.springframework.web.socket.client.ConnectionManagerSupport
All Implemented Interfaces:
Lifecycle, Phased, SmartLifecycle
Direct Known Subclasses:
AnnotatedEndpointConnectionManager, EndpointConnectionManager, WebSocketConnectionManager

public abstract class ConnectionManagerSupport extends Object implements SmartLifecycle
Base class for a connection manager that automates the process of connecting to a WebSocket server with the Spring ApplicationContext lifecycle. Connects to a WebSocket server on start() and disconnects on stop(). If setAutoStartup(boolean) is set to true this will be done automatically when the Spring ApplicationContext is refreshed.
Since:
4.0
Author:
Rossen Stoyanchev
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Log
     

    Fields inherited from interface org.springframework.context.SmartLifecycle

    DEFAULT_PHASE
  • Constructor Summary

    Constructors
    Constructor
    Description
    ConnectionManagerSupport(String uriTemplate, Object... uriVariables)
    Constructor with a URI template and variables.
    Constructor with a prepared URI.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract void
    Subclasses implement this to close the connection.
    int
    Return the phase in which this endpoint connection factory will be auto-connected and stopped.
    protected URI
     
    boolean
    Return the value for the 'autoStartup' property.
    abstract boolean
    Whether the connection is open/true or closed/false.
    boolean
    Return whether this ConnectionManager has been started.
    protected abstract void
    Subclasses implement this to actually establish the connection.
    void
    setAutoStartup(boolean autoStartup)
    Set whether to auto-connect to the remote endpoint after this connection manager has been initialized and the Spring context has been refreshed.
    void
    setPhase(int phase)
    Specify the phase in which a connection should be established to the remote endpoint and subsequently closed.
    final void
    Start the WebSocket connection.
    protected void
     
    final void
    Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.
    final void
    stop(Runnable callback)
    Indicates that a Lifecycle component must stop if it is currently running.
    protected void
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • logger

      protected final Log logger
  • Constructor Details

    • ConnectionManagerSupport

      public ConnectionManagerSupport(String uriTemplate, Object... uriVariables)
      Constructor with a URI template and variables.
    • ConnectionManagerSupport

      public ConnectionManagerSupport(URI uri)
      Constructor with a prepared URI.
      Parameters:
      uri - the url to connect to
      Since:
      6.0.5
  • Method Details

    • getUri

      protected URI getUri()
    • setAutoStartup

      public void setAutoStartup(boolean autoStartup)
      Set whether to auto-connect to the remote endpoint after this connection manager has been initialized and the Spring context has been refreshed.

      Default is "false".

    • isAutoStartup

      public boolean isAutoStartup()
      Return the value for the 'autoStartup' property. If "true", this endpoint connection manager will connect to the remote endpoint upon a ContextRefreshedEvent.
      Specified by:
      isAutoStartup in interface SmartLifecycle
      See Also:
    • setPhase

      public void setPhase(int phase)
      Specify the phase in which a connection should be established to the remote endpoint and subsequently closed. The startup order proceeds from lowest to highest, and the shutdown order is the reverse of that. By default, this value is Integer.MAX_VALUE meaning that this endpoint connection factory connects as late as possible and is closed as soon as possible.
    • getPhase

      public int getPhase()
      Return the phase in which this endpoint connection factory will be auto-connected and stopped.
      Specified by:
      getPhase in interface Phased
      Specified by:
      getPhase in interface SmartLifecycle
      See Also:
    • start

      public final void start()
      Start the WebSocket connection. If already connected, the method has no impact.
      Specified by:
      start in interface Lifecycle
      See Also:
    • startInternal

      protected void startInternal()
    • stop

      public final void stop()
      Description copied from interface: Lifecycle
      Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementing SmartLifecycle and its stop(Runnable) variant when asynchronous stop behavior is necessary.

      Note that this stop notification is not guaranteed to come before destruction: On regular shutdown, Lifecycle beans will first receive a stop notification before the general destruction callbacks are being propagated; however, on hot refresh during a context's lifetime or on aborted refresh attempts, a given bean's destroy method will be called without any consideration of stop signals upfront.

      Should not throw an exception if the component is not running (not started yet).

      In the case of a container, this will propagate the stop signal to all components that apply.

      Specified by:
      stop in interface Lifecycle
      See Also:
    • stop

      public final void stop(Runnable callback)
      Description copied from interface: SmartLifecycle
      Indicates that a Lifecycle component must stop if it is currently running.

      The provided callback is used by the LifecycleProcessor to support an ordered, and potentially concurrent, shutdown of all components having a common shutdown order value. The callback must be executed after the SmartLifecycle component does indeed stop.

      The LifecycleProcessor will call only this variant of the stop method; i.e. Lifecycle.stop() will not be called for SmartLifecycle implementations unless explicitly delegated to within the implementation of this method.

      The default implementation delegates to Lifecycle.stop() and immediately triggers the given callback in the calling thread. Note that there is no synchronization between the two, so custom implementations may at least want to put the same steps within their common lifecycle monitor (if any).

      Specified by:
      stop in interface SmartLifecycle
      See Also:
    • stopInternal

      protected void stopInternal() throws Exception
      Throws:
      Exception
    • isRunning

      public boolean isRunning()
      Return whether this ConnectionManager has been started.
      Specified by:
      isRunning in interface Lifecycle
      Returns:
      whether the component is currently running
    • isConnected

      public abstract boolean isConnected()
      Whether the connection is open/true or closed/false.
    • openConnection

      protected abstract void openConnection()
      Subclasses implement this to actually establish the connection.
    • closeConnection

      protected abstract void closeConnection() throws Exception
      Subclasses implement this to close the connection.
      Throws:
      Exception