Example usage for com.mongodb MongoClientOptions.Builder localThreshold

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

Introduction

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

Prototype

int localThreshold

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

Click Source Link

Usage

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);/*  w w w.ja va 2 s  .  c  o 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.wso2.extension.siddhi.store.mongodb.util.MongoTableUtils.java

License:Open Source License

/**
 * Utility method which can be used to create MongoClientOptionsBuilder from values defined in the
 * deployment yaml file.// ww w.j  a  va2 s  . c o  m
 *
 * @param storeAnnotation the source annotation which contains the needed parameters.
 * @param configReader    {@link ConfigReader} Configuration Reader
 * @return MongoClientOptions.Builder
 */
public static MongoClientOptions.Builder extractMongoClientOptionsBuilder(Annotation storeAnnotation,
        ConfigReader configReader) {

    MongoClientOptions.Builder mongoClientOptionsBuilder = MongoClientOptions.builder();
    try {
        mongoClientOptionsBuilder.connectionsPerHost(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.CONNECTIONS_PER_HOST, "100")));
        mongoClientOptionsBuilder.connectTimeout(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.CONNECT_TIMEOUT, "10000")));
        mongoClientOptionsBuilder.heartbeatConnectTimeout(Integer
                .parseInt(configReader.readConfig(MongoTableConstants.HEARTBEAT_CONNECT_TIMEOUT, "20000")));
        mongoClientOptionsBuilder.heartbeatSocketTimeout(Integer
                .parseInt(configReader.readConfig(MongoTableConstants.HEARTBEAT_SOCKET_TIMEOUT, "20000")));
        mongoClientOptionsBuilder.heartbeatFrequency(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.HEARTBEAT_FREQUENCY, "10000")));
        mongoClientOptionsBuilder.localThreshold(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.LOCAL_THRESHOLD, "15")));
        mongoClientOptionsBuilder.maxWaitTime(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.MAX_WAIT_TIME, "120000")));
        mongoClientOptionsBuilder.minConnectionsPerHost(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.MIN_CONNECTIONS_PER_HOST, "0")));
        mongoClientOptionsBuilder.minHeartbeatFrequency(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.MIN_HEARTBEAT_FREQUENCY, "500")));
        mongoClientOptionsBuilder.serverSelectionTimeout(Integer
                .parseInt(configReader.readConfig(MongoTableConstants.SERVER_SELECTION_TIMEOUT, "30000")));
        mongoClientOptionsBuilder.socketTimeout(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.SOCKET_TIMEOUT, "0")));
        mongoClientOptionsBuilder.threadsAllowedToBlockForConnectionMultiplier(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.THREADS_ALLOWED_TO_BLOCK, "5")));
        mongoClientOptionsBuilder.socketKeepAlive(
                Boolean.parseBoolean(configReader.readConfig(MongoTableConstants.SOCKET_KEEP_ALIVE, "false")));
        mongoClientOptionsBuilder.sslEnabled(
                Boolean.parseBoolean(configReader.readConfig(MongoTableConstants.SSL_ENABLED, "false")));
        mongoClientOptionsBuilder.cursorFinalizerEnabled(Boolean
                .parseBoolean(configReader.readConfig(MongoTableConstants.CURSOR_FINALIZER_ENABLED, "true")));
        mongoClientOptionsBuilder.readPreference(ReadPreference
                .valueOf(configReader.readConfig(MongoTableConstants.READ_PREFERENCE, "primary")));
        mongoClientOptionsBuilder.writeConcern(WriteConcern
                .valueOf(configReader.readConfig(MongoTableConstants.WRITE_CONCERN, "acknowledged")));

        String readConcern = configReader.readConfig(MongoTableConstants.READ_CONCERN, "DEFAULT");
        if (!readConcern.matches("DEFAULT")) {
            mongoClientOptionsBuilder.readConcern(new ReadConcern(ReadConcernLevel.fromString(readConcern)));
        }

        int maxConnectionIdleTime = Integer
                .parseInt(configReader.readConfig(MongoTableConstants.MAX_CONNECTION_IDLE_TIME, "0"));
        if (maxConnectionIdleTime != 0) {
            mongoClientOptionsBuilder.maxConnectionIdleTime(maxConnectionIdleTime);
        }

        int maxConnectionLifeTime = Integer
                .parseInt(configReader.readConfig(MongoTableConstants.MAX_CONNECTION_LIFE_TIME, "0"));
        if (maxConnectionIdleTime != 0) {
            mongoClientOptionsBuilder.maxConnectionLifeTime(maxConnectionLifeTime);
        }

        String requiredReplicaSetName = configReader.readConfig(MongoTableConstants.REQUIRED_REPLICA_SET_NAME,
                "");
        if (!requiredReplicaSetName.equals("")) {
            mongoClientOptionsBuilder.requiredReplicaSetName(requiredReplicaSetName);
        }

        String applicationName = configReader.readConfig(MongoTableConstants.APPLICATION_NAME, "");
        if (!applicationName.equals("")) {
            mongoClientOptionsBuilder.applicationName(applicationName);
        }

        String secureConnectionEnabled = storeAnnotation
                .getElement(MongoTableConstants.ANNOTATION_ELEMENT_SECURE_CONNECTION);
        secureConnectionEnabled = secureConnectionEnabled == null ? "false" : secureConnectionEnabled;

        if (secureConnectionEnabled.equalsIgnoreCase("true")) {
            mongoClientOptionsBuilder.sslEnabled(true);
            String trustStore = storeAnnotation.getElement(MongoTableConstants.ANNOTATION_ELEMENT_TRUSTSTORE);
            trustStore = trustStore == null ? configReader.readConfig("trustStore", DEFAULT_TRUST_STORE_FILE)
                    : trustStore;
            trustStore = resolveCarbonHome(trustStore);

            String trustStorePassword = storeAnnotation
                    .getElement(MongoTableConstants.ANNOTATION_ELEMENT_TRUSTSTOREPASS);
            trustStorePassword = trustStorePassword == null
                    ? configReader.readConfig("trustStorePassword", DEFAULT_TRUST_STORE_PASSWORD)
                    : trustStorePassword;

            String keyStore = storeAnnotation.getElement(MongoTableConstants.ANNOTATION_ELEMENT_KEYSTORE);
            keyStore = keyStore == null ? configReader.readConfig("keyStore", DEFAULT_KEY_STORE_FILE)
                    : keyStore;
            keyStore = resolveCarbonHome(keyStore);

            String keyStorePassword = storeAnnotation
                    .getElement(MongoTableConstants.ANNOTATION_ELEMENT_STOREPASS);
            keyStorePassword = keyStorePassword == null
                    ? configReader.readConfig("keyStorePassword", DEFAULT_KEY_STORE_PASSWORD)
                    : keyStorePassword;

            mongoClientOptionsBuilder.socketFactory(MongoTableUtils.extractSocketFactory(trustStore,
                    trustStorePassword, keyStore, keyStorePassword));
        }
        return mongoClientOptionsBuilder;
    } catch (IllegalArgumentException e) {
        throw new MongoTableException("Values Read from config readers have illegal values : ", e);
    }
}