@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<InetSocketAddress> |
programmaticContactPoints |
protected RequestTracker |
requestTracker |
protected SelfT |
self |
protected List<TypeCodec<?>> |
typeCodecs |
Constructor and Description |
---|
SessionBuilder() |
Modifier and Type | Method and Description |
---|---|
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,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
withKeyspace(CqlIdentifier.fromCql(keyspaceName)) |
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 Set<InetSocketAddress> programmaticContactPoints
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:
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)
datastax-java-driver
section.
core driver options
.
reference.conf
file with sensible defaults for all
mandatory options, except the contact points.@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, call InetAddress.getAllByName(String)
before calling this
method.
@NonNull public SelfT addContactPoint(@NonNull InetSocketAddress contactPoint)
addContactPoints(Collection)
@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).
@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)
withKeyspace(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,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–2018. All rights reserved.