use of com.mongodb.MongoClientOptions in project cas by apereo.
the class MongoDbConnectionFactory method buildMongoDbClientOptionsFactoryBean.
/**
* Create a MongoClientOptions object.
* <p>
* The object will be created from a collection of individual property
* settings, or a MongoDb client connection string (uri), or some
* combination of the two.
* <p>
* This is complicated by the fact that the default values provided by
* the CAS code in BaseMongoDbProperties.java are not the same as the
* default values for the corresponding options provided by the MongoDb
* Java driver when it creates a MongoClientOptions object.
* <p>
* To ensure predictable results in all cases, we initialize the client
* options from the individual property settings (even if just the CAS
* default values), and then use those values as the starting point to
* process the client uri (if one is provided). This way, any options
* in the uri will override the earlier ones, but any options missing
* from the uri will have the values (default or user-provided) from
* the individual property settings.
* <p>
* This behavior matches the comment in BaseMongoDbProperties.java for
* the clientUri property: "If not specified, will fallback onto other
* individual settings. If specified, takes over all other settings
* where applicable."
*
* @param mongo the property setttings (including, perhaps, a client uri)
* @return a bean containing the MongoClientOptions object
*/
@SneakyThrows
private MongoClientOptionsFactoryBean buildMongoDbClientOptionsFactoryBean(final BaseMongoDbProperties mongo) {
final MongoClientOptionsFactoryBean bean1 = new MongoClientOptionsFactoryBean();
bean1.setWriteConcern(WriteConcern.valueOf(mongo.getWriteConcern()));
bean1.setHeartbeatConnectTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis());
bean1.setHeartbeatSocketTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis());
bean1.setMaxConnectionLifeTime(mongo.getConns().getLifetime());
bean1.setSocketKeepAlive(mongo.isSocketKeepAlive());
bean1.setMaxConnectionIdleTime((int) Beans.newDuration(mongo.getIdleTimeout()).toMillis());
bean1.setConnectionsPerHost(mongo.getConns().getPerHost());
bean1.setSocketTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis());
bean1.setConnectTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis());
if (StringUtils.isNotBlank(mongo.getReplicaSet())) {
bean1.setRequiredReplicaSetName(mongo.getReplicaSet());
}
bean1.setSsl(mongo.isSslEnabled());
if (mongo.isSslEnabled()) {
bean1.setSslSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
}
bean1.afterPropertiesSet();
if (StringUtils.isNotBlank(mongo.getClientUri())) {
final MongoClientOptionsFactoryBean bean2 = new MongoClientOptionsFactoryBean();
final MongoClientURI uri = buildMongoClientURI(mongo.getClientUri(), bean1.getObject());
final MongoClientOptions opts = uri.getOptions();
bean2.setWriteConcern(opts.getWriteConcern());
bean2.setHeartbeatConnectTimeout(opts.getHeartbeatConnectTimeout());
bean2.setHeartbeatSocketTimeout(opts.getHeartbeatSocketTimeout());
bean2.setMaxConnectionLifeTime(opts.getMaxConnectionLifeTime());
bean2.setSocketKeepAlive(opts.isSocketKeepAlive());
bean2.setMaxConnectionIdleTime(opts.getMaxConnectionIdleTime());
bean2.setConnectionsPerHost(opts.getConnectionsPerHost());
bean2.setSocketTimeout(opts.getSocketTimeout());
bean2.setConnectTimeout(opts.getConnectTimeout());
bean2.setRequiredReplicaSetName(opts.getRequiredReplicaSetName());
bean2.setSsl(opts.isSslEnabled());
if (opts.isSslEnabled()) {
bean2.setSslSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
}
bean2.afterPropertiesSet();
bean1.destroy();
return bean2;
}
return bean1;
}
use of com.mongodb.MongoClientOptions in project modules-extra by CubeEngine.
the class Bigdata method aquireClient.
private MongoClient aquireClient(String db) {
if (mongoClient != null) {
return mongoClient;
}
try {
Authentication authConfig = config.authentication;
ServerAddress address = new ServerAddress(InetAddress.getByName(this.config.host), this.config.port);
List<MongoCredential> credentialList = Collections.emptyList();
if (authConfig != null && authConfig.username != null && authConfig.password != null) {
MongoCredential credential = MongoCredential.createCredential(authConfig.username, db, authConfig.password.toCharArray());
credentialList = singletonList(credential);
}
MongoClientOptions options = MongoClientOptions.builder().connectTimeout(this.config.connectionTimeout).build();
mongoClient = new MongoClient(address, credentialList, options);
// Check if available by pinging the database...
mongoClient.getDatabase(db).runCommand(new Document("ping", 1));
return mongoClient;
} catch (UnknownHostException e) {
throw new IllegalStateException("Invalid host", e);
}
}
use of com.mongodb.MongoClientOptions in project spring-integration by spring-projects.
the class MongoDbAvailableRule method apply.
@Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
MongoDbAvailable mongoAvailable = method.getAnnotation(MongoDbAvailable.class);
if (mongoAvailable != null) {
try {
MongoClientOptions options = new MongoClientOptions.Builder().serverSelectionTimeout(100).build();
MongoClient mongo = new MongoClient(ServerAddress.defaultHost(), options);
mongo.listDatabaseNames().first();
} catch (Exception e) {
logger.warn("MongoDb is not available. Skipping the test: " + target.getClass().getSimpleName() + "." + method.getName() + "()");
return;
}
}
base.evaluate();
}
};
}
use of com.mongodb.MongoClientOptions in project neo4j-apoc-procedures by neo4j-contrib.
the class MongoDBTest method setUp.
@BeforeClass
public static void setUp() throws Exception {
TestUtil.ignoreException(() -> {
MongoClientOptions options = MongoClientOptions.builder().socketTimeout(100).serverSelectionTimeout(100).heartbeatConnectTimeout(100).heartbeatSocketTimeout(100).heartbeatFrequency(100).minHeartbeatFrequency(100).maxWaitTime(100).connectTimeout(100).build();
mongoClient = new MongoClient("192.168.99.100", options);
MongoDatabase database = mongoClient.getDatabase("test");
collection = database.getCollection("test");
collection.deleteMany(new Document());
collection.insertOne(new Document(map("name", "testDocument", "date", currentTime, "longValue", longValue)));
mongoDBRunning = true;
}, MongoTimeoutException.class);
if (mongoDBRunning) {
db = new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().newGraphDatabase();
TestUtil.registerProcedure(db, MongoDB.class);
}
}
use of com.mongodb.MongoClientOptions in project serverless by bluenimble.
the class MongoDatabasePlugin method createClient.
private MongoClient createClient(String name, ApiSpace space, JsonObject spec) {
String factoryKey = createFactoryKey(name, space);
if (space.containsRecyclable(factoryKey)) {
return null;
}
String database = Json.getString(spec, Spec.Database);
if (Lang.isNullOrEmpty(database)) {
return null;
}
MongoCredential creds = createCredentials(database, Json.getObject(spec, Spec.Auth));
String cluster = Json.getString(spec, Spec.Cluster);
if (cluster == null) {
cluster = Lang.BLANK;
}
String[] sNodes = Lang.split(Json.getString(spec, Spec.Cluster), Lang.COMMA, true);
List<ServerAddress> nodes = new ArrayList<ServerAddress>(sNodes.length);
for (int i = 0; i < sNodes.length; i++) {
nodes.add(address(sNodes[i]));
}
MongoClientOptions options = MongoClientOptions.builder().cursorFinalizerEnabled(false).codecRegistry(codecRegistry).sslEnabled(Json.getBoolean(spec, Spec.SSL, false)).connectionsPerHost(Json.getInteger(spec, Spec.MaxConnections, 100)).threadsAllowedToBlockForConnectionMultiplier(Json.getInteger(spec, Spec.ThreadsAllowedToBlock, 5)).maxWaitTime(Json.getInteger(spec, Spec.MaxWaitTime, 1000)).connectTimeout(Json.getInteger(spec, Spec.ConnectTimeout, 10000)).socketTimeout(Json.getInteger(spec, Spec.SocketTimeout, 0)).build();
// apply other options such as pool
MongoClient client = null;
if (creds == null) {
client = new MongoClient(nodes, options);
} else {
client = new MongoClient(nodes, creds, options);
}
space.addRecyclable(factoryKey, new RecyclableClient(client, database));
return client;
}
Aggregations