public class Cluster extends Object
This is the main entry point of the driver. A simple example of access to a Cassandra cluster would be:
Cluster cluster = Cluster.builder().addContactPoint("192.168.0.1").build(); Session session = cluster.connect("db1"); for (Row row : session.execute("SELECT * FROM table1")) // do something ...
A cluster object maintains a permanent connection to one of the cluster nodes which it uses solely to maintain information on the state and current topology of the cluster. Using the connection, the driver will discover all the nodes currently in the cluster as well as new nodes joining the cluster subsequently.
Modifier and Type | Class and Description |
---|---|
static class |
Cluster.Builder
Helper class to build
Cluster instances. |
static interface |
Cluster.Initializer
Initializer for
Cluster instances. |
Modifier | Constructor and Description |
---|---|
protected |
Cluster(List<InetAddress> contactPoints,
Configuration configuration,
boolean init)
Constructs a new Cluster instance.
|
Modifier and Type | Method and Description |
---|---|
static Cluster.Builder |
builder()
Creates a new
Cluster.Builder instance. |
static Cluster |
buildFrom(Cluster.Initializer initializer)
Build a new cluster based on the provided initializer.
|
Session |
connect()
Creates a new session on this cluster.
|
Session |
connect(String keyspace)
Creates a new session on this cluster and sets the keyspace to the provided one.
|
Configuration |
getConfiguration()
The cluster configuration.
|
Metadata |
getMetadata()
Returns read-only metadata on the connected cluster.
|
Metrics |
getMetrics()
The cluster metrics.
|
boolean |
isShutdown()
Whether shutdown has been called on this Cluster instance.
|
Cluster |
register(Host.StateListener listener)
Registers the provided listener to be notified on hosts
up/down/added/removed events.
|
Cluster |
register(LatencyTracker tracker)
Registers the provided tracker to be updated with hosts read
latencies.
|
void |
shutdown()
Shuts down this cluster instance.
|
boolean |
shutdown(long timeout,
TimeUnit unit)
Shutdown this cluster instance, only waiting a definite amount of time.
|
Cluster |
unregister(Host.StateListener listener)
Unregisters the provided listener from being notified on hosts events.
|
Cluster |
unregister(LatencyTracker tracker)
Unregisters the provided latency tracking from being updated
with host read latencies.
|
protected Cluster(List<InetAddress> contactPoints, Configuration configuration, boolean init)
This constructor is mainly exposed so Cluster can be sub-classed as a mean to make testing/mocking
easier or to "intecept" it's method call. Most users shouldn't extend this class however and
should prefer either using the builder()
or calling buildFrom(com.datastax.driver.core.Cluster.Initializer)
with a custom
Initializer.
contactPoints
- the list of contact points to use for the new cluster.configuration
- the configuration for the new cluster.init
- whether or not initialization should be perform by this constructor. Passing
false
is equivalent to using Cluster.Builder.withDeferredInitialization()
on a
Cluster.Builder
.public static Cluster buildFrom(Cluster.Initializer initializer)
Note that for building a cluster programmatically, Cluster.Builder
provides a slightly less verbose shortcut with Cluster.Builder.build()
.
Also note that that all the contact points provided by initializer
must share the same port.
initializer
- the Cluster.Initializer to useNoHostAvailableException
- if no host amongst the contact points
can be reached.IllegalArgumentException
- if the list of contact points provided
by initializer
is empty or if not all those contact points have the same port.AuthenticationException
- if an authentication error occurs
while contacting the initial contact points.public static Cluster.Builder builder()
Cluster.Builder
instance.
This is a convenenience method for new Cluster.Builder()
.
public Session connect()
public Session connect(String keyspace)
keyspace
- The name of the keyspace to use for the created
Session
.keyspaceName
.NoHostAvailableException
- if no host can be contacted to set the
keyspace
.public Metadata getMetadata()
This includes the known nodes with their status as seen by the driver, as well as the schema definitions.
public Configuration getConfiguration()
public Metrics getMetrics()
null
if metrics collection has
been disabled (that is if Configuration.getMetricsOptions()
returns null
).public Cluster register(Host.StateListener listener)
Registering the same listener multiple times is a no-op.
Note that while LoadBalancingPolicy
implements
Host.StateListener
, the configured load balancy does not
need to (and should not) be registered through this method to
received host related events.
listener
- the new Host.StateListener
to register.Cluster
object;public Cluster unregister(Host.StateListener listener)
This method is a no-op if listener
hadn't previously be
registered against this Cluster.
listener
- the Host.StateListener
to unregister.Cluster
object;public Cluster register(LatencyTracker tracker)
Registering the same listener multiple times is a no-op.
Be warry that the registered tracker update
method will be call
very frequently (at the end of every query to a Cassandra host) and
should thus not be costly.
The main use case for a LatencyTracker
is so
LoadBalancingPolicy
can implement latency awareness
Typically, LatencyAwarePolicy
registers it's own internal
LatencyTracker
(automatically, you don't have to call this
method directly).
tracker
- the new LatencyTracker
to register.Cluster
object;public Cluster unregister(LatencyTracker tracker)
This method is a no-op if tracker
hadn't previously be
registered against this Cluster.
tracker
- the LatencyTracker
to unregister.Cluster
object;public void shutdown()
Cluster
instance and reclaims all resources used by it.
This method waits indefinitely for the driver to shut down.
This method has no effect if the cluster was already shut down.
public boolean shutdown(long timeout, TimeUnit unit)
Cluster
instance and reclaim all resources used by it.
Note that this method is not thread safe in the sense that if another
shutdown is perform in parallel, it might return true
even if
the instance is not yet fully shutdown.
timeout
- how long to wait for the cluster instance to shutdown.unit
- the unit for the timeout.true
if the instance has been properly shutdown within
the timeout
, false
otherwise.public boolean isShutdown()
true
if shutdown
has been called on this instance,
false
otherwise.Copyright © 2014. All Rights Reserved.