Package com.wavefront.metrics
Class ReconnectingSocket
- java.lang.Object
-
- com.wavefront.metrics.ReconnectingSocket
-
public class ReconnectingSocket extends Object
Creates a TCP client suitable for the WF proxy. That is: a client which is long-lived and semantically one-way. This client tries persistently to reconnect to the given host and port if a connection is ever broken. If the server (in practice, the WF proxy) sends a TCP FIN or TCP RST, we will treat it as a "broken connection" and just try to connect again on the next call to write(). This means each ReconnectingSocket has a polling thread for the server to listen for connection resets.- Author:
- Mori Bellamy (mori@wavefront.com)
-
-
Constructor Summary
Constructors Constructor Description ReconnectingSocket(String host, int port)ReconnectingSocket(String host, int port, SocketFactory socketFactory)ReconnectingSocket(String host, int port, SocketFactory socketFactory, Long connectionTimeToLiveMillis, Supplier<Long> timeSupplier)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidflush()Flushes the outputStream best-effort.voidwrite(String message)Try to send the given message.
-
-
-
Field Detail
-
logger
protected static final Logger logger
-
-
Constructor Detail
-
ReconnectingSocket
public ReconnectingSocket(String host, int port, SocketFactory socketFactory) throws IOException
- Throws:
IOException- When we cannot open the remote socket.
-
ReconnectingSocket
public ReconnectingSocket(String host, int port, SocketFactory socketFactory, @Nullable Long connectionTimeToLiveMillis, @Nullable Supplier<Long> timeSupplier) throws IOException
- Parameters:
host- Hostname to connect toport- Port to connect tosocketFactory- SocketFactory used to instantiate new socketsconnectionTimeToLiveMillis- Connection TTL, with expiration checked after each flush. When null, TTL is not enforced.timeSupplier- Get current timestamp in millis- Throws:
IOException- When we cannot open the remote socket.
-
ReconnectingSocket
public ReconnectingSocket(String host, int port) throws IOException
- Throws:
IOException
-
-
Method Detail
-
write
public void write(String message) throws Exception
Try to send the given message. On failure, reset and try again. If _that_ fails, just rethrow the exception.- Throws:
Exception- when a single retry is not enough to have a successful write to the remote host.
-
flush
public void flush() throws IOExceptionFlushes the outputStream best-effort. If that fails, we reset the connection.- Throws:
IOException
-
close
public void close() throws IOException- Throws:
IOException
-
-