Class WebSocketClient

java.lang.Object
gov.usgs.util.DefaultConfigurable
gov.usgs.earthquake.distribution.WebSocketClient
All Implemented Interfaces:
Configurable, Runnable

public class WebSocketClient extends DefaultConfigurable implements Runnable
Manages a simple connection to a websocket. Can also be overridden for more complex behavior.
  • Field Details

    • LOGGER

      public static final Logger LOGGER
      Initialzation of logger. For us later in file.
    • DEFAULT_ATTEMPTS

      public static final int DEFAULT_ATTEMPTS
      Default number of attempts
      See Also:
    • DEFAULT_TIMEOUT_MILLIS

      public static final long DEFAULT_TIMEOUT_MILLIS
      Default timeout in ms
      See Also:
    • DEFAULT_RETRY_ON_CLOSE

      public static final boolean DEFAULT_RETRY_ON_CLOSE
      Default for trying to retry on close
      See Also:
    • DEFAULT_PING_INTERVAL_MILLIS

      public static final long DEFAULT_PING_INTERVAL_MILLIS
      Default for time in between pings to server
      See Also:
    • DEFAULT_PING_WAIT_MILLIS

      public static final long DEFAULT_PING_WAIT_MILLIS
      Default for how long to wait for pong response to ping before closing or restarting connection
      See Also:
    • DEFAULT_ANY_MESSAGE_INTERVAL_MILLIS

      public static final long DEFAULT_ANY_MESSAGE_INTERVAL_MILLIS
      Default for how long to wait for any Message(excluding Pongs)
      See Also:
    • PING_WAIT_MILLIS_PROPERTY

      public static final String PING_WAIT_MILLIS_PROPERTY
      Property name to configure the pingWaitMillis value for this WebSocketClient
      See Also:
    • ANY_MESSAGE_INTERVAL_MILLIS_PROPERTY

      public static final String ANY_MESSAGE_INTERVAL_MILLIS_PROPERTY
      Property name to configure the anyMessageIntervalMillis value for this WebSocketClient
      See Also:
    • PING_INTERVAL_MILLIS_PROPERTY

      public static final String PING_INTERVAL_MILLIS_PROPERTY
      Property name to configure the pingIntervalMillis value for this WebSocketClient
      See Also:
    • CONNECT_RETRIES_PROPERTY

      public static final String CONNECT_RETRIES_PROPERTY
      Property name to configure the connectRetries value for this WebSocketClient
      See Also:
    • URI_PROPERTY

      public static final String URI_PROPERTY
      Property name to configure the url value for this WebSocketClient
      See Also:
    • CONNECT_TIMEOUT_PROPERTY

      public static final String CONNECT_TIMEOUT_PROPERTY
      Property name to configure the connectTimeout value for this WebSocketClient
      See Also:
    • RETRY_ON_CLOSE_PROPERTY

      public static final String RETRY_ON_CLOSE_PROPERTY
      Property name to configure the retryOnClose value for this WebSocketClient
      See Also:
  • Constructor Details

    • WebSocketClient

      public WebSocketClient() throws Exception
      Default constructor required for configurable interface.
      Throws:
      Exception
    • WebSocketClient

      @Deprecated public WebSocketClient(URI endpoint, WebSocketListener listener, int attempts, long timeoutMillis, boolean retryOnClose) throws Exception
      Constructs the client. Also connects to the server.
      Parameters:
      endpoint - the URI to connect to
      listener - a WebSocketListener to handle incoming messages
      attempts - an integer number of times to try the connection
      timeoutMillis - a long for the wait time between attempts
      retryOnClose - boolean for if the connection should retry when closed
      Throws:
      Exception - on thread interrupt or connection failure
    • WebSocketClient

      public WebSocketClient(URI endpoint, WebSocketListener listener, int attempts, long timeoutMillis, boolean retryOnClose, long pingIntervalMillis, long pingWaitMillis, long anyMessageIntervalMillis) throws Exception
      Creates a Websocket Client Default values for attempts ant timeoutMillis create an instance which is designed to be up and running at all times
      Parameters:
      endpoint - the URI to connect to
      listener - a WebSocketListener to handle incoming messages
      attempts - an integer number of times to try the connection
      timeoutMillis - a long for the wait time between attempts
      retryOnClose - boolean for if the connection should retry when closed
      pingIntervalMillis - how often to send ping in milliseconds. If you don't want to send pings set to 0 or negative value.
      pingWaitMillis - how long to wait, in milliseconds, before declaring socket down and closing and retrying if retryOnClose is set.
      anyMessageIntervalMillis - how often to check if any message has arrived(excluding Pongs) restart if one has not.
      Throws:
      Exception - on thread interrupt or connection failure
    • WebSocketClient

      public WebSocketClient(URI endpoint, WebSocketListener listener) throws Exception
      Constructs the client
      Parameters:
      endpoint - the URI to connect to
      listener - a WebSocketListener to handle incoming messages
      Throws:
      Exception - thread interrupt or connection failure
  • Method Details

    • catchPong

      public void catchPong(javax.websocket.PongMessage pongMessage, javax.websocket.Session session)
      Called by Websocket interface when a Pong is received in response to a ping that was sent.
      Parameters:
      pongMessage - Message that was populated from ping.
      session - The websocket session.
    • connect

      public void connect() throws Exception
      Connect to server
      Throws:
      Exception - if error occurs
    • isConnected

      public boolean isConnected() throws IOException
      Checks if there is an open session
      Returns:
      boolean
      Throws:
      IOException - if IO error occurs
    • onOpen

      public void onOpen(javax.websocket.Session session) throws IOException
      Sets the session and listener
      Parameters:
      session - Session
      Throws:
      IOException - if IO error occurs
    • onClose

      public void onClose(javax.websocket.Session session, javax.websocket.CloseReason reason) throws IOException
      Closes the session on the listener, sets constructor session to null Check if should be retryed
      Parameters:
      session - Session
      reason - for close
      Throws:
      IOException - if IO error occurs
    • onMessage

      public void onMessage(String message) throws IOException
      Gives listener the message
      Parameters:
      message - String
      Throws:
      IOException - if IO error occurs
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • configure

      public void configure(Config config) throws Exception
      Description copied from class: DefaultConfigurable
      Process configuration settings. Called before startup().
      Specified by:
      configure in interface Configurable
      Overrides:
      configure in class DefaultConfigurable
      Parameters:
      config - the Config object with settings.
      Throws:
      Exception - if configuration exceptions occur.
    • startup

      public void startup() throws Exception
      Connect the client
      Specified by:
      startup in interface Configurable
      Overrides:
      startup in class DefaultConfigurable
      Throws:
      Exception
    • shutdown

      public void shutdown() throws Exception
      Sets retry to false, then closes session
      Specified by:
      shutdown in interface Configurable
      Overrides:
      shutdown in class DefaultConfigurable
      Throws:
      Exception - if error occurs
    • setListener

      public void setListener(WebSocketListener listener)
      Parameters:
      listener - set WebSocketListener
    • getAnyMessageIntervalMillis

      public long getAnyMessageIntervalMillis()
    • getAttempts

      public int getAttempts()
    • getEndpoint

      public URI getEndpoint()
    • getPingIntervalMillis

      public long getPingIntervalMillis()
    • getPingWaitMillis

      public long getPingWaitMillis()
    • getScheduledExector

      public ScheduledExecutorService getScheduledExector()
    • getTimeoutMillis

      public long getTimeoutMillis()
    • isRetryOnClose

      public boolean isRetryOnClose()
    • setPingIntervalMillis

      public void setPingIntervalMillis(long pingIntervalMillis) throws Exception
      Throws:
      Exception
    • setAnyMessageIntervalMillis

      public void setAnyMessageIntervalMillis(long anyMessageIntervalMillis) throws Exception
      Throws:
      Exception