Search in sources :

Example 1 with Builder

use of com.mongodb.MongoClientOptions.Builder in project mongo-java-driver by mongodb.

the class MongoOptions method toClientOptions.

MongoClientOptions toClientOptions() {
    Builder builder = MongoClientOptions.builder().requiredReplicaSetName(requiredReplicaSetName).connectionsPerHost(connectionsPerHost).connectTimeout(connectTimeout).dbDecoderFactory(dbDecoderFactory).dbEncoderFactory(dbEncoderFactory).description(description).maxWaitTime(maxWaitTime).socketFactory(socketFactory).socketKeepAlive(socketKeepAlive).socketTimeout(socketTimeout).threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier).cursorFinalizerEnabled(cursorFinalizerEnabled).alwaysUseMBeans(alwaysUseMBeans);
    builder.writeConcern(getWriteConcern());
    if (readPreference != null) {
        builder.readPreference(getReadPreference());
    }
    return builder.build();
}
Also used : Builder(com.mongodb.MongoClientOptions.Builder)

Example 2 with Builder

use of com.mongodb.MongoClientOptions.Builder in project spring-cloud-connectors by spring-cloud.

the class MongoDbFactoryCreator method getMongoOptions.

private MongoClientOptions.Builder getMongoOptions(MongoDbFactoryConfig config) {
    MongoClientOptions.Builder builder;
    Method builderMethod = ClassUtils.getMethodIfAvailable(MongoClientOptions.class, "builder");
    if (builderMethod != null) {
        builder = (Builder) ReflectionUtils.invokeMethod(builderMethod, null);
    } else {
        Constructor<Builder> builderConstructor = ClassUtils.getConstructorIfAvailable(MongoClientOptions.Builder.class);
        try {
            builder = builderConstructor.newInstance(new Object[0]);
        } catch (InstantiationException e) {
            throw new IllegalStateException(e);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(e);
        } catch (IllegalArgumentException e) {
            throw new IllegalStateException(e);
        } catch (InvocationTargetException e) {
            throw new IllegalStateException(e);
        }
    }
    if (config != null) {
        if (config.getConnectionsPerHost() != null) {
            builder.connectionsPerHost(config.getConnectionsPerHost());
        }
        if (config.getMaxWaitTime() != null) {
            builder.maxWaitTime(config.getMaxWaitTime());
        }
        if (config.getWriteConcern() != null) {
            builder.writeConcern(new WriteConcern(config.getWriteConcern()));
        }
    }
    return builder;
}
Also used : MongoClientOptions(com.mongodb.MongoClientOptions) WriteConcern(com.mongodb.WriteConcern) Builder(com.mongodb.MongoClientOptions.Builder) Method(java.lang.reflect.Method) Builder(com.mongodb.MongoClientOptions.Builder) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Builder (com.mongodb.MongoClientOptions.Builder)2 MongoClientOptions (com.mongodb.MongoClientOptions)1 WriteConcern (com.mongodb.WriteConcern)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1