Example usage for com.mongodb.connection ConnectionPoolSettings builder

List of usage examples for com.mongodb.connection ConnectionPoolSettings builder

Introduction

In this page you can find the example usage for com.mongodb.connection ConnectionPoolSettings builder.

Prototype

public static Builder builder() 

Source Link

Document

Gets a Builder for creating a new ConnectionPoolSettings instance.

Usage

From source file:io.gravitee.am.repository.mongodb.common.MongoFactory.java

License:Apache License

@Override
public MongoClient getObject() throws Exception {
    // Client settings
    MongoClientSettings.Builder builder = MongoClientSettings.builder();
    builder.writeConcern(WriteConcern.ACKNOWLEDGED);

    // codec configuration for pojo mapping
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(),
            fromProviders(PojoCodecProvider.builder().automatic(true).build()));
    builder.codecRegistry(pojoCodecRegistry);

    // Trying to get the MongoClientURI if uri property is defined
    String uri = readPropertyValue(propertyPrefix + "uri");
    if (uri != null && !uri.isEmpty()) {
        // The builder can be configured with default options, which may be overridden by options specified in
        // the URI string.
        MongoClientSettings settings = builder.codecRegistry(pojoCodecRegistry)
                .applyConnectionString(new ConnectionString(uri)).build();

        return MongoClients.create(settings);
    } else {/*from w w  w  .  j ava2  s .  c  o  m*/
        // Advanced configuration
        SocketSettings.Builder socketBuilder = SocketSettings.builder();
        ClusterSettings.Builder clusterBuilder = ClusterSettings.builder();
        ConnectionPoolSettings.Builder connectionPoolBuilder = ConnectionPoolSettings.builder();
        ServerSettings.Builder serverBuilder = ServerSettings.builder();
        SslSettings.Builder sslBuilder = SslSettings.builder();

        Integer connectTimeout = readPropertyValue(propertyPrefix + "connectTimeout", Integer.class, 1000);
        Integer maxWaitTime = readPropertyValue(propertyPrefix + "maxWaitTime", Integer.class);
        Integer socketTimeout = readPropertyValue(propertyPrefix + "socketTimeout", Integer.class, 1000);
        Boolean socketKeepAlive = readPropertyValue(propertyPrefix + "socketKeepAlive", Boolean.class, true);
        Integer maxConnectionLifeTime = readPropertyValue(propertyPrefix + "maxConnectionLifeTime",
                Integer.class);
        Integer maxConnectionIdleTime = readPropertyValue(propertyPrefix + "maxConnectionIdleTime",
                Integer.class);

        // We do not want to wait for a server
        Integer serverSelectionTimeout = readPropertyValue(propertyPrefix + "serverSelectionTimeout",
                Integer.class, 1000);
        Integer minHeartbeatFrequency = readPropertyValue(propertyPrefix + "minHeartbeatFrequency",
                Integer.class);
        String description = readPropertyValue(propertyPrefix + "description", String.class, "gravitee.io");
        Integer heartbeatFrequency = readPropertyValue(propertyPrefix + "heartbeatFrequency", Integer.class);
        Boolean sslEnabled = readPropertyValue(propertyPrefix + "sslEnabled", Boolean.class);

        if (maxWaitTime != null)
            connectionPoolBuilder.maxWaitTime(maxWaitTime, TimeUnit.MILLISECONDS);
        if (connectTimeout != null)
            socketBuilder.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS);
        if (socketTimeout != null)
            socketBuilder.readTimeout(socketTimeout, TimeUnit.MILLISECONDS);
        if (socketKeepAlive != null)
            socketBuilder.keepAlive(socketKeepAlive);
        if (maxConnectionLifeTime != null)
            connectionPoolBuilder.maxConnectionLifeTime(maxConnectionLifeTime, TimeUnit.MILLISECONDS);
        if (maxConnectionIdleTime != null)
            connectionPoolBuilder.maxConnectionIdleTime(maxConnectionIdleTime, TimeUnit.MILLISECONDS);
        if (minHeartbeatFrequency != null)
            serverBuilder.minHeartbeatFrequency(minHeartbeatFrequency, TimeUnit.MILLISECONDS);
        if (description != null)
            clusterBuilder.description(description);
        if (heartbeatFrequency != null)
            serverBuilder.heartbeatFrequency(heartbeatFrequency, TimeUnit.MILLISECONDS);
        if (sslEnabled != null)
            sslBuilder.enabled(sslEnabled);
        if (serverSelectionTimeout != null)
            clusterBuilder.serverSelectionTimeout(serverSelectionTimeout, TimeUnit.MILLISECONDS);

        // credentials option
        String username = readPropertyValue(propertyPrefix + "username");
        String password = readPropertyValue(propertyPrefix + "password");
        MongoCredential credentials = null;
        if (username != null || password != null) {
            String authSource = readPropertyValue(propertyPrefix + "authSource", String.class, "gravitee-am");
            credentials = MongoCredential.createCredential(username, authSource, password.toCharArray());
            builder.credential(credentials);
        }

        // clustering option
        List<ServerAddress> seeds;
        int serversCount = getServersCount();
        if (serversCount == 0) {
            String host = readPropertyValue(propertyPrefix + "host", String.class, "localhost");
            int port = readPropertyValue(propertyPrefix + "port", int.class, 27017);
            seeds = Collections.singletonList(new ServerAddress(host, port));
        } else {
            seeds = new ArrayList<>(serversCount);
            for (int i = 0; i < serversCount; i++) {
                seeds.add(buildServerAddress(i));
            }
        }
        clusterBuilder.hosts(seeds);

        SocketSettings socketSettings = socketBuilder.build();
        ClusterSettings clusterSettings = clusterBuilder.build();
        ConnectionPoolSettings connectionPoolSettings = connectionPoolBuilder.build();
        ServerSettings serverSettings = serverBuilder.build();
        SslSettings sslSettings = sslBuilder.build();
        MongoClientSettings settings = builder
                .applyToClusterSettings(builder1 -> builder1.applySettings(clusterSettings))
                .applyToSocketSettings(builder1 -> builder1.applySettings(socketSettings))
                .applyToConnectionPoolSettings(builder1 -> builder1.applySettings(connectionPoolSettings))
                .applyToServerSettings(builder1 -> builder1.applySettings(serverSettings))
                .applyToSslSettings(builder1 -> builder1.applySettings(sslSettings)).build();

        return MongoClients.create(settings);
    }
}

From source file:org.jooby.mongodb.MongoRx.java

License:Apache License

static ConnectionPoolSettings pool(final ConnectionString cstr, final Config conf) {
    ConnectionPoolSettings.Builder pool = ConnectionPoolSettings.builder().applyConnectionString(cstr);
    withConf("pool", conf, c -> {
        withMs("maintenanceFrequency", c, s -> pool.maintenanceFrequency(s, TimeUnit.MILLISECONDS));
        withMs("maintenanceInitialDelay", c, s -> pool.maintenanceInitialDelay(s, TimeUnit.MILLISECONDS));
        withMs("maxConnectionIdleTime", c, s -> pool.maxConnectionIdleTime(s, TimeUnit.MILLISECONDS));
        withMs("maxConnectionLifeTime", c, s -> pool.maxConnectionLifeTime(s, TimeUnit.MILLISECONDS));
        withInt("maxSize", c, pool::maxSize);
        withInt("maxWaitQueueSize", c, pool::maxWaitQueueSize);
        withMs("maxWaitTime", c, s -> pool.maxWaitTime(s, TimeUnit.MILLISECONDS));
        withInt("minSize", c, pool::minSize);
    });//www . jav a  2 s .c  o  m
    return pool.build();
}

From source file:org.openo.commontosca.inventory.core.mongo.MongoInventoryProvider.java

License:Apache License

public static MongoClient create(final String url) {
    ConnectionString connectionString = new ConnectionString(url);
    Builder builder = MongoClientSettings.builder();
    builder.clusterSettings(ClusterSettings.builder().applyConnectionString(connectionString).build());
    builder.connectionPoolSettings(// ww w.ja  v  a  2s  .  co m
            ConnectionPoolSettings.builder().applyConnectionString(connectionString).build());
    builder.serverSettings(ServerSettings.builder().build());
    builder.credentialList(connectionString.getCredentialList());
    builder.sslSettings(SslSettings.builder().applyConnectionString(connectionString).build());
    builder.socketSettings(SocketSettings.builder().applyConnectionString(connectionString).build());
    builder.codecRegistry(
            CodecRegistries.fromRegistries(MongoClients.getDefaultCodecRegistry(), new ObjectCodecRegistry()));
    // builder.streamFactoryFactory(new NettyStreamFactoryFactory());
    return MongoClients.create(builder.build());
}