@NotThreadSafe public abstract class SessionBuilder<SelfT extends SessionBuilder,SessionT> extends Object
You only need to deal with this directly if you use custom driver extensions. For the default
session implementation, see CqlSession.builder()
.
Modifier and Type | Field and Description |
---|---|
protected DriverConfigLoader |
configLoader |
protected CqlIdentifier |
keyspace |
protected Set<EndPoint> |
programmaticContactPoints |
protected RequestTracker |
requestTracker |
protected SelfT |
self |
protected List<TypeCodec<?>> |
typeCodecs |
Constructor and Description |
---|
SessionBuilder() |
Modifier and Type | Method and Description |
---|---|
SelfT |
addContactEndPoint(EndPoint contactPoint)
Adds a contact point to use for the initial connection to the cluster.
|
SelfT |
addContactEndPoints(Collection<EndPoint> contactPoints)
Adds contact points to use for the initial connection to the cluster.
|
SelfT |
addContactPoint(InetSocketAddress contactPoint)
Adds a contact point to use for the initial connection to the cluster.
|
SelfT |
addContactPoints(Collection<InetSocketAddress> contactPoints)
Adds contact points to use for the initial connection to the cluster.
|
SelfT |
addTypeCodecs(TypeCodec<?>... typeCodecs)
Registers additional codecs for custom type mappings.
|
SessionT |
build()
Convenience method to call
buildAsync() and block on the result. |
CompletionStage<SessionT> |
buildAsync()
Creates the session with the options set by this builder.
|
protected DriverContext |
buildContext(DriverConfigLoader configLoader,
List<TypeCodec<?>> typeCodecs,
NodeStateListener nodeStateListener,
SchemaChangeListener schemaChangeListener,
RequestTracker requestTracker,
Map<String,String> localDatacenters,
Map<String,Predicate<Node>> nodeFilters,
ClassLoader classLoader)
This must return an instance of
InternalDriverContext (it's not expressed
directly in the signature to avoid leaking that type through the protected API). |
protected CompletionStage<CqlSession> |
buildDefaultSessionAsync() |
protected DriverConfigLoader |
defaultConfigLoader() |
SelfT |
withClassLoader(ClassLoader classLoader)
The
ClassLoader to use to reflectively load class names defined in configuration. |
SelfT |
withConfigLoader(DriverConfigLoader configLoader)
Sets the configuration loader to use.
|
SelfT |
withKeyspace(CqlIdentifier keyspace)
Sets the keyspace to connect the session to.
|
SelfT |
withKeyspace(String keyspaceName)
Shortcut for
setKeyspace(CqlIdentifier.fromCql(keyspaceName)) |
SelfT |
withLocalDatacenter(String localDatacenter)
Alias to
withLocalDatacenter(String, String) for the default profile. |
SelfT |
withLocalDatacenter(String profileName,
String localDatacenter)
Specifies the datacenter that is considered "local" by the load balancing policy.
|
SelfT |
withNodeFilter(Predicate<Node> nodeFilter)
Alias to
withNodeFilter(String, Predicate) for the default profile. |
SelfT |
withNodeFilter(String profileName,
Predicate<Node> nodeFilter)
Adds a custom filter to include/exclude nodes for a particular execution profile.
|
SelfT |
withNodeStateListener(NodeStateListener nodeStateListener)
Registers a node state listener to use with the session.
|
SelfT |
withRequestTracker(RequestTracker requestTracker)
Register a request tracker to use with the session.
|
SelfT |
withSchemaChangeListener(SchemaChangeListener schemaChangeListener)
Registers a schema change listener to use with the session.
|
protected abstract SessionT |
wrap(CqlSession defaultSession) |
protected final SelfT extends SessionBuilder self
protected DriverConfigLoader configLoader
protected RequestTracker requestTracker
protected CqlIdentifier keyspace
@NonNull public SelfT withConfigLoader(@Nullable DriverConfigLoader configLoader)
If you don't call this method, the builder will use the default implementation, based on the Typesafe config library. More precisely, configuration properties are loaded and merged from the following (first-listed are higher priority):
application.conf
(all resources on classpath with this name)
application.json
(all resources on classpath with this name)
application.properties
(all resources on classpath with this name)
reference.conf
(all resources on classpath with this name). In particular, this
will load the reference.conf
included in the core driver JAR, that defines
default options for all mandatory options.
datastax-java-driver
section.
This default loader will honor the reload interval defined by the option basic.config-reload-interval
.
@NonNull protected DriverConfigLoader defaultConfigLoader()
@NonNull public SelfT addContactPoints(@NonNull Collection<InetSocketAddress> contactPoints)
These 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.
Contact points can also be provided statically in the configuration. If both are specified, they will be merged. If both are absent, the driver will default to 127.0.0.1:9042.
Contrary to the configuration, DNS names with multiple A-records will not be handled here.
If you need that, extract them manually with InetAddress.getAllByName(String)
before calling this method. Similarly, if you need connect addresses to stay unresolved, make
sure you pass unresolved instances here (see advanced.resolve-contact-points
in the
configuration for more explanations).
@NonNull public SelfT addContactPoint(@NonNull InetSocketAddress contactPoint)
addContactPoints(Collection)
@NonNull public SelfT addContactEndPoints(@NonNull Collection<EndPoint> contactPoints)
You only need this method if you use a custom EndPoint
implementation. Otherwise,
use addContactPoints(Collection)
.
@NonNull public SelfT addContactEndPoint(@NonNull EndPoint contactPoint)
You only need this method if you use a custom EndPoint
implementation. Otherwise,
use addContactPoint(InetSocketAddress)
.
@NonNull public SelfT addTypeCodecs(@NonNull TypeCodec<?>... typeCodecs)
typeCodecs
- neither the individual codecs, nor the vararg array itself, can be null
.@NonNull public SelfT withNodeStateListener(@Nullable NodeStateListener nodeStateListener)
If the listener is specified programmatically with this method, it overrides the
configuration (that is, the metadata.node-state-listener.class
option will be ignored).
@NonNull public SelfT withSchemaChangeListener(@Nullable SchemaChangeListener schemaChangeListener)
If the listener is specified programmatically with this method, it overrides the
configuration (that is, the metadata.schema-change-listener.class
option will be
ignored).
@NonNull public SelfT withRequestTracker(@Nullable RequestTracker requestTracker)
If the tracker is specified programmatically with this method, it overrides the
configuration (that is, the request.tracker.class
option will be ignored).
public SelfT withLocalDatacenter(@NonNull String profileName, @NonNull String localDatacenter)
This is a programmatic alternative to the configuration option basic.load-balancing-policy.local-datacenter
. If this method is used, it takes precedence and
overrides the configuration.
Note that this setting may or may not be relevant depending on the load balancing policy
implementation in use. The driver's built-in DefaultLoadBalancingPolicy
relies on it;
if you use a third-party implementation, refer to their documentation.
public SelfT withLocalDatacenter(@NonNull String localDatacenter)
withLocalDatacenter(String, String)
for the default profile.@NonNull public SelfT withNodeFilter(@NonNull String profileName, @NonNull Predicate<Node> nodeFilter)
The predicate's test()
method will be invoked each time the
LoadBalancingPolicy
processes a topology or state change: if it returns false, the
policy will suggest distance IGNORED (meaning the driver won't ever connect to it if all
policies agree), and never included in any query plan.
Note that this behavior is implemented in the default load balancing policy. If you use a custom policy implementation, you'll need to explicitly invoke the filter.
If the filter is specified programmatically with this method, it overrides the configuration
(that is, the load-balancing-policy.filter.class
option will be ignored).
withNodeFilter(Predicate)
@NonNull public SelfT withNodeFilter(@NonNull Predicate<Node> nodeFilter)
withNodeFilter(String, Predicate)
for the default profile.@NonNull public SelfT withKeyspace(@Nullable CqlIdentifier keyspace)
Note that this can also be provided by the configuration; if both are defined, this method takes precedence.
@NonNull public SelfT withKeyspace(@Nullable String keyspaceName)
setKeyspace(CqlIdentifier.fromCql(keyspaceName))
@NonNull public SelfT withClassLoader(@Nullable ClassLoader classLoader)
ClassLoader
to use to reflectively load class names defined in configuration.
This is typically only needed when using OSGi or other in environments where there are complex class loading requirements.
If null, the driver attempts to use Thread.getContextClassLoader()
of the current
thread or the same ClassLoader
that loaded the core driver classes.
@NonNull public CompletionStage<SessionT> buildAsync()
@NonNull public SessionT build()
buildAsync()
and block on the result.
This must not be called on a driver thread.
protected abstract SessionT wrap(@NonNull CqlSession defaultSession)
@NonNull protected final CompletionStage<CqlSession> buildDefaultSessionAsync()
protected DriverContext buildContext(DriverConfigLoader configLoader, List<TypeCodec<?>> typeCodecs, NodeStateListener nodeStateListener, SchemaChangeListener schemaChangeListener, RequestTracker requestTracker, Map<String,String> localDatacenters, Map<String,Predicate<Node>> nodeFilters, ClassLoader classLoader)
InternalDriverContext
(it's not expressed
directly in the signature to avoid leaking that type through the protected API).Copyright © 2017–2019. All rights reserved.