Class StaticHostProvider

  • All Implemented Interfaces:
    HostProvider

    @Public
    public final class StaticHostProvider
    extends java.lang.Object
    implements HostProvider
    Most simple HostProvider, resolves on every next() call. Please be aware that although this class doesn't do any DNS caching, there're multiple levels of caching already present across the stack like in JVM, OS level, hardware, etc. The best we could do here is to get the most recent address from the underlying system which is considered up-to-date.
    • Constructor Summary

      Constructors 
      Constructor Description
      StaticHostProvider​(java.util.Collection<java.net.InetSocketAddress> serverAddresses)
      Constructs a SimpleHostSet.
      StaticHostProvider​(java.util.Collection<java.net.InetSocketAddress> serverAddresses, StaticHostProvider.Resolver resolver)
      Introduced for testing purposes.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.net.InetSocketAddress next​(long spinDelay)
      The next host to try to connect to.
      void onConnected()
      Notify the HostProvider of a successful connection.
      int size()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • StaticHostProvider

        public StaticHostProvider​(java.util.Collection<java.net.InetSocketAddress> serverAddresses)
        Constructs a SimpleHostSet.
        Parameters:
        serverAddresses - possibly unresolved ZooKeeper server addresses
        Throws:
        java.lang.IllegalArgumentException - if serverAddresses is empty or resolves to an empty list
      • StaticHostProvider

        public StaticHostProvider​(java.util.Collection<java.net.InetSocketAddress> serverAddresses,
                                  StaticHostProvider.Resolver resolver)
        Introduced for testing purposes. getAllByName() is a static method of InetAddress, therefore cannot be easily mocked. By abstraction of Resolver interface we can easily inject a mocked implementation in tests.
        Parameters:
        serverAddresses - possibly unresolved ZooKeeper server addresses
        resolver - custom resolver implementation
        Throws:
        java.lang.IllegalArgumentException - if serverAddresses is empty or resolves to an empty list
    • Method Detail

      • next

        public java.net.InetSocketAddress next​(long spinDelay)
        Description copied from interface: HostProvider
        The next host to try to connect to. For a spinDelay of 0 there should be no wait.
        Specified by:
        next in interface HostProvider
        Parameters:
        spinDelay - Milliseconds to wait if all hosts have been tried once.
        Returns:
        The next host to try to connect to with resolved address. If the host is not resolvable, the unresolved address will be returned.
      • onConnected

        public void onConnected()
        Description copied from interface: HostProvider
        Notify the HostProvider of a successful connection. The HostProvider may use this notification to reset it's inner state.
        Specified by:
        onConnected in interface HostProvider