Search in sources :

Example 11 with MongoClientOptions

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);
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) MongoClientOptions(com.mongodb.MongoClientOptions) ServerAddress(com.mongodb.ServerAddress)

Example 12 with MongoClientOptions

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());
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientOptions(com.mongodb.MongoClientOptions) Test(org.junit.Test)

Example 13 with MongoClientOptions

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);
}
Also used : MongoClientFactoryBean(org.springframework.data.mongodb.core.MongoClientFactoryBean) MongoClientOptions(com.mongodb.MongoClientOptions) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Test(org.junit.Test)

Example 14 with MongoClientOptions

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);
}
Also used : MongoClientFactoryBean(org.springframework.data.mongodb.core.MongoClientFactoryBean) MongoClientOptions(com.mongodb.MongoClientOptions) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Test(org.junit.Test)

Example 15 with MongoClientOptions

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");
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientOptions(com.mongodb.MongoClientOptions) ServerAddress(com.mongodb.ServerAddress) GridFS(com.mongodb.gridfs.GridFS) DB(com.mongodb.DB)

Aggregations

MongoClientOptions (com.mongodb.MongoClientOptions)18 MongoClient (com.mongodb.MongoClient)10 Test (org.junit.Test)8 ServerAddress (com.mongodb.ServerAddress)5 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)5 MongoClientFactoryBean (org.springframework.data.mongodb.core.MongoClientFactoryBean)5 MongoCredential (com.mongodb.MongoCredential)3 UnknownHostException (java.net.UnknownHostException)2 Document (org.bson.Document)2 DB (com.mongodb.DB)1 MongoClientURI (com.mongodb.MongoClientURI)1 WriteConcern (com.mongodb.WriteConcern)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 GridFS (com.mongodb.gridfs.GridFS)1 ArrayList (java.util.ArrayList)1 SneakyThrows (lombok.SneakyThrows)1 MongoDB (org.apache.jmeter.protocol.mongodb.mongo.MongoDB)1 Authentication (org.cubeengine.module.bigdata.MongoDBConfiguration.Authentication)1 BeforeClass (org.junit.BeforeClass)1 Statement (org.junit.runners.model.Statement)1