public static class Cluster.Builder extends Object implements Cluster.Initializer
Cluster
instances.Constructor and Description |
---|
Builder() |
Modifier and Type | Method and Description |
---|---|
Cluster.Builder |
addContactPoint(String address)
Adds a contact point.
|
Cluster.Builder |
addContactPoints(Collection<InetAddress> addresses)
Adds contact points.
|
Cluster.Builder |
addContactPoints(InetAddress... addresses)
Adds contact points.
|
Cluster.Builder |
addContactPoints(String... addresses)
Adds contact points.
|
Cluster.Builder |
addContactPoints(String address)
Adds a contact point - or many if it host resolves to multiple
InetAddress s (A records). |
Cluster.Builder |
addContactPointsWithPorts(Collection<InetSocketAddress> addresses)
Adds contact points.
|
Cluster |
build()
Builds the cluster with the configured set of initial contact points
and policies.
|
String |
getClusterName()
An optional name for the created cluster.
|
Configuration |
getConfiguration()
The configuration that will be used for the new cluster.
|
List<InetSocketAddress> |
getContactPoints()
Returns the initial Cassandra hosts to connect to.
|
Collection<Host.StateListener> |
getInitialListeners()
Optional listeners to register against the newly created cluster.
|
Cluster.Builder |
withAddressTranslater(AddressTranslater translater)
Configures the address translater to use for the new cluster.
|
Cluster.Builder |
withAuthProvider(AuthProvider authProvider)
Use the specified AuthProvider when connecting to Cassandra
hosts.
|
Cluster.Builder |
withClusterName(String name)
An optional name for the create cluster.
|
Cluster.Builder |
withCompression(ProtocolOptions.Compression compression)
Sets the compression to use for the transport.
|
Cluster.Builder |
withCredentials(String username,
String password)
Uses the provided credentials when connecting to Cassandra hosts.
|
Cluster.Builder |
withInitialListeners(Collection<Host.StateListener> listeners)
Register the provided listeners in the newly created cluster.
|
Cluster.Builder |
withLoadBalancingPolicy(LoadBalancingPolicy policy)
Configures the load balancing policy to use for the new cluster.
|
Cluster.Builder |
withMaxSchemaAgreementWaitSeconds(int maxSchemaAgreementWaitSeconds)
Sets the maximum time to wait for schema agreement before returning from a DDL query.
|
Cluster.Builder |
withNettyOptions(NettyOptions nettyOptions)
Set the
NettyOptions to use for the newly created Cluster. |
Cluster.Builder |
withoutJMXReporting()
Disables JMX reporting of the metrics.
|
Cluster.Builder |
withoutMetrics()
Disables metrics collection for the created cluster (metrics are
enabled by default otherwise).
|
Cluster.Builder |
withPoolingOptions(PoolingOptions options)
Sets the PoolingOptions to use for the newly created Cluster.
|
Cluster.Builder |
withPort(int port)
The port to use to connect to the Cassandra host.
|
Cluster.Builder |
withProtocolVersion(int version)
The native protocol version to use.
|
Cluster.Builder |
withQueryOptions(QueryOptions options)
Sets the QueryOptions to use for the newly created Cluster.
|
Cluster.Builder |
withReconnectionPolicy(ReconnectionPolicy policy)
Configures the reconnection policy to use for the new cluster.
|
Cluster.Builder |
withRetryPolicy(RetryPolicy policy)
Configures the retry policy to use for the new cluster.
|
Cluster.Builder |
withSocketOptions(SocketOptions options)
Sets the SocketOptions to use for the newly created Cluster.
|
Cluster.Builder |
withSpeculativeExecutionPolicy(SpeculativeExecutionPolicy policy)
Configures the speculative execution policy to use for the new cluster.
|
Cluster.Builder |
withSSL()
Enables the use of SSL for the created
Cluster . |
Cluster.Builder |
withSSL(SSLOptions sslOptions)
Enable the use of SSL for the created
Cluster using the provided options. |
public String getClusterName()
Cluster.Initializer
Such name is optional (a default name will be created otherwise) and is currently
only use for JMX reporting of metrics. See Cluster.getClusterName()
for more
information.
getClusterName
in interface Cluster.Initializer
null
to use an automatically
generated name.public List<InetSocketAddress> getContactPoints()
Cluster.Initializer
getContactPoints
in interface Cluster.Initializer
addContactPoint(java.lang.String)
for more details on contact points.public Cluster.Builder withClusterName(String name)
Note: this is not related to the Cassandra cluster name (though you
are free to provide the same name). See Cluster.getClusterName()
for
details.
If you use this method and create more than one Cluster instance in the same JVM (which should be avoided unless you need to connect to multiple Cassandra clusters), you should make sure each Cluster instance get a unique name or you may have a problem with JMX reporting.
name
- the cluster name to use for the created Cluster instance.public Cluster.Builder withPort(int port)
If not set through this method, the default port (9042) will be used instead.
port
- the port to set.public Cluster.Builder withMaxSchemaAgreementWaitSeconds(int maxSchemaAgreementWaitSeconds)
If not set through this method, the default value (10 seconds) will be used.
maxSchemaAgreementWaitSeconds
- the new value to set.IllegalStateException
- if the provided value is zero or less.public Cluster.Builder withProtocolVersion(int version)
The driver supports both version 1 and 2 of the native protocol. Version 2 of the protocol has more features and should be preferred, but it is only supported by Cassandra 2.0 and above, so you will have to use version 1 with Cassandra 1.2 nodes.
By default, the driver will "auto-detect" which protocol version it can use when connecting to the first node. More precisely, it will try the version 2 first and will fallback to version 1 if it is not supported by that first node it connects to. Please note that once the version is "auto-detected", it won't change: if the first node the driver connects to is a Cassandra 1.2 node and auto-detection is used (the default), then the native protocol version 1 will be use for the lifetime of the Cluster instance.
This method allows to force the use of a particular protocol version. Forcing
version 1 is always fine since all Cassandra version (at least all those
supporting the native protocol in the first place) so far supports it. However,
please note that a number of features of the driver won't be available if that
version of thr protocol is in use, including result set paging,
BatchStatement
, executing a non-prepared query with binary values
(Session.execute(String, Object...)
), ... (those methods will throw
an UnsupportedFeatureException). Using the protocol version 1 should thus
only be considered when using Cassandra 1.2, until nodes have been upgraded
to Cassandra 2.0.
If version 2 of the protocol is used, then Cassandra 1.2 nodes will be ignored (the driver won't connect to them).
The default behavior (auto-detection) is fine in almost all case, but you may want to force a particular version if you have a Cassandra cluster with mixed 1.2/2.0 nodes (i.e. during a Cassandra upgrade).
version
- the native protocol version to use. The versions supported by
this driver are version 1 and 2. Negative values are also supported to trigger
auto-detection (see above) but this is the default (so you don't have to call
this method for that behavior).IllegalArgumentException
- if version
is neither 1, 2 or a
negative value.public Cluster.Builder addContactPoint(String address)
Contact points are addresses of Cassandra nodes that the driver uses to discover the cluster topology. Only one contact point is required (the driver will retrieve the address of the other nodes automatically), but it is usually a good idea to provide more than one contact point, because if that single contact point is unavailable, the driver cannot initialize itself correctly.
Note that by default (that is, unless you use the withLoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy)
)
method of this builder), the first succesfully contacted host will be use
to define the local data-center for the client. If follows that if you are
running Cassandra in a multiple data-center setting, it is a good idea to
only provided contact points that are in the same datacenter than the client,
or to provide manually the load balancing policy that suits your need.
address
- the address of the node to connect toIllegalArgumentException
- if no IP address for address
could be foundSecurityException
- if a security manager is present and
permission to resolve the host name is denied.public Cluster.Builder addContactPoints(String... addresses)
See addContactPoint(java.lang.String)
for more details on contact
points.
addresses
- addresses of the nodes to add as contact point.IllegalArgumentException
- if no IP address for at least one
of addresses
could be foundSecurityException
- if a security manager is present and
permission to resolve the host name is denied.addContactPoint(java.lang.String)
public Cluster.Builder addContactPoints(String address)
InetAddress
s (A records).
If the host name points to a dns records with multiple a-records, all InetAddresses
returned will be used. Make sure that all resulting InetAddress
s returned
points to the same cluster and datacenter.
See addContactPoint(java.lang.String)
for more details on contact
points and thrown exceptions
address
- address of the nodes to look up InetAddresses from to add as contact points.addContactPoint(java.lang.String)
public Cluster.Builder addContactPoints(InetAddress... addresses)
See addContactPoint(java.lang.String)
for more details on contact
points.
addresses
- addresses of the nodes to add as contact point.addContactPoint(java.lang.String)
public Cluster.Builder addContactPoints(Collection<InetAddress> addresses)
addContactPoint(java.lang.String)
for more details on contact
points.addresses
- addresses of the nodes to add as contact pointaddContactPoint(java.lang.String)
public Cluster.Builder addContactPointsWithPorts(Collection<InetSocketAddress> addresses)
See addContactPoint(java.lang.String)
for more details on contact
points. Contrarily to other addContactPoints
methods, this method
allow to provide a different port for each contact points. Since Cassandra
nodes must always all listen on the same port, this is rarelly what you
want and most use should prefer other addContactPoints
methods to
this one. However, this can be useful if the Cassandra nodes are behind
a router and are not accessed directly. Note that if you are in this
situtation (Cassandra nodes are behind a router, not directly accessible),
you almost surely want to provide a specific AddressTranslater
(through withAddressTranslater(com.datastax.driver.core.policies.AddressTranslater)
) to translate actual Cassandra node
addresses to the addresses the driver should use, otherwise the driver
will not be able to auto-detect new nodes (and will generally not function
optimally).
addresses
- addresses of the nodes to add as contact pointaddContactPoint(java.lang.String)
public Cluster.Builder withLoadBalancingPolicy(LoadBalancingPolicy policy)
If no load balancing policy is set through this method,
Policies.defaultLoadBalancingPolicy()
will be used instead.
policy
- the load balancing policy to use.public Cluster.Builder withReconnectionPolicy(ReconnectionPolicy policy)
If no reconnection policy is set through this method,
Policies.DEFAULT_RECONNECTION_POLICY
will be used instead.
policy
- the reconnection policy to use.public Cluster.Builder withRetryPolicy(RetryPolicy policy)
If no retry policy is set through this method,
Policies.DEFAULT_RETRY_POLICY
will be used instead.
policy
- the retry policy to use.public Cluster.Builder withAddressTranslater(AddressTranslater translater)
See AddressTranslater
for more detail on address translation,
but the default translater, IdentityTranslater
, should be
correct in most cases. If unsure, stick to the default.
translater
- the translater to use.public Cluster.Builder withSpeculativeExecutionPolicy(SpeculativeExecutionPolicy policy)
If no policy is set through this method, Policies.defaultSpeculativeExecutionPolicy()
will be used instead.
policy
- the policy to use.public Cluster.Builder withCredentials(String username, String password)
This should be used if the Cassandra cluster has been configured to
use the PasswordAuthenticator
. If the the default AllowAllAuthenticator
is used instead, using this method has no
effect.
username
- the username to use to login to Cassandra hosts.password
- the password corresponding to username
.public Cluster.Builder withAuthProvider(AuthProvider authProvider)
Use this method when a custom authentication scheme is in place.
You shouldn't call both this method and withCredentials
on the same Builder
instance as one will supersede the
other
authProvider
- the AuthProvider
to use to login to
Cassandra hosts.public Cluster.Builder withCompression(ProtocolOptions.Compression compression)
compression
- the compression to set.ProtocolOptions.Compression
public Cluster.Builder withoutMetrics()
public Cluster.Builder withSSL()
Cluster
.
Calling this method will use default SSL options (see SSLOptions.SSLOptions()
).
This is thus a shortcut for withSSL(new SSLOptions())
.
Note that if SSL is enabled, the driver will not connect to any Cassandra nodes that doesn't have SSL enabled and it is strongly advised to enable SSL on every Cassandra node if you plan on using SSL in the driver.
public Cluster.Builder withSSL(SSLOptions sslOptions)
Cluster
using the provided options.sslOptions
- the SSL options to use.public Cluster.Builder withInitialListeners(Collection<Host.StateListener> listeners)
Note: repeated calls to this method will override the previous ones.
listeners
- the listeners to register.public Cluster.Builder withoutJMXReporting()
JMX reporting is enabled by default (see Metrics
) but can be
disabled using this option. If metrics are disabled, this is a
no-op.
public Cluster.Builder withPoolingOptions(PoolingOptions options)
If no pooling options are set through this method, default pooling options will be used.
options
- the pooling options to use.public Cluster.Builder withSocketOptions(SocketOptions options)
If no socket options are set through this method, default socket options will be used.
options
- the socket options to use.public Cluster.Builder withQueryOptions(QueryOptions options)
If no query options are set through this method, default query options will be used.
options
- the query options to use.public Cluster.Builder withNettyOptions(NettyOptions nettyOptions)
NettyOptions
to use for the newly created Cluster.
If no Netty options are set through this method, NettyOptions.DEFAULT_INSTANCE
will be used as a default value, which means that no customization will be applied.
nettyOptions
- the NettyOptions
to use.public Configuration getConfiguration()
You should not modify this object directly because changes made
to the returned object may not be used by the cluster build.
Instead, you should use the other methods of this Builder
.
getConfiguration
in interface Cluster.Initializer
public Collection<Host.StateListener> getInitialListeners()
Cluster.Initializer
Note that contrary to listeners registered post Cluster creation,
the listeners returned by this method will see Host.StateListener.onAdd(com.datastax.driver.core.Host)
events for the initial contact points.
getInitialListeners
in interface Cluster.Initializer
Host.StateListener
to register
against the newly created cluster.public Cluster build()
This is a convenience method for Cluster.buildFrom(this)
.
Copyright © 2012–2015. All rights reserved.