Class PersistentConnection<C>
- Type Parameters:
C
- connection context type
- Direct Known Subclasses:
PersistentSocketConnection
This class is suitable for use with any abstract notion of "client", "server", and "connection". Typically it would be used to maintain a persistent connection to a remote server over the network. This class mainly serves to implement the connection state machine, including an exponential back-off retry timer, subclass notifications for state transitions, and guaranteed thread safety.
Each instance has a dedicated thread that manages the connection and performs any required work while connected.
The lifecycle of a connection is delimited by calls (by this thread) to the subclass methods
createConnection()
and cleanupConnection()
.
The actual per-connection work is performed by handleConnection()
.
Each connection has an associated connection context (defined by the subclass) and passed to these methods.
The subclass is also notified of state machine transitions via the state transition methods
started()
, stopped()
, connectionSuccessful()
,
connectionFailed()
, and connectionEnded()
;
all of these methods are invoked by the background thread.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final long
Default maximum restart delay.static final long
Default minimum restart delay.protected final Logger
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
cleanupConnection
(C connectionContext) Perform cleanup after a server connection ends.protected void
State machine transition: an established connection to the server has ended.protected void
State machine transition: initialization of a new connection viacreateConnection()
failed due to an exception.protected void
State machine transition: initialization of a new connection viacreateConnection()
was successful and we are connected to the server.protected abstract C
Create a new server connection.long
long
protected String
Get the name to use for the network client thread.protected abstract void
handleConnection
(C connectionContext) Handle one server connection.final boolean
Determine if this instance is started.void
setMaxRestartDelay
(long maxRestartDelay) Set the maximum restart delay after being disconnected from the server.void
setMinRestartDelay
(long minRestartDelay) Set the minimum restart delay after being disconnected from the server.void
start()
Start this instance.protected void
started()
State machine transition: this instance was started viastart()
.void
stop()
Stop this client.protected void
State machine transition: this instance was stopped viastop()
of because or an unchecked exception was thrown by one of the subclass methods.
-
Field Details
-
DEFAULT_MIN_RESTART_DELAY
public static final long DEFAULT_MIN_RESTART_DELAYDefault minimum restart delay. -
DEFAULT_MAX_RESTART_DELAY
public static final long DEFAULT_MAX_RESTART_DELAYDefault maximum restart delay. -
log
-
-
Constructor Details
-
PersistentConnection
public PersistentConnection()
-
-
Method Details
-
setMinRestartDelay
public void setMinRestartDelay(long minRestartDelay) Set the minimum restart delay after being disconnected from the server. Default isDEFAULT_MIN_RESTART_DELAY
(10000L}ms).- Parameters:
minRestartDelay
- minimum restart delay in milliseconds
-
getMinRestartDelay
public long getMinRestartDelay() -
setMaxRestartDelay
public void setMaxRestartDelay(long maxRestartDelay) Set the maximum restart delay after being disconnected from the server. Default isDEFAULT_MAX_RESTART_DELAY
(600000L}ms).- Parameters:
maxRestartDelay
- maximum restart delay in milliseconds
-
getMaxRestartDelay
public long getMaxRestartDelay() -
getThreadName
Get the name to use for the network client thread.The implementation in
PersistentConnection
returnsthis.toString() + " thread"
.- Returns:
- network client thread name
-
start
public void start()Start this instance. This starts the background thread, which initiates the first connection attempt.If this instance is already started, nothing happens.
-
stop
public void stop()Stop this client.This method simply interrupts the background thread, if any.
-
isRunning
public final boolean isRunning()Determine if this instance is started.- Returns:
- true if this instance is currently running
-
createConnection
Create a new server connection.If this method throws an unchecked exception,
stopped()
will be invoked with the exception and this instance will be automatically stopped.- Returns:
- context for the connection, which will be supplied to
handleConnection()
andcleanupConnection()
- Throws:
InterruptedException
- if interruptedIOException
- if there is a problem establishing the connection
-
handleConnection
protected abstract void handleConnection(C connectionContext) throws InterruptedException, IOException Handle one server connection.This method may either throw an exception or return normally; the only difference is whether
connectionEnded()
is invoked with a non-null parameter or not.Ideally this method should never return normally. However, in practice there are legitimate reasons to do so, for example, if there is an application-level error that indicates the particular connection is no longer usable.
If this method throws an unchecked exception,
stopped()
will be invoked with the exception and this instance will be automatically stopped.- Parameters:
connectionContext
- connection context returned fromcreateConnection()
when this connection was created- Throws:
InterruptedException
- if interruptedIOException
- if there is a problem during the connection or the connection fails
-
cleanupConnection
Perform cleanup after a server connection ends. This method should close any open sockets, etc.For each successful invocation of
createConnection()
there is guaranteed be exactly one invocation of this method.The
exception
parameter indicates either a normal return (if null) or thrown exception (if not null) fromhandleConnection()
. In any case, this instance will automatically begin attempting to reconnect when this method returns.If this method throws an unchecked exception,
stopped()
will be invoked with the exception and this instance will be automatically stopped.The implementation in
PersistentConnection
does nothing. Subclasses should override if necessary.- Parameters:
connectionContext
- connection context returned fromcreateConnection()
when this connection was created
-
started
protected void started()State machine transition: this instance was started viastart()
.The implementation in
PersistentConnection
does nothing; subclasses may override. -
stopped
State machine transition: this instance was stopped viastop()
of because or an unchecked exception was thrown by one of the subclass methods.The implementation in
PersistentConnection
does nothing; subclasses may override.- Parameters:
t
- unexpected exception, or null if this instance was stopped viastop()
.
-
connectionSuccessful
protected void connectionSuccessful()State machine transition: initialization of a new connection viacreateConnection()
was successful and we are connected to the server.The implementation in
PersistentConnection
does nothing; subclasses may override. -
connectionFailed
State machine transition: initialization of a new connection viacreateConnection()
failed due to an exception. This instance will automatically attempt to retry.The implementation in
PersistentConnection
does nothing; subclasses may override.- Parameters:
e
- Exception thrown bycreateConnection()
.
-
connectionEnded
State machine transition: an established connection to the server has ended. This indicates either normal return or a thrown an exception fromhandleConnection()
. This instance will automatically attempt to reconnect.The implementation in
PersistentConnection
does nothing; subclasses may override.- Parameters:
e
- Exception thrown byhandleConnection()
, or null ifhandleConnection()
returned normally
-