Package org.apache.zookeeper.client
Class StaticHostProvider
java.lang.Object
org.apache.zookeeper.client.StaticHostProvider
- All Implemented Interfaces:
AutoCloseable,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.
It uses HostConnectionManager for all connection management and reconfiguration logic.
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionStaticHostProvider(Collection<InetSocketAddress> serverAddresses) Constructs a SimpleHostSet.StaticHostProvider(Collection<InetSocketAddress> serverAddresses, long randomnessSeed) Constructs a SimpleHostSet.StaticHostProvider(Collection<InetSocketAddress> serverAddresses, StaticHostProvider.Resolver resolver) Constructs a SimpleHostSet.StaticHostProvider(Collection<InetSocketAddress> serverAddresses, ZKClientConfig clientConfig) Constructs a SimpleHostSet with ZKClientConfig. -
Method Summary
Modifier and TypeMethodDescriptiongetServerAtIndex(int i) next(long spinDelay) The next host to try to connect to.voidNotify the HostProvider of a successful connection.intsize()booleanupdateServerList(Collection<InetSocketAddress> serverAddresses, InetSocketAddress currentHost) Update the list of servers.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.zookeeper.client.HostProvider
close
-
Constructor Details
-
StaticHostProvider
Constructs a SimpleHostSet.- Parameters:
serverAddresses- possibly unresolved ZooKeeper server addresses- Throws:
IllegalArgumentException- if serverAddresses is empty or resolves to an empty list
-
StaticHostProvider
public StaticHostProvider(Collection<InetSocketAddress> serverAddresses, ZKClientConfig clientConfig) Constructs a SimpleHostSet with ZKClientConfig. Introduced this new overload in 3.10.0 in order to take advantage of some newly introduced feature flags. Like the shuffle (old) / not to shuffle (new) behavior of DNS resolution.- Parameters:
serverAddresses- possibly unresolved ZooKeeper server addressesclientConfig- ZooKeeper client configuration
-
StaticHostProvider
public StaticHostProvider(Collection<InetSocketAddress> serverAddresses, StaticHostProvider.Resolver resolver) Constructs a SimpleHostSet. 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 addressesresolver- custom resolver implementation
-
StaticHostProvider
Constructs a SimpleHostSet. This constructor is used from StaticHostProviderTest to produce deterministic test results by initializing sourceOfRandomness with the same seed- Parameters:
serverAddresses- possibly unresolved ZooKeeper server addressesrandomnessSeed- a seed used to initialize sourceOfRandomness- Throws:
IllegalArgumentException- if serverAddresses is empty or resolves to an empty list
-
-
Method Details
-
updateServerList
public boolean updateServerList(Collection<InetSocketAddress> serverAddresses, InetSocketAddress currentHost) Description copied from interface:HostProviderUpdate the list of servers. This returns true if changing connections is necessary for load-balancing, false otherwise.- Specified by:
updateServerListin interfaceHostProvider- Parameters:
serverAddresses- new host listcurrentHost- the host to which this client is currently connected- Returns:
- true if changing connections is necessary for load-balancing, false otherwise
-
size
public int size()- Specified by:
sizein interfaceHostProvider
-
next
Description copied from interface:HostProviderThe next host to try to connect to. For a spinDelay of 0 there should be no wait.- Specified by:
nextin interfaceHostProvider- Parameters:
spinDelay- Milliseconds to wait if all hosts have been tried once.
-
onConnected
public void onConnected()Description copied from interface:HostProviderNotify the HostProvider of a successful connection. The HostProvider may use this notification to reset it's inner state.- Specified by:
onConnectedin interfaceHostProvider
-
getServerAtIndex
-
getServerAtCurrentIndex
-