Search in sources :

Example 16 with MongoClientOptions

use of com.mongodb.MongoClientOptions in project leopard by tanhaichao.

the class MongoImpl method init.

@SuppressWarnings("deprecation")
public void init() {
    String[] list = server.split(":");
    String host = list[0];
    int port = Integer.parseInt(list[1]);
    int connectTimeout = 1000 * 60;
    MongoClientOptions options = new MongoClientOptions.Builder().connectTimeout(connectTimeout).build();
    client = new MongoClient(new ServerAddress(host, port), options);
    this.db = client.getDB(this.database);
    if (username != null && username.length() > 0) {
        if (password != null && password.length() > 0) {
            db.addUser(username, password.toCharArray());
        }
    }
    this.collection = db.getCollection(collectionName);
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientOptions(com.mongodb.MongoClientOptions) ServerAddress(com.mongodb.ServerAddress)

Example 17 with MongoClientOptions

use of com.mongodb.MongoClientOptions in project nutzboot by nutzam.

the class NutMongoDbStarter method createMongoClientOptions.

/**
 * 获取Mongodb的配置信息
 */
@IocBean(name = "mongoClientOptions")
public MongoClientOptions createMongoClientOptions() {
    // 连接选项控制
    MongoClientOptions.Builder builder = MongoClientOptions.builder();
    builder.minConnectionsPerHost(conf.getInt(PROP_MIN_CONNECTIONS_PER_HOST, 0));
    builder.connectionsPerHost(conf.getInt(PROP_MAX_CONNECTIONS_PER_HOST, 100));
    builder.threadsAllowedToBlockForConnectionMultiplier(conf.getInt(PROP_THREADS, 5));
    builder.maxWaitTime(conf.getInt(PROP_MAX_WAIT_TIME, 1000 * 60 * 2));
    builder.connectTimeout(conf.getInt(PROP_CONNECT_TIMEOUT, 1000 * 10));
    builder.socketTimeout(conf.getInt(PROP_SOCKET_TIMEOUT, 0));
    // 写入的策略
    if (conf.has(PROP_WRITECONCERN)) {
        builder.writeConcern(WriteConcern.valueOf(conf.get(PROP_WRITECONCERN).trim().toUpperCase()));
    }
    // 读取的策略
    if (conf.has(PROP_READCONCERN)) {
        switch(conf.get(PROP_READCONCERN).trim().toUpperCase()) {
            case "DEFAULT":
                builder.readConcern(ReadConcern.DEFAULT);
                break;
            case "LOCAL":
                builder.readConcern(ReadConcern.LOCAL);
                break;
            case "MAJORITY":
                builder.readConcern(ReadConcern.MAJORITY);
                break;
            case "LINEARIZABLE":
                builder.readConcern(ReadConcern.LINEARIZABLE);
                break;
            default:
                break;
        }
    }
    // 读取的优先级策略
    if (conf.has(PROP_READPREFERENCE)) {
        switch(conf.get(PROP_READPREFERENCE)) {
            case "primary":
                builder.readPreference(ReadPreference.primary());
                break;
            case "primaryPreferred":
                builder.readPreference(ReadPreference.primaryPreferred());
                break;
            case "secondary":
                builder.readPreference(ReadPreference.secondary());
                break;
            case "secondaryPreferred":
                builder.readPreference(ReadPreference.secondaryPreferred());
                break;
            case "nearest":
                builder.readPreference(ReadPreference.nearest());
                break;
            default:
                break;
        }
    }
    // 完工,再见...
    MongoClientOptions options = builder.build();
    // 打印现有的参数信息,便于调优
    logger.info(options);
    return options;
}
Also used : MongoClientOptions(com.mongodb.MongoClientOptions) IocBean(org.nutz.ioc.loader.annotation.IocBean)

Example 18 with MongoClientOptions

use of com.mongodb.MongoClientOptions in project jmeter by apache.

the class MongoSourceElement method testStarted.

@Override
public void testStarted() {
    if (log.isDebugEnabled()) {
        log.debug(getTitle() + " testStarted");
    }
    MongoClientOptions.Builder builder = MongoClientOptions.builder().autoConnectRetry(getAutoConnectRetry()).connectTimeout(getConnectTimeout()).connectionsPerHost(getConnectionsPerHost()).maxAutoConnectRetryTime(getMaxAutoConnectRetryTime()).maxWaitTime(getMaxWaitTime()).socketKeepAlive(getSocketKeepAlive()).socketTimeout(getSocketTimeout()).threadsAllowedToBlockForConnectionMultiplier(getThreadsAllowedToBlockForConnectionMultiplier());
    if (getSafe()) {
        builder.writeConcern(WriteConcern.SAFE);
    } else {
        builder.writeConcern(new WriteConcern(getWriteOperationNumberOfServers(), getWriteOperationTimeout(), getFsync(), getWaitForJournaling(), getContinueOnInsertError()));
    }
    MongoClientOptions mongoOptions = builder.build();
    if (log.isDebugEnabled()) {
        log.debug("options : " + mongoOptions.toString());
    }
    if (getThreadContext().getVariables().getObject(getSource()) != null) {
        if (log.isWarnEnabled()) {
            log.warn(getSource() + " has already been defined.");
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug(getSource() + "  is being defined.");
        }
        try {
            getThreadContext().getVariables().putObject(getSource(), new MongoDB(MongoUtils.toServerAddresses(getConnection()), mongoOptions));
        } catch (UnknownHostException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) MongoClientOptions(com.mongodb.MongoClientOptions) WriteConcern(com.mongodb.WriteConcern) MongoDB(org.apache.jmeter.protocol.mongodb.mongo.MongoDB)

Aggregations

MongoClientOptions (com.mongodb.MongoClientOptions)18 MongoClient (com.mongodb.MongoClient)10 Test (org.junit.Test)8 ServerAddress (com.mongodb.ServerAddress)5 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)5 MongoClientFactoryBean (org.springframework.data.mongodb.core.MongoClientFactoryBean)5 MongoCredential (com.mongodb.MongoCredential)3 UnknownHostException (java.net.UnknownHostException)2 Document (org.bson.Document)2 DB (com.mongodb.DB)1 MongoClientURI (com.mongodb.MongoClientURI)1 WriteConcern (com.mongodb.WriteConcern)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 GridFS (com.mongodb.gridfs.GridFS)1 ArrayList (java.util.ArrayList)1 SneakyThrows (lombok.SneakyThrows)1 MongoDB (org.apache.jmeter.protocol.mongodb.mongo.MongoDB)1 Authentication (org.cubeengine.module.bigdata.MongoDBConfiguration.Authentication)1 BeforeClass (org.junit.BeforeClass)1 Statement (org.junit.runners.model.Statement)1