Search in sources :

Example 16 with Setting

use of cn.hutool.setting.Setting in project hutool by looly.

the class MongoDS method initSingle.

/**
 * 初始化<br>
 * 设定文件中的host和端口有三种形式:
 *
 * <pre>
 * host = host:port
 * </pre>
 *
 * <pre>
 * host = host
 * port = port
 * </pre>
 *
 * <pre>
 * host = host
 * </pre>
 */
public synchronized void initSingle() {
    if (setting == null) {
        try {
            setting = new Setting(MONGO_CONFIG_PATH, true);
        } catch (Exception e) {
        // 在single模式下,可以没有配置文件。
        }
    }
    String group = StrUtil.EMPTY;
    if (null == this.serverAddress) {
        // 存在唯一分组
        if (groups != null && groups.length == 1) {
            group = groups[0];
        }
        serverAddress = createServerAddress(group);
    }
    final MongoCredential credentail = createCredentail(group);
    try {
        if (null == credentail) {
            mongo = new MongoClient(serverAddress, buildMongoClientOptions(group));
        } else {
            mongo = new MongoClient(serverAddress, credentail, buildMongoClientOptions(group));
        }
    } catch (Exception e) {
        throw new DbRuntimeException(StrUtil.format("Init MongoDB pool with connection to [{}] error!", serverAddress), e);
    }
    log.info("Init MongoDB pool with connection to [{}]", serverAddress);
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) Setting(cn.hutool.setting.Setting) DbRuntimeException(cn.hutool.db.DbRuntimeException) NotInitedException(cn.hutool.core.exceptions.NotInitedException) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Aggregations

Setting (cn.hutool.setting.Setting)16 DbRuntimeException (cn.hutool.db.DbRuntimeException)8 NotInitedException (cn.hutool.core.exceptions.NotInitedException)3 Test (org.junit.Test)3 MongoClient (com.mongodb.MongoClient)2 MongoCredential (com.mongodb.MongoCredential)2 ServerAddress (com.mongodb.ServerAddress)2 SimpleDataSource (cn.hutool.db.ds.simple.SimpleDataSource)1 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)1 DataSource (org.apache.tomcat.jdbc.pool.DataSource)1 PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)1 Ignore (org.junit.Ignore)1 JedisPool (redis.clients.jedis.JedisPool)1 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)1