本文整理汇总了Java中com.mongodb.MongoClientOptions.Builder类的典型用法代码示例。如果您正苦于以下问题:Java Builder类的具体用法?Java Builder怎么用?Java Builder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Builder类属于com.mongodb.MongoClientOptions包,在下文中一共展示了Builder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getOptions
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
private MongoClientOptions getOptions() {
Builder builder = new Builder();
if (this.connectionsPerHost != null) {
builder.connectionsPerHost(connectionsPerHost);
}
if (this.threadsAllowedToBlockForConnectionMultiplier != null) {
builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);
}
if (this.maxWaitTime != null) {
builder.maxWaitTime(maxWaitTime);
}
if (this.connectTimeout != null) {
builder.connectTimeout(connectTimeout);
}
if (this.socketTimeout != null) {
builder.socketTimeout(socketTimeout);
}
if (this.socketKeepAlive != null) {
builder.socketKeepAlive(socketKeepAlive);
}
// database configure
return builder.build();
}
示例2: connect
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
public MongoDatabase connect(String host, int port, String user, String password) {
Builder o = MongoClientOptions.builder().serverSelectionTimeout(3000);
String databaseName = "djigger";
List<MongoCredential> credentials = new ArrayList<>();
if (user != null && password != null && !user.trim().isEmpty() && !password.trim().isEmpty()) {
credentials.add(MongoCredential.createCredential(user, databaseName, password.toCharArray()));
}
mongoClient = new MongoClient(new ServerAddress(host,port), credentials, o.build());
// call this method to check if the connection succeeded as the mongo client lazy loads the connection
mongoClient.getAddress();
db = mongoClient.getDatabase(databaseName);
return db;
}
示例3: connect
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
public MongoDbConnection connect(ConnectionSettings connectionSettings) {
StringBuilder authString = new StringBuilder();
String user = connectionSettings.getUser();
if (user != null && !user.isEmpty()) {
authString.append(user);
String password = connectionSettings.getPassword();
if (password != null && !password.isEmpty()) {
authString.append(":").append(password);
}
authString.append("@");
}
String uri = String.format("mongodb://%s%s", authString, connectionSettings.getHost());
Builder options = MongoClientOptions.builder().serverSelectionTimeout(10000);
MongoClient client = new MongoClient(new MongoClientURI(uri, options));
MongoConnection mongoConnection = new MongoConnection(client);
return new MongoDbConnection(mongoConnection, connectionSettings);
}
示例4: options
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
private MongoClientOptions.Builder options(final Config config) {
MongoClientOptions.Builder builder = MongoClientOptions.builder();
builder.connectionsPerHost(config.getInt("connectionsPerHost"));
builder.threadsAllowedToBlockForConnectionMultiplier(
config.getInt("threadsAllowedToBlockForConnectionMultiplier"));
builder.maxWaitTime((int) config.getDuration("maxWaitTime", TimeUnit.MILLISECONDS));
builder.connectTimeout((int) config.getDuration("connectTimeout", TimeUnit.MILLISECONDS));
builder.socketTimeout((int) config.getDuration("socketTimeout", TimeUnit.MILLISECONDS));
builder.socketKeepAlive(config.getBoolean("socketKeepAlive"));
builder.cursorFinalizerEnabled(config.getBoolean("cursorFinalizerEnabled"));
builder.alwaysUseMBeans(config.getBoolean("alwaysUseMBeans"));
builder.heartbeatFrequency(config.getInt("heartbeatFrequency"));
builder.minHeartbeatFrequency(config.getInt("minHeartbeatFrequency"));
builder.heartbeatConnectTimeout(
(int) config.getDuration("heartbeatConnectTimeout", TimeUnit.MILLISECONDS));
builder.heartbeatSocketTimeout(
(int) config.getDuration("heartbeatSocketTimeout", TimeUnit.MILLISECONDS));
return builder;
}
示例5: MongoV3
import com.mongodb.MongoClientOptions.Builder; //导入依赖的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);
}
示例6: createMongoOptions
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
private MongoClientOptions createMongoOptions(MongoDataSourceSetting setting){
Builder option = MongoClientOptions.builder()
.writeConcern(setting.getWriteConcern())
.connectionsPerHost(setting.getConnectionsPerHost())
.socketKeepAlive(setting.getSocketKeepAlive());
return option.build();
}
示例7: init
import com.mongodb.MongoClientOptions.Builder; //导入依赖的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);
}
}
示例8: getInstance
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
private static MongoClient getInstance(){
if (client == null) {
Properties prop = PropertiesUtil.loadProperties("mongodb.properties");
String host = PropertiesUtil.getString(prop, "host");
int port = PropertiesUtil.getInt(prop, "port");
defauleDbName = "admin";
try {
client = new MongoClient(host, port);
} catch (UnknownHostException e) {
logger.info("{}", e);
}
// or, to connect to a replica set, with auto-discovery of the primary, supply a seed list of members
// List<ServerAddress> listHost = Arrays.asList(new ServerAddress("localhost", 27017),new ServerAddress("localhost", 27018));
// instance.mongoClient = new MongoClient(listHost);
// 大部分用户使用mongodb都在安全内网下,但如果将mongodb设为安全验证模式,就需要在客户端提供用户名和密码:
//boolean auth = client.authenticate(myUserName, myPassword);
Builder options = new MongoClientOptions.Builder();
options.connectionsPerHost(300); // 连接池设置为300个连接,默认为100
options.connectTimeout(15000); // 连接超时,推荐>3000毫秒
options.maxWaitTime(5000);
options.socketTimeout(0); // 套接字超时时间,0无限制
options.threadsAllowedToBlockForConnectionMultiplier(5000);// 线程队列数,如果连接线程排满了队列就会抛出 "Out of semaphores to get db"错误。
options.writeConcern(WriteConcern.SAFE);
options.build();
}
return client;
}
示例9: MorphiaManager
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
private MorphiaManager(String host, int port) {
try {
if (mongoClient == null) {
Builder optionsBuilder = new MongoClientOptions.Builder().socketKeepAlive(true);
mongoClient = new MongoClient(new MongoClientURI(
host != null ? "mongodb://" + host + ':' + port : "mongodb://localhost:27017",
optionsBuilder));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例10: createDefaultClientOptionsBuilder
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
private static MongoClientOptions.Builder createDefaultClientOptionsBuilder(Properties connProperties )
{
Builder clientOptionsBuilder = new MongoClientOptions.Builder( );
if ( connProperties != null )
{
if ( hasKeepSocketAlive( connProperties ) ) // need to change
// setting,
// as MongoDB default is
// false
clientOptionsBuilder.socketKeepAlive( true );
}
return clientOptionsBuilder;
}
示例11: getMongoURI
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
private static MongoClientURI getMongoURI( Properties connProps,
MongoClientOptions.Builder clientOptionsBuilder ) throws Exception
{
// check if explicitly indicated not to use URI, even if URI value exists
Boolean ignoreURI = getBooleanPropValue( connProps, IGNORE_URI_PROP );
if( ignoreURI != null && ignoreURI )
return null;
String uri = getStringPropValue( connProps, MONGO_URI_PROP );
if( uri == null || uri.isEmpty() )
return null;
try
{
if ( clientOptionsBuilder != null )
{
return new MongoClientURI( uri, clientOptionsBuilder );
}
else
{
return new MongoClientURI( uri );
}
}
catch( Exception ex )
{
// log and ignore
getLogger().log( Level.INFO, Messages.bind( "Invalid Mongo Database URI: {0}", uri ), ex ); //$NON-NLS-1$
throw ex;
}
//return null;
}
示例12: getObject
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
@Override
public MongoClientOptions.Builder getObject() throws Exception {
Builder builder = MongoClientOptions.builder();
builder.socketTimeout(this.socketTimeout);
builder.connectTimeout(this.connectTimeout);
return builder;
}
示例13: builder
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
private Builder builder(MongoClientOptions options) {
if (options != null) {
return MongoClientOptions.builder(options);
}
return MongoClientOptions.builder();
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:7,代码来源:MongoProperties.java
示例14: options
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
@Bean
public Builder options() {
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry(),
CodecRegistries.fromCodecs(new LocalDateTimeCodec(), new HostAndPortCodec(), new LocalDateTimeCodec()));
return MongoClientOptions.builder().codecRegistry(codecRegistry);
}
示例15: createMongoDbFactory
import com.mongodb.MongoClientOptions.Builder; //导入依赖的package包/类
private SimpleMongoDbFactory createMongoDbFactory(MongoServiceInfo serviceInfo, MongoClientOptions.Builder mongoOptionsToUse) throws UnknownHostException {
MongoClientURI mongoClientURI = new MongoClientURI(serviceInfo.getUri(), mongoOptionsToUse);
MongoClient mongo = new MongoClient(mongoClientURI);
return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase());
}