Package gov.usgs.earthquake.distribution
Class WebSocketClient
java.lang.Object
gov.usgs.util.DefaultConfigurable
gov.usgs.earthquake.distribution.WebSocketClient
- All Implemented Interfaces:
Configurable,Runnable
Manages a simple connection to a websocket. Can also be overridden for more
complex behavior.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringProperty name to configure the anyMessageIntervalMillis value for this WebSocketClientstatic final StringProperty name to configure the connectRetries value for this WebSocketClientstatic final StringProperty name to configure the connectTimeout value for this WebSocketClientstatic final longDefault for how long to wait for any Message(excluding Pongs)static final intDefault number of attemptsstatic final longDefault for time in between pings to serverstatic final longDefault for how long to wait for pong response to ping before closing or restarting connectionstatic final booleanDefault for trying to retry on closestatic final longDefault timeout in msstatic final LoggerInitialzation of logger.static final StringProperty name to configure the pingIntervalMillis value for this WebSocketClientstatic final StringProperty name to configure the pingWaitMillis value for this WebSocketClientstatic final StringProperty name to configure the retryOnClose value for this WebSocketClientstatic final StringProperty name to configure the url value for this WebSocketClient -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor required for configurable interface.WebSocketClient(URI endpoint, WebSocketListener listener) Constructs the clientWebSocketClient(URI endpoint, WebSocketListener listener, int attempts, long timeoutMillis, boolean retryOnClose) Deprecated.WebSocketClient(URI endpoint, WebSocketListener listener, int attempts, long timeoutMillis, boolean retryOnClose, long pingIntervalMillis, long pingWaitMillis, long anyMessageIntervalMillis) Creates a Websocket Client Default values for attempts ant timeoutMillis create an instance which is designed to be up and running at all times -
Method Summary
Modifier and TypeMethodDescriptionvoidcatchPong(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.voidProcess configuration settings.voidconnect()Connect to serverlongintlonglonglongbooleanChecks if there is an open sessionbooleanvoidonClose(javax.websocket.Session session, javax.websocket.CloseReason reason) Closes the session on the listener, sets constructor session to null Check if should be retryedvoidGives listener the messagevoidonOpen(javax.websocket.Session session) Sets the session and listenervoidrun()voidsetAnyMessageIntervalMillis(long anyMessageIntervalMillis) voidsetListener(WebSocketListener listener) voidsetPingIntervalMillis(long pingIntervalMillis) voidshutdown()Sets retry to false, then closes sessionvoidstartup()Connect the clientMethods inherited from class gov.usgs.util.DefaultConfigurable
getName, setName
-
Field Details
-
LOGGER
Initialzation of logger. For us later in file. -
DEFAULT_ATTEMPTS
public static final int DEFAULT_ATTEMPTSDefault number of attempts- See Also:
-
DEFAULT_TIMEOUT_MILLIS
public static final long DEFAULT_TIMEOUT_MILLISDefault timeout in ms- See Also:
-
DEFAULT_RETRY_ON_CLOSE
public static final boolean DEFAULT_RETRY_ON_CLOSEDefault for trying to retry on close- See Also:
-
DEFAULT_PING_INTERVAL_MILLIS
public static final long DEFAULT_PING_INTERVAL_MILLISDefault for time in between pings to server- See Also:
-
DEFAULT_PING_WAIT_MILLIS
public static final long DEFAULT_PING_WAIT_MILLISDefault 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_MILLISDefault for how long to wait for any Message(excluding Pongs)- See Also:
-
PING_WAIT_MILLIS_PROPERTY
Property name to configure the pingWaitMillis value for this WebSocketClient- See Also:
-
ANY_MESSAGE_INTERVAL_MILLIS_PROPERTY
Property name to configure the anyMessageIntervalMillis value for this WebSocketClient- See Also:
-
PING_INTERVAL_MILLIS_PROPERTY
Property name to configure the pingIntervalMillis value for this WebSocketClient- See Also:
-
CONNECT_RETRIES_PROPERTY
Property name to configure the connectRetries value for this WebSocketClient- See Also:
-
URI_PROPERTY
Property name to configure the url value for this WebSocketClient- See Also:
-
CONNECT_TIMEOUT_PROPERTY
Property name to configure the connectTimeout value for this WebSocketClient- See Also:
-
RETRY_ON_CLOSE_PROPERTY
Property name to configure the retryOnClose value for this WebSocketClient- See Also:
-
-
Constructor Details
-
WebSocketClient
Default constructor required for configurable interface.- Throws:
Exception
-
WebSocketClient
@Deprecated public WebSocketClient(URI endpoint, WebSocketListener listener, int attempts, long timeoutMillis, boolean retryOnClose) throws Exception Deprecated.useWebSocketClient(URI, WebSocketListener, int, long, boolean, long, long, long), includes ping configurationConstructs the client. Also connects to the server.- Parameters:
endpoint- the URI to connect tolistener- a WebSocketListener to handle incoming messagesattempts- an integer number of times to try the connectiontimeoutMillis- a long for the wait time between attemptsretryOnClose- 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 tolistener- a WebSocketListener to handle incoming messagesattempts- an integer number of times to try the connectiontimeoutMillis- a long for the wait time between attemptsretryOnClose- boolean for if the connection should retry when closedpingIntervalMillis- 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
Constructs the client- Parameters:
endpoint- the URI to connect tolistener- 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
Connect to server- Throws:
Exception- if error occurs
-
isConnected
Checks if there is an open session- Returns:
- boolean
- Throws:
IOException- if IO error occurs
-
onOpen
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- Sessionreason- for close- Throws:
IOException- if IO error occurs
-
onMessage
Gives listener the message- Parameters:
message- String- Throws:
IOException- if IO error occurs
-
run
public void run() -
configure
Description copied from class:DefaultConfigurableProcess configuration settings. Called before startup().- Specified by:
configurein interfaceConfigurable- Overrides:
configurein classDefaultConfigurable- Parameters:
config- the Config object with settings.- Throws:
Exception- if configuration exceptions occur.
-
startup
Connect the client- Specified by:
startupin interfaceConfigurable- Overrides:
startupin classDefaultConfigurable- Throws:
Exception
-
shutdown
Sets retry to false, then closes session- Specified by:
shutdownin interfaceConfigurable- Overrides:
shutdownin classDefaultConfigurable- Throws:
Exception- if error occurs
-
setListener
- Parameters:
listener- set WebSocketListener
-
getAnyMessageIntervalMillis
public long getAnyMessageIntervalMillis() -
getAttempts
public int getAttempts() -
getEndpoint
-
getPingIntervalMillis
public long getPingIntervalMillis() -
getPingWaitMillis
public long getPingWaitMillis() -
getScheduledExector
-
getTimeoutMillis
public long getTimeoutMillis() -
isRetryOnClose
public boolean isRetryOnClose() -
setPingIntervalMillis
- Throws:
Exception
-
setAnyMessageIntervalMillis
- Throws:
Exception
-
WebSocketClient(URI, WebSocketListener, int, long, boolean, long, long, long), includes ping configuration