public abstract class DelegatingCluster extends Cluster
Cluster
implementations that wrap another instance (delegate /
decorator pattern).Cluster.Builder, Cluster.EnhancedInitializer, Cluster.Initializer
Modifier | Constructor and Description |
---|---|
protected |
DelegatingCluster()
Builds a new instance.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Initiates a shutdown of this cluster instance and blocks until that shutdown completes.
|
CloseFuture |
closeAsync()
Initiates a shutdown of this cluster instance.
|
Session |
connect()
Creates a new session on this cluster and initialize it.
|
Session |
connect(String keyspace)
Creates a new session on this cluster, initialize it and sets the keyspace to the provided one.
|
ListenableFuture<Session> |
connectAsync()
Creates a new session on this cluster and initializes it asynchronously.
|
ListenableFuture<Session> |
connectAsync(String keyspace)
Creates a new session on this cluster, and initializes it to the given keyspace asynchronously.
|
protected abstract Cluster |
delegate()
Returns the delegate instance where all calls will be forwarded.
|
Configuration |
getConfiguration()
The cluster configuration.
|
UUID |
getId() |
Metadata |
getMetadata()
Returns read-only metadata on the connected cluster.
|
Metrics |
getMetrics()
The cluster metrics.
|
Cluster |
init()
Initialize this Cluster instance.
|
boolean |
isClosed()
Whether this Cluster instance has been closed.
|
Session |
newSession()
Creates a new session on this cluster but does not initialize it.
|
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.
|
Cluster |
register(SchemaChangeListener listener)
Registers the provided listener to be updated with schema change events.
|
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.
|
Cluster |
unregister(SchemaChangeListener listener)
Unregisters the provided schema change listener from being updated with schema change events.
|
builder, buildFrom, getClusterName, getDriverVersion, logDriverVersion
protected abstract Cluster delegate()
public Cluster init()
Cluster
This method creates an initial connection to one of the contact points used to construct the
Cluster
instance. That connection is then used to populate the cluster Metadata
.
Calling this method is optional in the sense that any call to one of the connect
methods of this object will automatically trigger a call to this method beforehand. It is thus
only useful to call this method if for some reason you want to populate the metadata (or test
that at least one contact point can be reached) without creating a first Session
.
Please note that this method only creates one control connection for gathering cluster
metadata. In particular, it doesn't create any connection pools. Those are created when a new
Session
is created through connect
.
This method has no effect if the cluster is already initialized.
public Session newSession()
Cluster
Because this method does not perform any initialization, it cannot fail. The initialization
of the session (the connection of the Session to the Cassandra nodes) will occur if either the
Session.init()
method is called explicitly, or whenever the returned session object is
used.
Once a session returned by this method gets initialized (see above), it will be set to no keyspace. If you want to set such session to a keyspace, you will have to explicitly execute a 'USE mykeyspace' query.
Note that if you do not particularly need to defer initialization, it is simpler to use one
of the connect()
method of this class.
newSession
in class Cluster
public Session connect()
Cluster
Note that this method will initialize the newly created session, trying to connect to the
Cassandra nodes before returning. If you only want to create a Session object without
initializing it right away, see Cluster.newSession()
.
public Session connect(String keyspace)
Cluster
Note that this method will initialize the newly created session, trying to connect to the
Cassandra nodes before returning. If you only want to create a Session object without
initializing it right away, see Cluster.newSession()
.
public ListenableFuture<Session> connectAsync()
Cluster
This will also initialize the Cluster
if needed; note that cluster initialization
happens synchronously on the thread that called this method. Therefore it is recommended to
initialize the cluster at application startup, and not rely on this method to do it.
connectAsync
in class Cluster
Cluster.connect()
public ListenableFuture<Session> connectAsync(String keyspace)
Cluster
This will also initialize the Cluster
if needed; note that cluster initialization
happens synchronously on the thread that called this method. Therefore it is recommended to
initialize the cluster at application startup, and not rely on this method to do it.
connectAsync
in class Cluster
keyspace
- The name of the keyspace to use for the created Session
.public Metadata getMetadata()
Cluster
This includes the known nodes with their status as seen by the driver, as well as the schema
definitions. Since this return metadata on the connected cluster, this method may trigger the
creation of a connection if none has been established yet (neither init()
nor connect()
has been called yet).
getMetadata
in class Cluster
public Configuration getConfiguration()
Cluster
getConfiguration
in class Cluster
public Metrics getMetrics()
Cluster
getMetrics
in class Cluster
null
if this cluster has not yet been initialized
, or if metrics collection has been disabled (that is if Configuration.getMetricsOptions()
returns null
).public Cluster register(Host.StateListener listener)
Cluster
Registering the same listener multiple times is a no-op.
This method should be used to register additional listeners on an already-initialized
cluster. To add listeners to a cluster object prior to its initialization, use Cluster.Builder.withInitialListeners(Collection)
. Calling this method on a non-initialized cluster
will result in the listener being notified
twice of cluster
registration: once inside this method, and once at cluster initialization.
register
in class Cluster
listener
- the new Host.StateListener
to register.Cluster
object;public Cluster unregister(Host.StateListener listener)
Cluster
This method is a no-op if listener
hasn't previously been registered against this
Cluster.
unregister
in class Cluster
listener
- the Host.StateListener
to unregister.Cluster
object;public Cluster register(LatencyTracker tracker)
Cluster
Registering the same tracker multiple times is a no-op.
Beware that the registered tracker's update
method will be called 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 to allow load balancing policies to
implement latency awareness. For example, LatencyAwarePolicy
registers it's own
internal LatencyTracker
(automatically, you don't have to call this method directly).
register
in class Cluster
tracker
- the new LatencyTracker
to register.Cluster
object;public Cluster unregister(LatencyTracker tracker)
Cluster
This method is a no-op if tracker
hasn't previously been registered against this
Cluster.
unregister
in class Cluster
tracker
- the LatencyTracker
to unregister.Cluster
object;public Cluster register(SchemaChangeListener listener)
Cluster
Registering the same listener multiple times is a no-op.
register
in class Cluster
listener
- the new SchemaChangeListener
to register.Cluster
object;public Cluster unregister(SchemaChangeListener listener)
Cluster
This method is a no-op if listener
hasn't previously been registered against this
Cluster.
unregister
in class Cluster
listener
- the SchemaChangeListener
to unregister.Cluster
object;public CloseFuture closeAsync()
Cluster
This method is asynchronous and return a future on the completion of the shutdown process. As soon a the cluster is shutdown, no new request will be accepted, but already submitted queries are allowed to complete. This method closes all connections from all sessions and reclaims all resources used by this Cluster instance.
If for some reason you wish to expedite this process, the CloseFuture.force()
can be
called on the result future.
This method has no particular effect if the cluster was already closed (in which case the returned future will return immediately).
closeAsync
in class Cluster
public void close()
Cluster
This method is a shortcut for closeAsync().get()
.
public boolean isClosed()
Cluster
Note that this method returns true as soon as one of the close methods (Cluster.closeAsync()
or Cluster.close()
) has been called, it does not guarantee that the closing is done. If you want
to guarantee that the closing is done, you can call close()
and wait until it returns
(or call the get method on closeAsync()
with a very short timeout and check this
doesn't timeout).
Copyright © 2012–2019. All rights reserved.