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.
|
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 |
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.
|
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 |
getMaxConnectionCount()
Returns the maximum number of connections to use.
|
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.
|
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 |
setAutoDiscoverServers(boolean autoDiscoverServers)
Sets if additional servers are auto discovered or if connections are
limited to the ones manually configured.
|
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 |
setMaxConnectionCount(int maxConnectionCount)
Sets the maximum number of connections to use.
|
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 |
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
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 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 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.NONE
.
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 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 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).
@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 setAutoDiscoverServers(boolean autoDiscoverServers)
Defaults to true, e.g., auto-discover.
autoDiscoverServers
- The new value for auto-discovering servers.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 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 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 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-2013 Allanbank Consulting, Inc.. All Rights Reserved.