Class 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)
    • Field Detail

      • logger

        protected static final Logger logger
    • Constructor Detail

      • ReconnectingSocket

        public ReconnectingSocket​(String host,
                                  int port,
                                  SocketFactory socketFactory,
                                  @Nullable
                                  Long connectionTimeToLiveMillis,
                                  @Nullable
                                  Supplier<Long> timeSupplier)
                           throws IOException
        Parameters:
        host - Hostname to connect to
        port - Port to connect to
        socketFactory - SocketFactory used to instantiate new sockets
        connectionTimeToLiveMillis - 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.
    • 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 IOException
        Flushes the outputStream best-effort. If that fails, we reset the connection.
        Throws:
        IOException