Search in sources :

Example 1 with MongoClientOptionsFactoryBean

use of org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean in project cas by apereo.

the class Beans method newMongoDbClientOptionsFactoryBean.

/**
     * New mongo db client options factory bean.
     *
     * @param mongo the mongo properties.
     * @return the mongo client options factory bean
     */
public static MongoClientOptionsFactoryBean newMongoDbClientOptionsFactoryBean(final AbstractMongoInstanceProperties mongo) {
    try {
        final MongoClientOptionsFactoryBean bean = new MongoClientOptionsFactoryBean();
        bean.setWriteConcern(WriteConcern.valueOf(mongo.getWriteConcern()));
        bean.setHeartbeatConnectTimeout(Long.valueOf(mongo.getTimeout()).intValue());
        bean.setHeartbeatSocketTimeout(Long.valueOf(mongo.getTimeout()).intValue());
        bean.setMaxConnectionLifeTime(mongo.getConns().getLifetime());
        bean.setSocketKeepAlive(mongo.isSocketKeepAlive());
        bean.setMaxConnectionIdleTime(Long.valueOf(mongo.getIdleTimeout()).intValue());
        bean.setConnectionsPerHost(mongo.getConns().getPerHost());
        bean.setSocketTimeout(Long.valueOf(mongo.getTimeout()).intValue());
        bean.setConnectTimeout(Long.valueOf(mongo.getTimeout()).intValue());
        bean.afterPropertiesSet();
        return bean;
    } catch (final Exception e) {
        throw new BeanCreationException(e.getMessage(), e);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MongoClientOptionsFactoryBean(org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean) BeanCreationException(org.springframework.beans.factory.BeanCreationException)

Example 2 with MongoClientOptionsFactoryBean

use of org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean in project cas by apereo.

the class MongoDbConnectionFactory method buildMongoDbClientOptionsFactoryBean.

/**
 * Create a MongoClientOptions object.
 * <p>
 * The object will be created from a collection of individual property
 * settings, or a MongoDb client connection string (uri), or some
 * combination of the two.
 * <p>
 * This is complicated by the fact that the default values provided by
 * the CAS code in BaseMongoDbProperties.java are not the same as the
 * default values for the corresponding options provided by the MongoDb
 * Java driver when it creates a MongoClientOptions object.
 * <p>
 * To ensure predictable results in all cases, we initialize the client
 * options from the individual property settings (even if just the CAS
 * default values), and then use those values as the starting point to
 * process the client uri (if one is provided). This way, any options
 * in the uri will override the earlier ones, but any options missing
 * from the uri will have the values (default or user-provided) from
 * the individual property settings.
 * <p>
 * This behavior matches the comment in BaseMongoDbProperties.java for
 * the clientUri property: "If not specified, will fallback onto other
 * individual settings. If specified, takes over all other settings
 * where applicable."
 *
 * @param mongo the property setttings (including, perhaps, a client uri)
 * @return a bean containing the MongoClientOptions object
 */
@SneakyThrows
private MongoClientOptionsFactoryBean buildMongoDbClientOptionsFactoryBean(final BaseMongoDbProperties mongo) {
    final MongoClientOptionsFactoryBean bean1 = new MongoClientOptionsFactoryBean();
    bean1.setWriteConcern(WriteConcern.valueOf(mongo.getWriteConcern()));
    bean1.setHeartbeatConnectTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis());
    bean1.setHeartbeatSocketTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis());
    bean1.setMaxConnectionLifeTime(mongo.getConns().getLifetime());
    bean1.setSocketKeepAlive(mongo.isSocketKeepAlive());
    bean1.setMaxConnectionIdleTime((int) Beans.newDuration(mongo.getIdleTimeout()).toMillis());
    bean1.setConnectionsPerHost(mongo.getConns().getPerHost());
    bean1.setSocketTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis());
    bean1.setConnectTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis());
    if (StringUtils.isNotBlank(mongo.getReplicaSet())) {
        bean1.setRequiredReplicaSetName(mongo.getReplicaSet());
    }
    bean1.setSsl(mongo.isSslEnabled());
    if (mongo.isSslEnabled()) {
        bean1.setSslSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
    }
    bean1.afterPropertiesSet();
    if (StringUtils.isNotBlank(mongo.getClientUri())) {
        final MongoClientOptionsFactoryBean bean2 = new MongoClientOptionsFactoryBean();
        final MongoClientURI uri = buildMongoClientURI(mongo.getClientUri(), bean1.getObject());
        final MongoClientOptions opts = uri.getOptions();
        bean2.setWriteConcern(opts.getWriteConcern());
        bean2.setHeartbeatConnectTimeout(opts.getHeartbeatConnectTimeout());
        bean2.setHeartbeatSocketTimeout(opts.getHeartbeatSocketTimeout());
        bean2.setMaxConnectionLifeTime(opts.getMaxConnectionLifeTime());
        bean2.setSocketKeepAlive(opts.isSocketKeepAlive());
        bean2.setMaxConnectionIdleTime(opts.getMaxConnectionIdleTime());
        bean2.setConnectionsPerHost(opts.getConnectionsPerHost());
        bean2.setSocketTimeout(opts.getSocketTimeout());
        bean2.setConnectTimeout(opts.getConnectTimeout());
        bean2.setRequiredReplicaSetName(opts.getRequiredReplicaSetName());
        bean2.setSsl(opts.isSslEnabled());
        if (opts.isSslEnabled()) {
            bean2.setSslSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
        }
        bean2.afterPropertiesSet();
        bean1.destroy();
        return bean2;
    }
    return bean1;
}
Also used : MongoClientOptionsFactoryBean(org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean) MongoClientOptions(com.mongodb.MongoClientOptions) MongoClientURI(com.mongodb.MongoClientURI) SneakyThrows(lombok.SneakyThrows)

Example 3 with MongoClientOptionsFactoryBean

use of org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean in project cas by apereo.

the class MongoDbConnectionFactory method buildMongoDbClientOptions.

@SneakyThrows
private MongoClientOptions buildMongoDbClientOptions() {
    final MongoClientOptionsFactoryBean bean = new MongoClientOptionsFactoryBean();
    bean.setSocketTimeout(TIMEOUT);
    bean.setConnectTimeout(TIMEOUT);
    bean.setMaxWaitTime(TIMEOUT);
    bean.afterPropertiesSet();
    return bean.getObject();
}
Also used : MongoClientOptionsFactoryBean(org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean) SneakyThrows(lombok.SneakyThrows)

Example 4 with MongoClientOptionsFactoryBean

use of org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean in project cas by apereo.

the class MongoDbCloudConfigBootstrapConfiguration method mongoClientOptions.

@Bean
public MongoClientOptions mongoClientOptions() {
    try {
        final MongoClientOptionsFactoryBean bean = new MongoClientOptionsFactoryBean();
        bean.setSocketTimeout(TIMEOUT);
        bean.setConnectTimeout(TIMEOUT);
        bean.afterPropertiesSet();
        return bean.getObject();
    } catch (final Exception e) {
        throw new BeanCreationException(e.getMessage(), e);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MongoClientOptionsFactoryBean(org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean) BeanCreationException(org.springframework.beans.factory.BeanCreationException) MongoClientOptionsFactoryBean(org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

MongoClientOptionsFactoryBean (org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean)4 SneakyThrows (lombok.SneakyThrows)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 MongoClientOptions (com.mongodb.MongoClientOptions)1 MongoClientURI (com.mongodb.MongoClientURI)1 Bean (org.springframework.context.annotation.Bean)1