public class WhiteListPolicy extends Object implements LoadBalancingPolicy
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<InetAddress> 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 |
---|---|
HostDistance |
distance(Host host)
Return the HostDistance for the provided host.
|
void |
init(Cluster cluster,
Collection<Host> hosts)
Initialize this load balancing policy.
|
Iterator<Host> |
newQueryPlan(Query query)
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 |
onUp(Host host)
Called when a node is determined to be up.
|
public WhiteListPolicy(LoadBalancingPolicy childPolicy, Collection<InetAddress> 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 void init(Cluster cluster, Collection<Host> hosts)
init
in interface LoadBalancingPolicy
cluster
- 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 LoadBalancingPolicy
host
- 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(Query query)
It is guaranteed that only hosts from the white list will be returned.
newQueryPlan
in interface LoadBalancingPolicy
query
- the query for which to build the plan.public void onUp(Host host)
Host.StateListener
onUp
in interface Host.StateListener
host
- the host that has been detected up.public void onDown(Host host)
Host.StateListener
onDown
in interface Host.StateListener
host
- the host that has been detected down.public void onAdd(Host host)
Host.StateListener
The newly added node should be considered up.
onAdd
in interface Host.StateListener
host
- the host that has been newly added.public void onRemove(Host host)
Host.StateListener
onRemove
in interface Host.StateListener
host
- the removed host.Copyright © 2014. All Rights Reserved.