use of com.mongodb.MongoClientOptions in project java-design-patterns by iluwatar.
the class MongoDb method connect.
/**
* Connect to Db. Check th connection
*/
@Override
public void connect() {
MongoCredential mongoCredential = MongoCredential.createCredential(MONGO_USER, DATABASE_NAME, MONGO_PASSWORD.toCharArray());
MongoClientOptions options = MongoClientOptions.builder().build();
client = new MongoClient(new ServerAddress(), List.of(mongoCredential), options);
db = client.getDatabase(DATABASE_NAME);
}
use of com.mongodb.MongoClientOptions in project spring-boot by spring-projects.
the class MongoPropertiesTests method allMongoClientOptionsCanBeSet.
@Test
public void allMongoClientOptionsCanBeSet() {
MongoClientOptions.Builder builder = MongoClientOptions.builder();
builder.alwaysUseMBeans(true);
builder.connectionsPerHost(101);
builder.connectTimeout(10001);
builder.cursorFinalizerEnabled(false);
builder.description("test");
builder.maxWaitTime(120001);
builder.socketKeepAlive(true);
builder.socketTimeout(1000);
builder.threadsAllowedToBlockForConnectionMultiplier(6);
builder.minConnectionsPerHost(0);
builder.maxConnectionIdleTime(60000);
builder.maxConnectionLifeTime(60000);
builder.heartbeatFrequency(10001);
builder.minHeartbeatFrequency(501);
builder.heartbeatConnectTimeout(20001);
builder.heartbeatSocketTimeout(20001);
builder.localThreshold(20);
builder.requiredReplicaSetName("testReplicaSetName");
MongoClientOptions options = builder.build();
MongoProperties properties = new MongoProperties();
MongoClient client = new MongoClientFactory(properties, null).createMongoClient(options);
MongoClientOptions wrapped = client.getMongoClientOptions();
assertThat(wrapped.isAlwaysUseMBeans()).isEqualTo(options.isAlwaysUseMBeans());
assertThat(wrapped.getConnectionsPerHost()).isEqualTo(options.getConnectionsPerHost());
assertThat(wrapped.getConnectTimeout()).isEqualTo(options.getConnectTimeout());
assertThat(wrapped.isCursorFinalizerEnabled()).isEqualTo(options.isCursorFinalizerEnabled());
assertThat(wrapped.getDescription()).isEqualTo(options.getDescription());
assertThat(wrapped.getMaxWaitTime()).isEqualTo(options.getMaxWaitTime());
assertThat(wrapped.getSocketTimeout()).isEqualTo(options.getSocketTimeout());
assertThat(wrapped.isSocketKeepAlive()).isEqualTo(options.isSocketKeepAlive());
assertThat(wrapped.getThreadsAllowedToBlockForConnectionMultiplier()).isEqualTo(options.getThreadsAllowedToBlockForConnectionMultiplier());
assertThat(wrapped.getMinConnectionsPerHost()).isEqualTo(options.getMinConnectionsPerHost());
assertThat(wrapped.getMaxConnectionIdleTime()).isEqualTo(options.getMaxConnectionIdleTime());
assertThat(wrapped.getMaxConnectionLifeTime()).isEqualTo(options.getMaxConnectionLifeTime());
assertThat(wrapped.getHeartbeatFrequency()).isEqualTo(options.getHeartbeatFrequency());
assertThat(wrapped.getMinHeartbeatFrequency()).isEqualTo(options.getMinHeartbeatFrequency());
assertThat(wrapped.getHeartbeatConnectTimeout()).isEqualTo(options.getHeartbeatConnectTimeout());
assertThat(wrapped.getHeartbeatSocketTimeout()).isEqualTo(options.getHeartbeatSocketTimeout());
assertThat(wrapped.getLocalThreshold()).isEqualTo(options.getLocalThreshold());
assertThat(wrapped.getRequiredReplicaSetName()).isEqualTo(options.getRequiredReplicaSetName());
}
use of com.mongodb.MongoClientOptions in project spring-data-mongodb by spring-projects.
the class MongoNamespaceTests method testMongoSingletonWithSslEnabled.
// DATAMONGO-764
@Test
public void testMongoSingletonWithSslEnabled() throws Exception {
assertTrue(ctx.containsBean("mongoSsl"));
MongoClientFactoryBean mfb = (MongoClientFactoryBean) ctx.getBean("&mongoSsl");
MongoClientOptions options = (MongoClientOptions) getField(mfb, "mongoClientOptions");
assertTrue("socketFactory should be a SSLSocketFactory", options.getSocketFactory() instanceof SSLSocketFactory);
}
use of com.mongodb.MongoClientOptions in project spring-data-mongodb by spring-projects.
the class MongoNamespaceTests method testMongoSingletonWithAttributes.
@Test
public void testMongoSingletonWithAttributes() throws Exception {
assertTrue(ctx.containsBean("defaultMongo"));
MongoClientFactoryBean mfb = (MongoClientFactoryBean) ctx.getBean("&defaultMongo");
String host = (String) getField(mfb, "host");
Integer port = (Integer) getField(mfb, "port");
assertEquals("localhost", host);
assertEquals(new Integer(27017), port);
MongoClientOptions options = (MongoClientOptions) getField(mfb, "mongoClientOptions");
assertFalse("By default socketFactory should not be a SSLSocketFactory", options.getSocketFactory() instanceof SSLSocketFactory);
}
use of com.mongodb.MongoClientOptions in project leopard by tanhaichao.
the class DfsGridImpl method loadGridFS.
@SuppressWarnings("deprecation")
private synchronized GridFS loadGridFS() {
if (fs != null) {
return fs;
}
String[] list = server.split(":");
String host = list[0];
int port = Integer.parseInt(list[1]);
String username = null;
String password = null;
if (list.length >= 4) {
username = list[2];
password = list[3];
}
int connectTimeout = 1000 * 60;
MongoClientOptions options = new MongoClientOptions.Builder().connectTimeout(connectTimeout).build();
client = new MongoClient(new ServerAddress(host, port), options);
// @SuppressWarnings("deprecation")
DB db = client.getDB("dfs");
if (username != null && username.length() > 0) {
if (password != null && password.length() > 0) {
db.addUser(username, password.toCharArray());
}
}
return new GridFS(db, "fs");
}
Aggregations