public class MongoClientConfiguration extends Object implements Cloneable, Serializable
Modifier and Type | Field and Description |
---|---|
static String |
ADMIN_DB_NAME
The name of the administration database.
|
static String |
DEFAULT_DB_NAME
The default database.
|
protected static int |
DEFAULT_MAX_STRING_CACHE_ENTRIES
The default maximum number of strings to keep in the string encoder and
decoder cache.
|
protected static int |
DEFAULT_MAX_STRING_CACHE_LENGTH
The default maximum length byte array / string to cache.
|
static Charset |
UTF8
The ASCII character encoding.
|
Modifier | Constructor and Description |
---|---|
|
MongoClientConfiguration()
Creates a new MongoClientConfiguration.
|
|
MongoClientConfiguration(InetSocketAddress... servers)
Creates a new MongoClientConfiguration.
|
|
MongoClientConfiguration(MongoClientConfiguration other)
Creates a new MongoClientConfiguration.
|
|
MongoClientConfiguration(MongoDbUri mongoDbUri)
Creates a new
MongoClientConfiguration instance using a MongoDB
style URL to initialize its state. |
protected |
MongoClientConfiguration(MongoDbUri mongoDbUri,
Durability defaultDurability)
Creates a new
MongoClientConfiguration instance using a MongoDB
style URL to initialize its state. |
|
MongoClientConfiguration(String mongoDbUri)
Creates a new
MongoClientConfiguration instance using a MongoDB
style URL to initialize its state. |
Modifier and Type | Method and Description |
---|---|
void |
addCredential(Credential.Builder credentials)
Adds the specified credentials to the configuration.
|
void |
addCredential(Credential credentials)
Adds the specified credentials to the configuration.
|
void |
addPropertyChangeListener(PropertyChangeListener listener)
Add a
PropertyChangeListener from the configuration. |
void |
addPropertyChangeListener(String propertyName,
PropertyChangeListener listener)
Add a
PropertyChangeListener from the configuration. |
void |
addServer(InetSocketAddress server)
Adds a server to initially attempt to connect to.
|
void |
addServer(String server)
Adds a server to initially attempt to connect to.
|
void |
authenticate(String userName,
String password)
Deprecated.
Replaced with the more general
Credential capability.
Will be removed after the 1.3.0 release. |
void |
authenticateAsAdmin(String userName,
String password)
Deprecated.
Replaced with the more general
Credential capability.
Will be removed after the 1.3.0 release. |
MongoClientConfiguration |
clone()
Creates a copy of this MongoClientConfiguration.
|
ConnectionModel |
getConnectionModel()
Returns the model the driver uses for managing connections.
|
int |
getConnectTimeout()
Returns how long to wait (in milliseconds) for a socket connection to
complete.
|
Collection<Credential> |
getCredentials()
Returns the map of database names to credentials to use to access that
database on the server.
|
String |
getDefaultDatabase()
Deprecated.
Replaced with the more general
Credential capability.
Will be removed after the 1.3.0 release. |
Durability |
getDefaultDurability()
Returns the default durability for write operations on the server.
|
ReadPreference |
getDefaultReadPreference()
Returns the default read preference for a query.
|
Executor |
getExecutor()
Returns the executor to use when processing responses from the server.
|
LockType |
getLockType()
Returns the type of hand off lock to use between threads in the core of
the driver.
|
int |
getMaxCachedStringEntries()
Returns the maximum number of strings that may have their encoded form
cached.
|
int |
getMaxCachedStringLength()
Returns the maximum length for a string that the stream is allowed to
cache.This can be used to stop a single long string from pushing useful
values out of the cache.
|
int |
getMaxConnectionCount()
Returns the maximum number of connections to use.
|
int |
getMaxIdleTickCount()
Returns the number of read timeouts (a tick) before closing the
connection.
|
int |
getMaxPendingOperationsPerConnection()
Returns the maximum number of pending operations to allow per connection.
|
long |
getMaxSecondaryLag()
Returns the maximum number of milliseconds that a secondary can be behind
the primary before they will be excluded from being used for queries on
secondaries.
|
int |
getMinConnectionCount()
Returns the minimum number of connections to try and keep open.
|
String |
getPasswordHash()
Deprecated.
Replaced with the more general
Credential capability.
Will be removed after the 1.3.0 release. |
int |
getReadTimeout()
Returns how long to wait (in milliseconds) for a socket read to complete.
|
int |
getReconnectTimeout()
Returns how long to wait (in milliseconds) for a broken connection to be
reconnected.
|
List<InetSocketAddress> |
getServerAddresses()
Returns the list of servers to initially attempt to connect to.
|
List<String> |
getServers()
Returns the list of servers to initially attempt to connect to.
|
SocketFactory |
getSocketFactory()
Returns the socket factory to use in making connections to the MongoDB
server.
|
ThreadFactory |
getThreadFactory()
Returns the thread factory for managing connections.
|
String |
getUserName()
Deprecated.
Replaced with the more general
Credential capability.
Will be removed after the 1.3.0 release. |
boolean |
isAdminUser()
Deprecated.
Replaced with the more general
Credential capability.
Will be removed after the 1.3.0 release. |
boolean |
isAuthenticating()
Returns true if the connection is authenticating.
|
boolean |
isAutoDiscoverServers()
Returns if additional servers are auto discovered or if connections are
limited to the ones manually configured.
|
boolean |
isUsingSoKeepalive()
Returns if the
SO_KEEPALIVE
socket option is set. |
void |
removePropertyChangeListener(PropertyChangeListener listener)
Removes a
PropertyChangeListener from the configuration. |
void |
removePropertyChangeListener(String propertyName,
PropertyChangeListener listener)
Removes a
PropertyChangeListener from the configuration. |
void |
setAutoDiscoverServers(boolean autoDiscoverServers)
Sets if additional servers are auto discovered or if connections are
limited to the ones manually configured.
|
void |
setConnectionModel(ConnectionModel connectionModel)
Sets the model the driver uses for managing connections.
|
void |
setConnectTimeout(int connectTimeout)
Sets how long to wait (in milliseconds) for a socket connection to
complete.
|
void |
setCredentials(Collection<Credential> credentials)
Sets the credentials to use to access the server.
|
void |
setDefaultDatabase(String defaultDatabase)
Deprecated.
Replaced with the more general
Credential capability.
Will be removed after the 1.3.0 release. |
void |
setDefaultDurability(Durability defaultDurability)
Sets the default durability for write operations on the server to the new
value.
|
void |
setDefaultReadPreference(ReadPreference defaultReadPreference)
Sets the value of the default read preference for a query.
|
void |
setExecutor(Executor executor)
Sets the value of executor for replies from the server.
|
void |
setLockType(LockType lockType)
Sets the type of hand off lock to use between threads in the core of the
driver.
|
void |
setMaxCachedStringEntries(int maxCacheEntries)
Sets the value of maximum number of strings that may have their encoded
form cached.
|
void |
setMaxCachedStringLength(int maxlength)
Sets the value of length for a string that may be cached.
|
void |
setMaxConnectionCount(int maxConnectionCount)
Sets the maximum number of connections to use.
|
void |
setMaxIdleTickCount(int idleTickCount)
Sets the value of the number of read timeouts (a tick) before closing the
connection.
|
void |
setMaxPendingOperationsPerConnection(int maxPendingOperationsPerConnection)
Sets the maximum number of pending operations to allow per connection.
|
void |
setMaxSecondaryLag(long maxSecondaryLag)
Sets the maximum number of milliseconds that a secondary can be behind
the primary before they will be excluded from being used for queries on
secondaries.
|
void |
setMinConnectionCount(int minimumConnectionCount)
Sets the value of the minimum number of connections to try and keep open.
|
void |
setReadTimeout(int readTimeout) |
void |
setReconnectTimeout(int connectTimeout)
Sets how long to wait (in milliseconds) for a broken connection to
reconnect.
|
void |
setServers(List<InetSocketAddress> servers)
Sets the servers to initially attempt to connect to.
|
void |
setSocketFactory(SocketFactory socketFactory)
Sets the socket factory to use in making connections to the MongoDB
server.
|
void |
setThreadFactory(ThreadFactory factory)
Sets the thread factory for managing connections to the new value.
|
void |
setUsingSoKeepalive(boolean usingSoKeepalive)
Sets if the
SO_KEEPALIVE
socket option is set. |
public static final String ADMIN_DB_NAME
public static final String DEFAULT_DB_NAME
public static final Charset UTF8
protected static final int DEFAULT_MAX_STRING_CACHE_ENTRIES
protected static final int DEFAULT_MAX_STRING_CACHE_LENGTH
public MongoClientConfiguration()
public MongoClientConfiguration(InetSocketAddress... servers)
servers
- The initial set of servers to connect to.public MongoClientConfiguration(MongoClientConfiguration other)
other
- The configuration to copy.public MongoClientConfiguration(MongoDbUri mongoDbUri) throws IllegalArgumentException
MongoClientConfiguration
instance using a MongoDB
style URL to initialize its state. Further configuration is possible once
the MongoClientConfiguration
has been instantiated.mongoDbUri
- The configuration for the connection to MongoDB expressed as a
MongoDB URL.IllegalArgumentException
- If the mongoDbUri is not a properly formated MongoDB
style URL.public MongoClientConfiguration(String mongoDbUri) throws IllegalArgumentException
MongoClientConfiguration
instance using a MongoDB
style URL to initialize its state. Further configuration is possible once
the MongoClientConfiguration
has been instantiated.mongoDbUri
- The configuration for the connection to MongoDB expressed as a
MongoDB URL.IllegalArgumentException
- If the mongoDbUri is not a properly formated MongoDB
style URL.protected MongoClientConfiguration(MongoDbUri mongoDbUri, Durability defaultDurability) throws IllegalArgumentException
MongoClientConfiguration
instance using a MongoDB
style URL to initialize its state. Further configuration is possible once
the MongoClientConfiguration
has been instantiated.mongoDbUri
- The configuration for the connection to MongoDB expressed as a
MongoDB URL.defaultDurability
- The default durability.IllegalArgumentException
- If the mongoDbUri is not a properly formated MongoDB
style URL.public void addCredential(Credential credentials) throws IllegalArgumentException
credentials
- The credentials to use when accessing the MongoDB server.IllegalArgumentException
- If the credentials refer to an unknown authentication type or
the configuration already has a set of credentials for the
credentials specified database.public void addCredential(Credential.Builder credentials) throws IllegalArgumentException
credentials
- The credentials to use when accessing the MongoDB server.IllegalArgumentException
- If the credentials refer to an unknown authentication type or
the configuration already has a set of credentials for the
credentials specified database.public void addPropertyChangeListener(PropertyChangeListener listener)
PropertyChangeListener
from the configuration. The listener
will receive notification of all changes to the configuration.listener
- The PropertyChangeListener
to be addedpublic void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
PropertyChangeListener
from the configuration. The listener
will receive notification of all changes to the configuration's specified
property.propertyName
- The name of the property to listen on.listener
- The PropertyChangeListener
to be addedpublic void addServer(InetSocketAddress server)
server
- The server to add.public void addServer(String server)
server
- The server to add.@Deprecated public void authenticate(String userName, String password) throws MongoDbAuthenticationException
Credential
capability.
Will be removed after the 1.3.0 release.Mongo
instance.userName
- The user name.password
- the password.MongoDbAuthenticationException
- On a failure initializing the authentication information.@Deprecated public void authenticateAsAdmin(String userName, String password) throws MongoDbAuthenticationException
Credential
capability.
Will be removed after the 1.3.0 release.Mongo
instance.userName
- The user name.password
- the password.MongoDbAuthenticationException
- On a failure initializing the authentication information.public MongoClientConfiguration clone()
Note: This is not a traditional clone to ensure a deep copy of all information.
public ConnectionModel getConnectionModel()
Defaults to ConnectionModel.RECEIVER_THREAD
.
public int getConnectTimeout()
Defaults to 0 or forever.
public Collection<Credential> getCredentials()
@Deprecated public String getDefaultDatabase()
Credential
capability.
Will be removed after the 1.3.0 release.This is used as the database to authenticate against if the user is not an administrative user.
Defaults to "local".
public Durability getDefaultDurability()
Defaults to Durability.ACK
.
public ReadPreference getDefaultReadPreference()
Defaults to ReadPreference.PRIMARY
.
public Executor getExecutor()
By default the executor is null
which will cause the reply
handling to execute on the socket's receive thread.
Care should be taken to ensure that the executor does not drop requests.
This implies that the
ThreadPoolExecutor.CallerRunsPolicy
or
similar should be used as the
RejectedExecutionHandler
.
public LockType getLockType()
Defaults to LockType.MUTEX
.
public int getMaxCachedStringEntries()
Defaults to 1024.
Note: The caches are maintained per connection and there is a cache for the encoder and another for the decoder. The results is that caching 25 string with 10 connections can result in 500 cache entries (2 * 25 * 10).
public int getMaxCachedStringLength()
Defaults to 25.
Note: The caches are maintained per connection and there is a cache for the encoder and another for the decoder. The results is that caching 25 string with 10 connections can result in 500 cache entries (2 * 25 * 10).
public int getMaxConnectionCount()
Defaults to 3.
Note: In the case of connecting to a replica set this setting
limits the number of connections to the primary server. The driver will
create single connections to the secondary servers if queries are issued
with a ReadPreference
other than ReadPreference.PRIMARY
.
public int getMaxIdleTickCount()
Defaults to Integer.MAX_VALUE
.
public int getMaxPendingOperationsPerConnection()
Defaults to 1024.
Note: In the case of an connection error it is impossible to determine which pending operations completed and which did not. Setting this value to 1 results in synchronous operations that wait for responses.
public long getMaxSecondaryLag()
Defaults to 5 minutes (300,000).
public int getMinConnectionCount()
Defaults to 0.
@Deprecated public String getPasswordHash()
Credential
capability.
Will be removed after the 1.3.0 release.public int getReadTimeout()
Defaults to 0 or never.
public int getReconnectTimeout()
Defaults to 0 or forever.
public List<InetSocketAddress> getServerAddresses()
public List<String> getServers()
public SocketFactory getSocketFactory()
Defaults to SocketFactory.getDefault()
.
setSocketFactory(...) or usage
examples and suggestions.
public ThreadFactory getThreadFactory()
@Deprecated public String getUserName()
Credential
capability.
Will be removed after the 1.3.0 release.@Deprecated public boolean isAdminUser()
Credential
capability.
Will be removed after the 1.3.0 release.public boolean isAuthenticating()
public boolean isAutoDiscoverServers()
Defaults to true, e.g., auto-discover.
public boolean isUsingSoKeepalive()
SO_KEEPALIVE
socket option is set.
Defaults to true, e.g., use SO_KEEPALIVE.
SO_KEEPALIVE
socket option is set.public void removePropertyChangeListener(PropertyChangeListener listener)
PropertyChangeListener
from the configuration. The
listener will no longer receive notification of all changes to the
configuration.listener
- The PropertyChangeListener
to be removedpublic void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
PropertyChangeListener
from the configuration. The
listener will no longer receive notification of all changes to the
configuration's specified property.propertyName
- The name of the property that was listened on.listener
- The PropertyChangeListener
to be removedpublic void setAutoDiscoverServers(boolean autoDiscoverServers)
Defaults to true, e.g., auto-discover.
autoDiscoverServers
- The new value for auto-discovering servers.public void setConnectionModel(ConnectionModel connectionModel)
Defaults to ConnectionModel.RECEIVER_THREAD
.
connectionModel
- The new value for the model the driver uses for managing
connections.public void setConnectTimeout(int connectTimeout)
connectTimeout
- The time to wait (in milliseconds) for a socket connection to
complete.public void setCredentials(Collection<Credential> credentials)
credentials
- The credentials to use to access the server..IllegalArgumentException
- If the credentials refer to an unknown authentication type or
the configuration already has a set of credentials for the
credentials specified database.@Deprecated public void setDefaultDatabase(String defaultDatabase)
Credential
capability.
Will be removed after the 1.3.0 release.This is used as the database to authenticate against if the user is not an administrative user.
Defaults to "local".
defaultDatabase
- The new default database value.public void setDefaultDurability(Durability defaultDurability)
defaultDurability
- The default durability for write operations on the server.public void setDefaultReadPreference(ReadPreference defaultReadPreference)
Defaults to ReadPreference.PRIMARY
if null
is set.
defaultReadPreference
- The default read preference for a query.public void setExecutor(Executor executor)
By default the executor is null
which will cause the reply
handling to execute on the socket's receive thread.
Care should be taken to ensure that the executor does not drop requests.
This implies that the
ThreadPoolExecutor.CallerRunsPolicy
or
similar should be used as the
RejectedExecutionHandler
.
executor
- The new value for the executor.public void setLockType(LockType lockType)
Defaults to LockType.MUTEX
.
lockType
- The new value for the type of hand off lock used.public void setMaxCachedStringEntries(int maxCacheEntries)
Defaults to 1024.
Note: The caches are maintained per MongoClient
instance.
maxCacheEntries
- The new value for the maximum number of strings that may have
their encoded form cached.public void setMaxCachedStringLength(int maxlength)
Defaults to 25.
Note: The caches are maintained per MongoClient
instance.
maxlength
- The new value for the length for a string that the encoder is
allowed to cache.public void setMaxConnectionCount(int maxConnectionCount)
Defaults to 3.
Note: In the case of connecting to a replica set this setting
limits the number of connections to the primary server. The driver will
create single connections to the secondary servers if queries are issued
with a ReadPreference
other than ReadPreference.PRIMARY
.
maxConnectionCount
- New maximum number of connections to use.public void setMaxIdleTickCount(int idleTickCount)
idleTickCount
- The new value for the number of read timeouts (a tick) before
closing the connection.public void setMaxPendingOperationsPerConnection(int maxPendingOperationsPerConnection)
maxPendingOperationsPerConnection
- The new maximum number of pending operations to allow per
connection.public void setMaxSecondaryLag(long maxSecondaryLag)
Defaults to 5 minutes (300,000).
maxSecondaryLag
- The new value for the maximum number of milliseconds that a
secondary can be behind the primary before they will be
excluded from being used for queries on secondaries.public void setMinConnectionCount(int minimumConnectionCount)
minimumConnectionCount
- The new value for the minimum number of connections to try and
keep open.public void setReadTimeout(int readTimeout)
readTimeout
- The time to wait (in milliseconds) for a socket read to
complete.public void setReconnectTimeout(int connectTimeout)
connectTimeout
- The time to wait (in milliseconds) for a broken connection to
reconnect.public void setServers(List<InetSocketAddress> servers)
servers
- The servers to connect to.public void setSocketFactory(SocketFactory socketFactory)
Defaults to SocketFactory.getDefault().
For SSL based connections this can be an appropriately configured
SSLSocketFactory
.
Other Socket
and InetSocketAddress
implementations with
an appropriate SocketFactory
implementation can be used with the
driver. The driver only ever calls the
SocketFactory.createSocket()
method and then connects the socket
passing the server's InetSocketAddress
.
See the junixsocket
Project for an example of a Socket
and
InetSocketAddress
implementations for UNIX Domain Sockets that
can be wrapped with SocketFactory similar to the following:
public class AFUNIXSocketFactory extends SocketFactory {
public Socket createSocket() throws java.io.IOException {
return new org.newsclub.net.unix.AFUNIXSocket.newInstance();
}
public Socket createSocket(String host, int port) throws SocketException {
throw new SocketException("AFUNIX socket does not support connections to a host/port");
}
public Socket createSocket(InetAddress host, int port) throws SocketException {
throw new SocketException("AFUNIX socket does not support connections to a host/port");
}
public Socket createSocket(String host, int port, InetAddress localHost,
int localPort) throws SocketException {
throw new SocketException("AFUNIX socket does not support connections to a host/port");
}
public Socket createSocket(InetAddress address, int port,
InetAddress localAddress, int localPort) throws SocketException {
throw new SocketException("AFUNIX socket does not support connections to a host/port");
}
}
socketFactory
- The socketFactory value.public void setThreadFactory(ThreadFactory factory)
factory
- The thread factory for managing connections.public void setUsingSoKeepalive(boolean usingSoKeepalive)
SO_KEEPALIVE
socket option is set.
Defaults to true, e.g., use SO_KEEPALIVE.
usingSoKeepalive
- The new value for using SO_KEEPALIVE.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.