當前位置: 首頁>>代碼示例>>Java>>正文


Java MongoClientOptions.Builder方法代碼示例

本文整理匯總了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();
}
 
開發者ID:apache,項目名稱:beam,代碼行數:22,代碼來源:MongoDbIO.java

示例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);
}
 
開發者ID:heisedebaise,項目名稱:tephra,代碼行數:27,代碼來源:MongoImpl.java

示例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);
            }
        }
    }
 
開發者ID:dadrus,項目名稱:jpa-unit,代碼行數:18,代碼來源:AbstractConfiguration.java

示例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());
    }
}
 
開發者ID:CenturyLinkCloud,項目名稱:mdw,代碼行數:20,代碼來源:DatabaseAccess.java

示例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());
        }
    }
}
 
開發者ID:suninformation,項目名稱:ymate-platform-v2,代碼行數:26,代碼來源:MongoDataSourceAdapter.java

示例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);
}
 
開發者ID:aureliano,項目名稱:verbum-domini,代碼行數:27,代碼來源:PersistenceManagerImpl.java

示例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());
}
 
開發者ID:aureliano,項目名稱:verbum-domini,代碼行數:21,代碼來源:PersistenceManagerImpl.java

示例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();
}
 
開發者ID:lightblue-platform,項目名稱:lightblue-mongo,代碼行數:27,代碼來源:MongoConfiguration.java

示例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());
}
 
開發者ID:apache,項目名稱:calcite,代碼行數:17,代碼來源:MongoSchemaFactory.java

示例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);
}
 
開發者ID:bjornharvold,項目名稱:bearchoke,代碼行數:18,代碼來源:MongoLocalConfig.java

示例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();
}
 
開發者ID:wso2,項目名稱:carbon-data,代碼行數:27,代碼來源:MongoConfig.java

示例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);
}
 
開發者ID:jiumao-org,項目名稱:wechat-mall,代碼行數:11,代碼來源:MongoV3.java

示例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);
	}
}
 
開發者ID:dev-share,項目名稱:database-transform-tool,代碼行數:33,代碼來源:MongoDBFactory.java

示例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();
}
 
開發者ID:axelspringer,項目名稱:polymorphia,代碼行數:9,代碼來源:MongoClientOptionsConfig.java

示例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();
    }
}
 
開發者ID:geminiKim,項目名稱:logregator,代碼行數:14,代碼來源:MongoTransporterConfig.java


注:本文中的com.mongodb.MongoClientOptions.Builder方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。