Example usage for com.mongodb MongoClientOptions.Builder maxWaitTime

List of usage examples for com.mongodb MongoClientOptions.Builder maxWaitTime

Introduction

In this page you can find the example usage for com.mongodb MongoClientOptions.Builder maxWaitTime.

Prototype

int maxWaitTime

To view the source code for com.mongodb MongoClientOptions.Builder maxWaitTime.

Click Source Link

Usage

From source file:com.github.nlloyd.hornofmongo.adaptor.Mongo.java

License:Open Source License

private void initMongoConnection() throws UnknownHostException {
    if ((innerMongo == null)) {
        MongoClientOptions.Builder builder = MongoClientOptions.builder();
        if (mongoOptions != null) {
            //Restore previous options
            builder.description(mongoOptions.description);
            builder.connectionsPerHost(mongoOptions.connectionsPerHost);
            builder.threadsAllowedToBlockForConnectionMultiplier(
                    mongoOptions.threadsAllowedToBlockForConnectionMultiplier);
            builder.maxWaitTime(mongoOptions.maxWaitTime);
            builder.connectTimeout(mongoOptions.connectTimeout);
            builder.socketTimeout(mongoOptions.socketTimeout);
            builder.socketKeepAlive(mongoOptions.socketKeepAlive);
        }/*from   w w  w  . j  a  v a  2 s . c  o m*/
        MongoClientOptions clientOptions = builder.dbEncoderFactory(HornOfMongoBSONEncoder.FACTORY).build();
        this.innerMongo = new com.mongodb.MongoClient(this.hosts, clientOptions);
        if (options != 0)
            this.innerMongo.setOptions(options);
    }
    if (mongoScope.useMongoShellWriteConcern())
        innerMongo.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
}

From source file:in.mtap.iincube.mongoser.config.MongoConfig.java

License:Apache License

private com.mongodb.MongoClient getMongo() {
    if (mongo != null)
        return mongo;
    try {// w w w .j av  a2s.c om
        List<ServerAddress> serverAddresses = toAddress(servers);
        MongoClientOptions.Builder opts = new MongoClientOptions.Builder();

        if (threadNo < 100) {
            opts.connectionsPerHost(threadNo);
        } else {
            opts.connectionsPerHost(100);
        }
        opts.threadsAllowedToBlockForConnectionMultiplier(10);
        opts.maxWaitTime(10000);
        mongo = new com.mongodb.MongoClient(serverAddresses, opts.build());
        return mongo;
    } catch (UnknownHostException e) {
        throw new IllegalArgumentException("Unknown host : " + servers);
    }
}

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

License:Apache License

private MongoClientOptions.Builder builder() {
    MongoClientOptions.Builder builder = MongoClientOptions.builder();

    builder.writeConcern(WriteConcern.SAFE);

    Integer connectionsPerHost = readPropertyValue(propertyPrefix + "connectionsPerHost", Integer.class);
    Integer connectTimeout = readPropertyValue(propertyPrefix + "connectTimeout", Integer.class, 500);
    Integer maxWaitTime = readPropertyValue(propertyPrefix + "maxWaitTime", Integer.class);
    Integer socketTimeout = readPropertyValue(propertyPrefix + "socketTimeout", Integer.class, 500);
    Boolean socketKeepAlive = readPropertyValue(propertyPrefix + "socketKeepAlive", Boolean.class);
    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,
            0);/*www  .j  av a2 s. c  om*/
    Integer minHeartbeatFrequency = readPropertyValue(propertyPrefix + "minHeartbeatFrequency", Integer.class);
    String description = readPropertyValue(propertyPrefix + "description", String.class, "gravitee.io");
    Integer heartbeatConnectTimeout = readPropertyValue(propertyPrefix + "heartbeatConnectTimeout",
            Integer.class, 1000);
    Integer heartbeatFrequency = readPropertyValue(propertyPrefix + "heartbeatFrequency", Integer.class);
    Integer heartbeatSocketTimeout = readPropertyValue(propertyPrefix + "heartbeatSocketTimeout",
            Integer.class);
    Integer localThreshold = readPropertyValue(propertyPrefix + "localThreshold", Integer.class);
    Integer minConnectionsPerHost = readPropertyValue(propertyPrefix + "minConnectionsPerHost", Integer.class);
    Boolean sslEnabled = readPropertyValue(propertyPrefix + "sslEnabled", Boolean.class);
    Integer threadsAllowedToBlockForConnectionMultiplier = readPropertyValue(
            propertyPrefix + "threadsAllowedToBlockForConnectionMultiplier", Integer.class);
    Boolean cursorFinalizerEnabled = readPropertyValue(propertyPrefix + "cursorFinalizerEnabled",
            Boolean.class);

    if (connectionsPerHost != null)
        builder.connectionsPerHost(connectionsPerHost);
    if (maxWaitTime != null)
        builder.maxWaitTime(maxWaitTime);
    if (connectTimeout != null)
        builder.connectTimeout(connectTimeout);
    if (socketTimeout != null)
        builder.socketTimeout(socketTimeout);
    if (socketKeepAlive != null)
        builder.socketKeepAlive(socketKeepAlive);
    if (maxConnectionLifeTime != null)
        builder.maxConnectionLifeTime(maxConnectionLifeTime);
    if (maxConnectionIdleTime != null)
        builder.maxConnectionIdleTime(maxConnectionIdleTime);
    if (minHeartbeatFrequency != null)
        builder.minHeartbeatFrequency(minHeartbeatFrequency);
    if (description != null)
        builder.description(description);
    if (heartbeatConnectTimeout != null)
        builder.heartbeatConnectTimeout(heartbeatConnectTimeout);
    if (heartbeatFrequency != null)
        builder.heartbeatFrequency(heartbeatFrequency);
    if (heartbeatSocketTimeout != null)
        builder.heartbeatSocketTimeout(heartbeatSocketTimeout);
    if (localThreshold != null)
        builder.localThreshold(localThreshold);
    if (minConnectionsPerHost != null)
        builder.minConnectionsPerHost(minConnectionsPerHost);
    if (sslEnabled != null)
        builder.sslEnabled(sslEnabled);
    if (threadsAllowedToBlockForConnectionMultiplier != null)
        builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);
    if (cursorFinalizerEnabled != null)
        builder.cursorFinalizerEnabled(cursorFinalizerEnabled);
    if (serverSelectionTimeout != null)
        builder.serverSelectionTimeout(serverSelectionTimeout);

    return builder;
}

From source file:org.craftercms.commons.mongo.MongoClientOptionsFactory.java

License:Open Source License

@Override
protected MongoClientOptions createInstance() throws Exception {
    MongoClientOptions.Builder builder = MongoClientOptions.builder();
    builder.alwaysUseMBeans(this.alwaysUseMBeans);
    builder.connectionsPerHost(this.connectionsPerHost);
    builder.cursorFinalizerEnabled(this.cursorFinalizerEnabled);
    builder.connectTimeout(this.connectTimeout);
    builder.maxWaitTime(this.maxWaitTime);

    switch (this.readPreference) {
    case PRIMARY_READ_PREFERENCE:
        builder.readPreference(ReadPreference.primary());
        break;//from  w  w w  .  j a  v  a  2s  .  c  om
    case NEAREST_READ_PREFERENCE:
        builder.readPreference(ReadPreference.nearest());
        break;
    case SECONDARY_READ_PREFERENCE:
        builder.readPreference(ReadPreference.secondary());
        break;
    default:
        builder.readPreference(ReadPreference.primary());
        break;
    }
    builder.writeConcern(WriteConcern.valueOf(this.writeConcern));
    builder.threadsAllowedToBlockForConnectionMultiplier(this.threadsAllowedToBlockForConnectionMultiplier);
    return builder.build();

}

From source file:org.craftercms.studio.impl.repository.mongodb.data.ClientOptionsFactory.java

License:Open Source License

public void init() {
    MongoClientOptions.Builder builder = MongoClientOptions.builder();
    builder.alwaysUseMBeans(this.alwaysUseMBeans);
    builder.autoConnectRetry(this.autoConnectRetry);
    builder.connectionsPerHost(this.connectionsPerHost);
    builder.cursorFinalizerEnabled(this.cursorFinalizerEnabled);
    builder.connectTimeout(this.connectTimeout);
    builder.maxAutoConnectRetryTime(this.maxAutoConnectRetryTime);
    builder.maxWaitTime(this.maxWaitTime);

    switch (this.readPreference) {
    case PRIMARY_READ_PREFERENCE:
        builder.readPreference(ReadPreference.primary());
        break;/*from  ww w.  j  av a2s .  c om*/
    case NEAREST_READ_PREFERENCE:
        builder.readPreference(ReadPreference.nearest());
        break;
    case SECONDARY_READ_PREFERENCE:
        builder.readPreference(ReadPreference.secondary());
        break;
    default:
        builder.readPreference(ReadPreference.primary());
        break;
    }
    builder.socketKeepAlive(this.socketKeepAlive);
    builder.writeConcern(WriteConcern.valueOf(this.writeConcern));
    builder.threadsAllowedToBlockForConnectionMultiplier(this.threadsAllowedToBlockForConnectionMultiplier);
    this.clientOptions = builder.build();
}

From source file:org.eclipselabs.emongo.components.MongoClientProviderComponent.java

License:Open Source License

private MongoClientOptions createMongoClientOptions(Map<String, Object> properties) {
    MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();

    String description = (String) properties.get(PROP_DESCRIPTION);

    if (description != null)
        optionsBuilder.description(description);

    Integer connectionsPerHost = (Integer) properties.get(PROP_CONNECTIONS_PER_HOST);

    if (connectionsPerHost != null)
        optionsBuilder.connectionsPerHost(connectionsPerHost);

    Integer threadsAllowedToBlockForConnectionMultiplier = (Integer) properties
            .get(PROP_THREADS_ALLOWED_TO_BLOCK_FOR_CONNECTION_MULTIPLIER);

    if (threadsAllowedToBlockForConnectionMultiplier != null)
        optionsBuilder//from www  .  j a  va  2 s  . c  o  m
                .threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);

    Integer maxWaitTime = (Integer) properties.get(PROP_MAX_WAIT_TIME);

    if (maxWaitTime != null)
        optionsBuilder.maxWaitTime(maxWaitTime);

    Integer connectTimeout = (Integer) properties.get(PROP_CONNECT_TIMEOUT);

    if (connectTimeout != null)
        optionsBuilder.connectTimeout(connectTimeout);

    Integer socketTimeout = (Integer) properties.get(PROP_SOCKET_TIMEOUT);

    if (socketTimeout != null)
        optionsBuilder.socketTimeout(socketTimeout);

    Boolean socketKeepAlive = (Boolean) properties.get(PROP_SOCKET_KEEP_ALIVE);

    if (socketKeepAlive != null)
        optionsBuilder.socketKeepAlive(socketKeepAlive);

    Boolean autoConnectRetry = (Boolean) properties.get(PROP_AUTO_CONNECT_RETRY);

    if (autoConnectRetry != null)
        optionsBuilder.autoConnectRetry(autoConnectRetry);

    Long maxAutoConnectRetryTime = (Long) properties.get(PROP_MAX_AUTO_CONNECT_RETRY_TIME);

    if (maxAutoConnectRetryTime != null)
        optionsBuilder.maxAutoConnectRetryTime(maxAutoConnectRetryTime);

    Boolean continueOnInsertError = (Boolean) properties.get(PROP_CONTINUE_ON_INSERT_ERROR);

    if (continueOnInsertError == null)
        continueOnInsertError = Boolean.FALSE;

    Integer w = (Integer) properties.get(PROP_W);

    if (w == null)
        w = Integer.valueOf(1);

    Integer wtimeout = (Integer) properties.get(PROP_WTIMEOUT);

    if (wtimeout == null)
        wtimeout = Integer.valueOf(0);

    Boolean fsync = (Boolean) properties.get(PROP_FSYNC);

    if (fsync == null)
        fsync = Boolean.FALSE;

    Boolean j = (Boolean) properties.get(PROP_J);

    if (j == null)
        j = Boolean.FALSE;

    WriteConcern writeConcern = new WriteConcern(w, wtimeout, fsync, j, continueOnInsertError);
    optionsBuilder.writeConcern(writeConcern);

    return optionsBuilder.build();
}

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

License:Apache License

private MongoClientOptions.Builder options(final Config config) {
    MongoClientOptions.Builder builder = MongoClientOptions.builder();

    builder.connectionsPerHost(config.getInt("connectionsPerHost"));
    builder.threadsAllowedToBlockForConnectionMultiplier(
            config.getInt("threadsAllowedToBlockForConnectionMultiplier"));
    builder.maxWaitTime((int) config.getDuration("maxWaitTime", TimeUnit.MILLISECONDS));
    builder.connectTimeout((int) config.getDuration("connectTimeout", TimeUnit.MILLISECONDS));
    builder.socketTimeout((int) config.getDuration("socketTimeout", TimeUnit.MILLISECONDS));
    builder.socketKeepAlive(config.getBoolean("socketKeepAlive"));
    builder.cursorFinalizerEnabled(config.getBoolean("cursorFinalizerEnabled"));
    builder.alwaysUseMBeans(config.getBoolean("alwaysUseMBeans"));
    builder.heartbeatFrequency(config.getInt("heartbeatFrequency"));
    builder.minHeartbeatFrequency(config.getInt("minHeartbeatFrequency"));
    builder.heartbeatConnectTimeout((int) config.getDuration("heartbeatConnectTimeout", TimeUnit.MILLISECONDS));
    builder.heartbeatSocketTimeout((int) config.getDuration("heartbeatSocketTimeout", TimeUnit.MILLISECONDS));

    return builder;
}

From source file:org.kaaproject.kaa.server.appenders.mongo.appender.LogEventMongoDao.java

License:Apache License

/**
 * Create new instance of <code>LogEventMongoDao</code> using configuration instance of
 * <code>MongoDbConfig</code>.
 *
 * @param configuration the configuration of log event mongo dao, it contain server size,
 *                      credentials, max wait time, etc.
 *///from   w w w  .  j av  a2  s  .com
@SuppressWarnings("deprecation")
public LogEventMongoDao(MongoDbConfig configuration) throws Exception {

    List<ServerAddress> seeds = new ArrayList<>(configuration.getMongoServers().size());
    for (MongoDbServer server : configuration.getMongoServers()) {
        seeds.add(new ServerAddress(server.getHost(), server.getPort()));
    }

    List<MongoCredential> credentials = new ArrayList<>();
    if (configuration.getMongoCredentials() != null) {
        for (MongoDBCredential credential : configuration.getMongoCredentials()) {
            credentials.add(MongoCredential.createMongoCRCredential(credential.getUser(),
                    configuration.getDbName(), credential.getPassword().toCharArray()));
        }
    }

    MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();
    if (configuration.getConnectionsPerHost() != null) {
        optionsBuilder.connectionsPerHost(configuration.getConnectionsPerHost());
    }
    if (configuration.getMaxWaitTime() != null) {
        optionsBuilder.maxWaitTime(configuration.getMaxWaitTime());
    }
    if (configuration.getConnectionTimeout() != null) {
        optionsBuilder.connectTimeout(configuration.getConnectionTimeout());
    }
    if (configuration.getSocketTimeout() != null) {
        optionsBuilder.socketTimeout(configuration.getSocketTimeout());
    }
    if (configuration.getSocketKeepalive() != null) {
        optionsBuilder.socketKeepAlive(configuration.getSocketKeepalive());
    }

    MongoClientOptions options = optionsBuilder.build();
    mongoClient = new MongoClient(seeds, credentials, options);

    MongoDbFactory dbFactory = new SimpleMongoDbFactory(mongoClient, configuration.getDbName());

    MappingMongoConverter converter = new MappingMongoConverter(dbFactory, new MongoMappingContext());
    converter.setTypeMapper(new DefaultMongoTypeMapper(null));

    mongoTemplate = new MongoTemplate(dbFactory, converter);
    mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
}

From source file:org.mule.module.mongo.MongoCloudConnector.java

License:Open Source License

private MongoClientOptions.Builder getMongoOptions(String database) {
    final MongoClientOptions.Builder options = MongoClientOptions.builder();

    if (connectionsPerHost != null) {
        options.connectionsPerHost(connectionsPerHost);
    }//ww w .  j  a  va 2 s . c o m
    if (threadsAllowedToBlockForConnectionMultiplier != null) {
        options.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);
    }
    if (maxWaitTime != null) {
        options.maxWaitTime(maxWaitTime);
    }
    if (connectTimeout != null) {
        options.connectTimeout(connectTimeout);
    }
    if (socketTimeout != null) {
        options.socketTimeout(socketTimeout);
    }
    if (autoConnectRetry != null) {
        options.autoConnectRetry(autoConnectRetry);
    }
    if (database != null) {
        this.database = database;
    }
    return options;
}

From source file:org.s1.mongodb.MongoDBConnectionHelper.java

License:Apache License

/**
 *
 * @param instance//from   www  .j av  a 2s . c  o  m
 */
private static synchronized void initialize(String instance) {
    if (!connections.containsKey(instance)) {

        Map<String, Object> mopt = Options.getStorage().getMap(OPTIONS);
        Map<String, Object> m = Objects.get(mopt, "connections." + instance);
        if (Objects.isNullOrEmpty(m)) {
            m = Objects.get(mopt, "connections." + DEFAULT_INSTANCE);
        }

        MongoClientOptions.Builder b = MongoClientOptions.builder();
        MongoClientOptions def_opt = MongoClientOptions.builder().build();
        b.connectionsPerHost(Objects.get(m, "connectionsPerHost", def_opt.getConnectionsPerHost()));
        b.autoConnectRetry(Objects.get(m, "autoConnectRetry", def_opt.isAutoConnectRetry()));
        b.connectTimeout(Objects.get(m, "connectTimeout", def_opt.getConnectTimeout()));
        b.socketKeepAlive(Objects.get(m, "socketKeepAlive", def_opt.isSocketKeepAlive()));
        b.socketTimeout(Objects.get(m, "socketTimeout", def_opt.getSocketTimeout()));
        b.maxAutoConnectRetryTime(
                Objects.get(m, "maxAutoConnectRetryTime", def_opt.getMaxAutoConnectRetryTime()));
        b.maxWaitTime(Objects.get(m, "maxWaitTime", def_opt.getMaxWaitTime()));
        b.threadsAllowedToBlockForConnectionMultiplier(
                Objects.get(m, "threadsAllowedToBlockForConnectionMultiplier",
                        def_opt.getThreadsAllowedToBlockForConnectionMultiplier()));
        b.writeConcern(WriteConcern.FSYNC_SAFE);
        MongoClientOptions opt = b.build();

        MongoClient cl = null;
        try {
            cl = new MongoClient(
                    new ServerAddress(Objects.get(m, "host", "localhost"), Objects.get(m, "port", 27017)), opt);
        } catch (UnknownHostException e) {
            throw S1SystemError.wrap(e);
        }

        String dbName = Objects.get(m, "name");
        if (Objects.isNullOrEmpty(dbName))
            throw new S1SystemError("Cannot initialize MongoDB connection, because name is not set");

        DB db = cl.getDB(dbName);

        String user = Objects.get(m, "user");
        String password = Objects.get(m, "password");
        if (!Objects.isNullOrEmpty(user)) {
            if (!db.authenticate(user, password.toCharArray())) {
                throw new S1SystemError(
                        "Cannot authenticate MongoDB connection " + dbName + " with user " + user);
            }
        }
        LOG.info("MongoDB connected " + cl.getAddress().getHost() + ":" + cl.getAddress().getPort());

        connections.put(instance, db);
    }
}