Package org.dellroad.stuff.net
Class PersistentSocketConnection
java.lang.Object
org.dellroad.stuff.net.PersistentConnection<Socket>
org.dellroad.stuff.net.PersistentSocketConnection
Maintains a persistent
Socket connection to a remote server, automatically reconnecting when the connection fails.
Subclasses need only override handleConnection().
-
Field Summary
Fields inherited from class org.dellroad.stuff.net.PersistentConnection
DEFAULT_MAX_RESTART_DELAY, DEFAULT_MIN_RESTART_DELAY, log -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcleanupConnection(Socket socket) Perform cleanup after a server connection ends.protected SocketCreate a new server connection.protected SocketCreate theSocketto use for a new connection.Get the address to which this instance should connect.voidsetServerAddress(SocketAddress serverAddress) Configure the address to which this instance should connect.voidstart()Start this instance.Methods inherited from class org.dellroad.stuff.net.PersistentConnection
connectionEnded, connectionFailed, connectionSuccessful, getMaxRestartDelay, getMinRestartDelay, getThreadName, handleConnection, isRunning, setMaxRestartDelay, setMinRestartDelay, started, stop, stopped
-
Constructor Details
-
PersistentSocketConnection
public PersistentSocketConnection()
-
-
Method Details
-
getServerAddress
Get the address to which this instance should connect.- Returns:
- remote server address
-
setServerAddress
Configure the address to which this instance should connect.Required property.
- Parameters:
serverAddress- remote server address
-
start
public void start()Description copied from class:PersistentConnectionStart this instance. This starts the background thread, which initiates the first connection attempt.If this instance is already started, nothing happens.
- Overrides:
startin classPersistentConnection<Socket>
-
createConnection
Create a new server connection.The implementation in
PersistentSocketConnectioncreates a socket viacreateSocket()and connects the socket to the configured server.- Specified by:
createConnectionin classPersistentConnection<Socket>- Returns:
- connected socket
- Throws:
IOException- if the connection attempt failsInterruptedException- if the current thread is interrupted
-
cleanupConnection
Perform cleanup after a server connection ends.The implementation in
PersistentSocketConnectionensures the socket is closed.- Overrides:
cleanupConnectionin classPersistentConnection<Socket>- Parameters:
socket- socket returned fromcreateConnection()when this connection was created
-
createSocket
Create theSocketto use for a new connection. The socket should not be connected yet.The implementation in
PersistentSocketConnectioncreates a normalSocketand sets the keep-alive flag to ensure connection failures are detected promptly. Subclasses may override to configure the socket differently.- Returns:
- new socket
- Throws:
IOException- if socket creation fails
-