Example usage for com.mongodb MongoClientOptions.Builder socketKeepAlive

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

Introduction

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

Prototype

boolean socketKeepAlive

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

Click Source Link

Usage

From source file:com.centurylink.mdw.dataaccess.DatabaseAccess.java

License:Apache License

private static synchronized void openMongoDbClient() {
    if (mongoClient == null) {
        String mongoHost = PropertyManager.getProperty(PropertyNames.MDW_MONGODB_HOST);
        int mongoPort = PropertyManager.getIntegerProperty(PropertyNames.MDW_MONGODB_PORT, 27017);
        int maxConnections = PropertyManager.getIntegerProperty(PropertyNames.MDW_MONGODB_POOLSIZE,
                PropertyManager.getIntegerProperty(PropertyNames.MDW_DB_POOLSIZE, 100));

        MongoClientOptions.Builder options = MongoClientOptions.builder();
        options.socketKeepAlive(true);
        if (maxConnections > 100) // MongoClient default is 100 max connections per host
            options.connectionsPerHost(maxConnections);

        mongoClient = new MongoClient(new ServerAddress(mongoHost, mongoPort), options.build());
        LoggerUtil.getStandardLogger().info(mongoClient.getMongoClientOptions().toString());
    }/*ww w .  j  av a  2s  .c o  m*/
}

From source file:com.ebay.cloud.cms.mongo.MongoDataSource.java

License:Apache License

public MongoDataSource(String servers, int connectionsPerHost, ReadPreference readPreference,
        CMSDBConfig config) {//from   w w w . j a v  a 2 s  .  co  m
    this.addrs = parseServerString(servers);

    Collections.sort(addrs, new Comparator<ServerAddress>() {

        @Override
        public int compare(ServerAddress s1, ServerAddress s2) {
            int result = s1.getHost().compareTo(s2.getHost());
            if (result != 0) {
                return result;
            } else {
                return s1.getPort() - s2.getPort();
            }
        }

    });

    MongoClientOptions.Builder builder = MongoClientOptions.builder();
    builder.socketKeepAlive(false);
    builder.connectionsPerHost(connectionsPerHost);
    if (readPreference != null) {
        builder.readPreference(readPreference);
    }
    // set socket timeout
    if (config != null) {
        Integer socketTimeOut = (Integer) config.get(CMSDBConfig.MONGO_CONNECTION_SOCKET_TIMEOUT);
        builder.socketTimeout(socketTimeOut);
    }
    MongoClientOptions mongoOptions = builder.build();
    this.mongo = new MongoClient(addrs, mongoOptions);
}

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 ww  w.  ja  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:com.novemberain.quartz.mongodb.MongoDBJobStore.java

License:Open Source License

private Mongo connectToMongoDB() throws SchedulerConfigException {

    if (mongoUri != null) {
        return connectToMongoDB(mongoUri);
    }/* w  w  w  .ja v  a  2  s .  c om*/

    MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder();
    optionsBuilder.writeConcern(WriteConcern.SAFE);

    if (mongoOptionAutoConnectRetry != null)
        optionsBuilder.autoConnectRetry(mongoOptionAutoConnectRetry);
    if (mongoOptionMaxConnectionsPerHost != null)
        optionsBuilder.connectionsPerHost(mongoOptionMaxConnectionsPerHost);
    if (mongoOptionConnectTimeoutMillis != null)
        optionsBuilder.connectTimeout(mongoOptionConnectTimeoutMillis);
    if (mongoOptionSocketTimeoutMillis != null)
        optionsBuilder.socketTimeout(mongoOptionSocketTimeoutMillis);
    if (mongoOptionSocketKeepAlive != null)
        optionsBuilder.socketKeepAlive(mongoOptionSocketKeepAlive);
    if (mongoOptionThreadsAllowedToBlockForConnectionMultiplier != null)
        optionsBuilder.threadsAllowedToBlockForConnectionMultiplier(
                mongoOptionThreadsAllowedToBlockForConnectionMultiplier);

    MongoClientOptions options = optionsBuilder.build();

    try {
        ArrayList<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
        for (String a : addresses) {
            serverAddresses.add(new ServerAddress(a));
        }
        return new MongoClient(serverAddresses, options);

    } catch (UnknownHostException e) {
        throw new SchedulerConfigException("Could not connect to MongoDB", e);
    } catch (MongoException e) {
        throw new SchedulerConfigException("Could not connect to MongoDB", e);
    }
}

From source file:fr.eolya.utils.nosql.mongodb.MongoDBConnection.java

License:Apache License

/**
 * @param hostName         The MongoDB server host name
 * @param hostPort         The MongoDB server host port
 * @return//from w w w .  ja  v  a2 s  .c o  m
 * @throws UnknownHostException 
 */
public MongoDBConnection(String hostName, int hostPort, String userName, String userPassword)
        throws UnknownHostException {

    MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
    builder.autoConnectRetry(true);
    builder.socketKeepAlive(true);
    builder.writeConcern(WriteConcern.SAFE);

    if ("".equals(hostName))
        hostName = "localhost";
    if (hostPort > 0) {
        ServerAddress addr = new ServerAddress(hostName, hostPort);
        m = new MongoClient(addr, builder.build());
    } else {
        m = new MongoClient(hostName, builder.build());
    }
    this.hostName = hostName;
    this.hostPort = hostPort;
}

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);//from   w  ww  . j  a va 2s.co m
    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.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  a  v a  2  s  .com*/
    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 w w w.  j av a 2s .  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.
 *//*  ww w  .ja  v  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);
}