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);
}
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;
}
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);
}
}
}
Aggregations