public class WhiteListPolicy extends Object implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy
This policy wraps another load balancing policy and will delegate the choice
of hosts to the wrapped policy with the exception that only hosts contained
in the white list provided when constructing this policy will ever be
returned. Any host not in the while list will be considered IGNORED
and thus will not be connected to.
This policy can be useful to ensure that the driver only connects to a predefined set of hosts. Keep in mind however that this policy defeats somewhat the host auto-detection of the driver. As such, this policy is only useful in a few special cases or for testing, but is not optimal in general. If all you want to do is limiting connections to hosts of the local data-center then you should use DCAwareRoundRobinPolicy and *not* this policy in particular.
| Constructor and Description |
|---|
WhiteListPolicy(LoadBalancingPolicy childPolicy,
Collection<InetSocketAddress> whiteList)
Create a new policy that wraps the provided child policy but only "allow" hosts
from the provided while list.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Gets invoked at cluster shutdown.
|
HostDistance |
distance(Host host)
Return the HostDistance for the provided host.
|
LoadBalancingPolicy |
getChildPolicy()
Returns the child policy.
|
void |
init(Cluster cluster,
Collection<Host> hosts)
Initialize this load balancing policy.
|
Iterator<Host> |
newQueryPlan(String loggedKeyspace,
Statement statement)
Returns the hosts to use for a new query.
|
void |
onAdd(Host host)
Called when a new node is added to the cluster.
|
void |
onDown(Host host)
Called when a node is determined to be down.
|
void |
onRemove(Host host)
Called when a node is removed from the cluster.
|
void |
onSuspected(Host host) |
void |
onUp(Host host)
Called when a node is determined to be up.
|
public WhiteListPolicy(LoadBalancingPolicy childPolicy, Collection<InetSocketAddress> whiteList)
childPolicy - the wrapped policy.whiteList - the white listed hosts. Only hosts from this list may get connected
to (whether they will get connected to or not depends on the child policy).public LoadBalancingPolicy getChildPolicy()
ChainableLoadBalancingPolicygetChildPolicy in interface ChainableLoadBalancingPolicypublic void init(Cluster cluster, Collection<Host> hosts)
init in interface LoadBalancingPolicycluster - the Cluster instance for which the policy is created.hosts - the initial hosts to use.IllegalArgumentException - if none of the host in hosts
(which will correspond to the contact points) are part of the white list.public HostDistance distance(Host host)
distance in interface LoadBalancingPolicyhost - the host of which to return the distance of.HostDistance.IGNORED if host is not part of the white list, the HostDistance
as returned by the wrapped policy otherwise.public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement)
It is guaranteed that only hosts from the white list will be returned.
newQueryPlan in interface LoadBalancingPolicyloggedKeyspace - the currently logged keyspace (the one set through either
Cluster.connect(String) or by manually doing a USE query) for
the session on which this plan need to be built. This can be null if
the corresponding session has no keyspace logged in.statement - the query for which to build a plan.public void onUp(Host host)
Host.StateListeneronUp in interface Host.StateListenerhost - the host that has been detected up.public void onSuspected(Host host)
onSuspected in interface Host.StateListenerpublic void onDown(Host host)
Host.StateListeneronDown in interface Host.StateListenerhost - the host that has been detected down.public void onAdd(Host host)
Host.StateListenerThe newly added node should be considered up.
onAdd in interface Host.StateListenerhost - the host that has been newly added.public void onRemove(Host host)
Host.StateListeneronRemove in interface Host.StateListenerhost - the removed host.public void close()
CloseableLoadBalancingPolicyclose in interface CloseableLoadBalancingPolicyCopyright © 2012–2015. All rights reserved.