本文整理汇总了Java中com.mongodb.MongoClientOptions.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java MongoClientOptions.Builder方法的具体用法?Java MongoClientOptions.Builder怎么用?Java MongoClientOptions.Builder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mongodb.MongoClientOptions
的用法示例。
在下文中一共展示了MongoClientOptions.Builder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
@Override
public boolean start() {
Read spec = source.spec;
MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();
optionsBuilder.maxConnectionIdleTime(spec.maxConnectionIdleTime());
optionsBuilder.socketKeepAlive(spec.keepAlive());
client = new MongoClient(new MongoClientURI(spec.uri(), optionsBuilder));
MongoDatabase mongoDatabase = client.getDatabase(spec.database());
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(spec.collection());
if (spec.filter() == null) {
cursor = mongoCollection.find().iterator();
} else {
Document bson = Document.parse(spec.filter());
cursor = mongoCollection.find(bson).iterator();
}
return advance();
}
示例2: create
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
@Override
public void create(JSONObject config) {
String key = config.getString("key");
if (mongos.containsKey(key))
return;
String schema = config.getString("schema");
if (validator.isEmpty(schema))
throw new NullPointerException("未设置schema值[" + config + "]!");
JSONArray array = config.getJSONArray("ips");
if (array == null || array.size() == 0)
throw new NullPointerException("未设置ips值[" + config + "]!");
String username = config.getString("username");
String password = config.getString("password");
MongoClientOptions.Builder builder = MongoClientOptions.builder().connectionsPerHost(maxActive).maxWaitTime(maxWait);
List<MongoClient> list = new ArrayList<>();
for (int i = 0; i < array.size(); i++)
list.add(new MongoClient(new MongoClientURI("mongodb://" + username + ":" + password + "@" + array.getString(i) + "/" + schema, builder)));
schemas.put(key, schema);
mongos.put(key, list);
if (logger.isDebugEnable())
logger.debug("Mongo数据库[{}]初始化完成。", config);
}
示例3: setOptions
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
protected void setOptions(final MongoClientOptions.Builder builder, final ConfigurationPropertyRetriever propertyRetriever) {
final Map<String, Method> settingsMap = createSettingsMap();
for (final Map.Entry<String, Method> entry : settingsMap.entrySet()) {
final String value = propertyRetriever.get(entry.getKey());
if (value == null) {
continue;
}
final Method setterMethod = entry.getValue();
try {
setterMethod.invoke(builder, convertTo(entry.getValue().getParameterTypes()[0], value));
} catch (InvocationTargetException | IllegalAccessException e) {
LOG.error("Could not set options", e);
}
}
}
示例4: openMongoDbClient
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
private static synchronized void openMongoDbClient() {
if (mongoClient == null) {
String mongoHost = PropertyManager.getProperty(PropertyNames.MDW_MONGODB_HOST);
int mongoPort = PropertyManager.getIntegerProperty(PropertyNames.MDW_MONGODB_PORT, 27017);
int maxConnections = PropertyManager.getIntegerProperty(PropertyNames.MDW_MONGODB_POOLSIZE, PropertyManager.getIntegerProperty(PropertyNames.MDW_DB_POOLSIZE, 100));
MongoClientOptions.Builder options = MongoClientOptions.builder();
options.socketKeepAlive(true);
if (maxConnections > 100) // MongoClient default is 100 max connections per host
options.connectionsPerHost(maxConnections);
mongoClient = new MongoClient(new ServerAddress(mongoHost, mongoPort), options.build());
for (String name : mongoClient.getDatabase("mdw").listCollectionNames()) {
createMongoDocIdIndex(name);
}
LoggerUtil.getStandardLogger().info(mongoClient.getMongoClientOptions().toString());
}
}
示例5: initialize
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
public void initialize(IMongoClientOptionsHandler optionsHandler, MongoDataSourceCfgMeta cfgMeta) throws Exception {
__cfgMeta = cfgMeta;
MongoClientOptions.Builder _builder = null;
if (optionsHandler != null) {
_builder = optionsHandler.handler(cfgMeta.getName());
}
if (_builder == null) {
_builder = MongoClientOptions.builder();
}
if (StringUtils.isNotBlank(cfgMeta.getConnectionUrl())) {
__mongoClient = new MongoClient(new MongoClientURI(cfgMeta.getConnectionUrl(), _builder));
} else {
String _username = StringUtils.trimToNull(cfgMeta.getUserName());
String _password = StringUtils.trimToNull(cfgMeta.getPassword());
if (_username != null && _password != null) {
if (__cfgMeta.isPasswordEncrypted() && __cfgMeta.getPasswordClass() != null) {
_password = __cfgMeta.getPasswordClass().newInstance().decrypt(_password);
}
MongoCredential _credential = MongoCredential.createCredential(cfgMeta.getUserName(), cfgMeta.getDatabaseName(), _password == null ? null : _password.toCharArray());
__mongoClient = new MongoClient(cfgMeta.getServers(), Collections.singletonList(_credential), _builder.build());
} else {
__mongoClient = new MongoClient(cfgMeta.getServers(), _builder.build());
}
}
}
示例6: startUp
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
@Override
public void startUp() {
Logger.getLogger("org.mongodb").setLevel(Level.SEVERE);
Logger.getLogger("com.mongodb").setLevel(Level.SEVERE);
AppConfiguration configuration = AppConfiguration.instance();
this.databaseName = configuration.getProperty("database.name");
if (this.databaseName == null) {
throw new VerbumDominiException("Property database.name not found in app-configuration.properties file.");
}
String connectionUrl = configuration.getProperty("mongodb.connection.url");
if (Environments.TEST.equals(configuration.getEnvironment())) {
this.databaseName = "verbum_domini_test";
connectionUrl = "mongodb://localhost";
} else if (Environments.PRODUCTION.equals(configuration.getEnvironment())) {
this.databaseName = System.getenv("MONGOLAB_DB_NAME");
connectionUrl = System.getenv("MONGOLAB_URI");
}
MongoClientOptions.Builder options = this.buildOptions(configuration);
MongoClientURI uri = new MongoClientURI(connectionUrl, options);
this.mongoClient = new MongoClient(uri);
}
示例7: buildOptions
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
private MongoClientOptions.Builder buildOptions(AppConfiguration configuration) {
MongoClientOptions.Builder builder = MongoClientOptions.builder();
if (configuration.getProperty("mongodb.connection.max.per.host") != null) {
builder.connectionsPerHost(Integer.parseInt(configuration.getProperty("mongodb.connection.max.per.host")));
}
if (configuration.getProperty("mongodb.connection.min.per.host") != null) {
builder.minConnectionsPerHost(Integer.parseInt(configuration.getProperty("mongodb.connection.min.per.host")));
}
if (configuration.getProperty("mongodb.connection.life.time") != null) {
builder.maxConnectionLifeTime(Integer.parseInt(configuration.getProperty("mongodb.connection.life.time")));
}
if (configuration.getProperty("mongodb.connection.timeout") != null) {
builder.connectTimeout(Integer.parseInt(configuration.getProperty("mongodb.connection.timeout")));
}
return builder.codecRegistry(this.buildCodecRegistries());
}
示例8: getMongoClientOptions
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
/**
* Returns an options object with defaults overriden where there is a valid
* override.
*
* @return
*/
public MongoClientOptions getMongoClientOptions() {
MongoClientOptions.Builder builder = MongoClientOptions.builder();
if (connectionsPerHost != null) {
builder.connectionsPerHost(connectionsPerHost);
}
if (this.readPreference != null) {
builder.readPreference(readPreference);
}
if (ssl) {
// taken from MongoClientURI, written this way so we don't have to
// construct a URI to connect
builder.socketFactory(getSocketFactory());
}
builder.writeConcern(writeConcern);
return builder.build();
}
示例9: create
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
public Schema create(SchemaPlus parentSchema, String name,
Map<String, Object> operand) {
final String host = (String) operand.get("host");
final String database = (String) operand.get("database");
final String authMechanismName = (String) operand.get("authMechanism");
final MongoClientOptions.Builder options = MongoClientOptions.builder();
final List<MongoCredential> credentials = new ArrayList<>();
if (authMechanismName != null) {
final MongoCredential credential = createCredentials(operand);
credentials.add(credential);
}
return new MongoSchema(host, database, credentials, options.build());
}
示例10: mongo
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
@Bean
public MongoClient mongo() throws UnknownHostException {
// location of db
ServerAddress sa = new ServerAddress(
environment.getProperty("mongodb.host"),
environment.getProperty("mongodb.port", Integer.class)
);
// set optional default parameters here
MongoClientOptions.Builder builder = MongoClientOptions.builder();
// none yet
MongoClientOptions options = builder.build();
return new MongoClient(sa, options);
}
示例11: extractMongoOptions
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
private MongoClientOptions extractMongoOptions(Map<String, String> properties) {
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
String connectionsPerHost = properties.get(DBConstants.MongoDB.CONNECTIONS_PER_HOST);
if (!DBUtils.isEmptyString(connectionsPerHost)) {
builder.connectionsPerHost(Integer.parseInt(connectionsPerHost));
}
String maxWaitTime = properties.get(DBConstants.MongoDB.MAX_WAIT_TIME);
if (!DBUtils.isEmptyString(maxWaitTime)) {
builder.maxWaitTime(Integer.parseInt(maxWaitTime));
}
String connectTimeout = properties.get(DBConstants.MongoDB.CONNECT_TIMEOUT);
if (!DBUtils.isEmptyString(connectTimeout)) {
builder.connectTimeout(Integer.parseInt(connectTimeout));
}
String socketTimeout = properties.get(DBConstants.MongoDB.SOCKET_TIMEOUT);
if (!DBUtils.isEmptyString(socketTimeout)) {
builder.socketTimeout(Integer.parseInt(socketTimeout));
}
String threadsAllowedToBlockForConnectionMultiplier = properties.get(
DBConstants.MongoDB.THREADS_ALLOWED_TO_BLOCK_CONN_MULTIPLIER);
if (!DBUtils.isEmptyString(threadsAllowedToBlockForConnectionMultiplier)) {
builder.threadsAllowedToBlockForConnectionMultiplier(
Integer.parseInt(threadsAllowedToBlockForConnectionMultiplier));
}
return builder.build();
}
示例12: MongoV3
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
public MongoV3(List<ServerAddress> servers, List<MongoCredential> authors) {
Builder options = new MongoClientOptions.Builder();
options.connectionsPerHost(50);// 连接池设置为300个连接,默认为100
options.connectTimeout(15000);// 连接超时,推荐>3000毫秒
options.maxWaitTime(5000); //
options.socketTimeout(500);
options.writeConcern(WriteConcern.W2);
con = new MongoClient(servers, authors, options.build());
setMongoConnect(this);
}
示例13: init
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
/**
* @decription 初始化配置
* @author yi.zhang
* @time 2017年6月2日 下午2:15:57
*/
public void init(String servers,String database,String schema,String username,String password) {
try {
List<ServerAddress> saddress = new ArrayList<ServerAddress>();
if (servers != null && !"".equals(servers)) {
for (String server : servers.split(",")) {
String[] address = server.split(":");
String ip = address[0];
int port = 27017;
if (address != null && address.length > 1) {
port = Integer.valueOf(address[1]);
}
saddress.add(new ServerAddress(ip, port));
}
}
MongoCredential credential = MongoCredential.createScramSha1Credential(username, database,password.toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
Builder builder = new MongoClientOptions.Builder();
builder.maxWaitTime(MAX_WAIT_TIME);
// 通过连接认证获取MongoDB连接
MongoClient client = new MongoClient(saddress, credentials, builder.build());
// 连接到数据库
session = client.getDatabase(schema);
} catch (Exception e) {
logger.error("-----MongoDB Config init Error-----", e);
}
}
示例14: MongoClientOptionsConfig
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
@Autowired
public MongoClientOptionsConfig(Optional<CodecRegistry> codecRegistry) {
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
if (codecRegistry.isPresent()) {
builder.codecRegistry(codecRegistry.get());
}
this.mongoClientOptions = builder.build();
}
示例15: MongoTransporterConfig
import com.mongodb.MongoClientOptions; //导入方法依赖的package包/类
public MongoTransporterConfig(Config config) {
try {
MongoClientOptions.Builder builder = MongoClientOptions.builder().connectTimeout(5000).socketTimeout(5000).serverSelectionTimeout(3000);
client = new MongoClient(new ServerAddress(config.getConfigString("host"), config.getConfigInt("port")), builder.build());
client.getAddress();
database = client.getDatabase(config.getConfigString("database"));
collection = database.getCollection(config.getConfigString("collection"));
} catch (MongoTimeoutException e) {
client.close();
log.error("MongoClient initial failed", e);
throw new LogregatorException();
}
}