List of usage examples for com.mongodb MongoClientOptions builder
public static Builder builder()
From source file:org.jooby.mongodb.Mongodb.java
License:Apache License
/** * Set an options callback.// w w w . j av a 2 s . c o m * * <pre> * { * use(new Mongodb() * .options((options, config) {@literal ->} { * options.connectionsPerHost(100); * }) * ); * } * </pre> * * @param options Configure callback. * @return This module */ public Mongodb options(final BiConsumer<MongoClientOptions.Builder, Config> options) { this.options = requireNonNull(options, "Options callback is required."); return this; }
From source file:org.kaaproject.kaa.server.appenders.mongo.appender.LogEventMongoDao.java
License:Apache License
/** * Create new instance of <code>LogEventMongoDao</code> using configuration instance of * <code>MongoDbConfig</code>. * * @param configuration the configuration of log event mongo dao, it contain server size, * credentials, max wait time, etc. *//*from ww w . j a v a 2 s . com*/ @SuppressWarnings("deprecation") public LogEventMongoDao(MongoDbConfig configuration) throws Exception { List<ServerAddress> seeds = new ArrayList<>(configuration.getMongoServers().size()); for (MongoDbServer server : configuration.getMongoServers()) { seeds.add(new ServerAddress(server.getHost(), server.getPort())); } List<MongoCredential> credentials = new ArrayList<>(); if (configuration.getMongoCredentials() != null) { for (MongoDBCredential credential : configuration.getMongoCredentials()) { credentials.add(MongoCredential.createMongoCRCredential(credential.getUser(), configuration.getDbName(), credential.getPassword().toCharArray())); } } MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder(); if (configuration.getConnectionsPerHost() != null) { optionsBuilder.connectionsPerHost(configuration.getConnectionsPerHost()); } if (configuration.getMaxWaitTime() != null) { optionsBuilder.maxWaitTime(configuration.getMaxWaitTime()); } if (configuration.getConnectionTimeout() != null) { optionsBuilder.connectTimeout(configuration.getConnectionTimeout()); } if (configuration.getSocketTimeout() != null) { optionsBuilder.socketTimeout(configuration.getSocketTimeout()); } if (configuration.getSocketKeepalive() != null) { optionsBuilder.socketKeepAlive(configuration.getSocketKeepalive()); } MongoClientOptions options = optionsBuilder.build(); mongoClient = new MongoClient(seeds, credentials, options); MongoDbFactory dbFactory = new SimpleMongoDbFactory(mongoClient, configuration.getDbName()); MappingMongoConverter converter = new MappingMongoConverter(dbFactory, new MongoMappingContext()); converter.setTypeMapper(new DefaultMongoTypeMapper(null)); mongoTemplate = new MongoTemplate(dbFactory, converter); mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION); }
From source file:org.lambda3.indra.mongo.test.MongoIndraDriverIT.java
License:Open Source License
@BeforeTest private void configure() { final String mongoURI = System.getProperty("indra.mongoURI"); if (mongoURI == null) { Assert.fail(//w w w . j ava 2 s . c o m "System.getProperty(\"indra.mongoURI\") is null. Provide a mongoURI to execute the integration test."); } MongoClientOptions builder = MongoClientOptions.builder().serverSelectionTimeout(5000).build(); MongoClient mongoClient = new MongoClient(mongoURI, builder); vectorSpaceFactory = new MongoVectorSpaceFactory(mongoClient); translatorFactory = new MongoTranslatorFactory(mongoClient); }
From source file:org.mule.module.mongo.MongoCloudConnector.java
License:Open Source License
private MongoClientOptions.Builder getMongoOptions(String database) { final MongoClientOptions.Builder options = MongoClientOptions.builder(); if (connectionsPerHost != null) { options.connectionsPerHost(connectionsPerHost); }/*ww w . j av a 2s . c o m*/ if (threadsAllowedToBlockForConnectionMultiplier != null) { options.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier); } if (maxWaitTime != null) { options.maxWaitTime(maxWaitTime); } if (connectTimeout != null) { options.connectTimeout(connectTimeout); } if (socketTimeout != null) { options.socketTimeout(socketTimeout); } if (autoConnectRetry != null) { options.autoConnectRetry(autoConnectRetry); } if (database != null) { this.database = database; } return options; }
From source file:org.nuxeo.directory.mongodb.MongoDBConnectionHelper.java
License:Apache License
/** * Initialize a connection to the MongoDB server * * @param server the server url//from w w w . j a va 2 s . co m * @return the MongoDB client */ public static MongoClient newMongoClient(String server) { if (StringUtils.isBlank(server)) { throw new NuxeoException("Missing <server> in MongoDB repository descriptor"); } MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder() // Can help to prevent firewall disconnects // inactive connection, option not available from URI .socketKeepAlive(true) // don't wait for ever by default, // can be overridden using URI options .connectTimeout(MONGODB_OPTION_CONNECTION_TIMEOUT_MS) .socketTimeout(MONGODB_OPTION_SOCKET_TIMEOUT_MS).description("Nuxeo"); MongoClient client; if (server.startsWith("mongodb://")) { // allow mongodb:// URI syntax for the server, to pass everything in one string client = new MongoClient(new MongoClientURI(server, optionsBuilder)); } else { client = new MongoClient(new ServerAddress(server), optionsBuilder.build()); } if (log.isDebugEnabled()) { log.debug("MongoClient initialized with options: " + client.getMongoClientOptions().toString()); } return client; }
From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBChecker.java
License:Apache License
@Override public void check(ConfigurationGenerator cg) throws ConfigurationException { MongoClient ret = null;/*from ww w. j a va2s . c om*/ String serverName = cg.getUserConfig().getProperty(ConfigurationGenerator.PARAM_MONGODB_SERVER); String dbName = cg.getUserConfig().getProperty(ConfigurationGenerator.PARAM_MONGODB_NAME); MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder() .serverSelectionTimeout((int) TimeUnit.SECONDS.toMillis(1)).description("Nuxeo DB Check"); if (serverName.startsWith("mongodb://")) { // allow mongodb:// URI syntax for the server, to pass everything in one string ret = new MongoClient(new MongoClientURI(serverName, optionsBuilder)); } else { ret = new MongoClient(new ServerAddress(serverName), optionsBuilder.build()); } try { Document ping = new Document("ping", "1"); ret.getDatabase(dbName).runCommand(ping); } catch (MongoTimeoutException e) { throw new ConfigurationException( String.format("Unable to connect to MongoDB at %s, please check your connection", serverName)); } finally { ret.close(); } }
From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBRepository.java
License:Apache License
public static MongoClient newMongoClient(MongoDBRepositoryDescriptor descriptor) throws UnknownHostException { MongoClient ret;/*from w ww .j av a2 s . c om*/ String server = descriptor.server; if (StringUtils.isBlank(server)) { throw new NuxeoException("Missing <server> in MongoDB repository descriptor"); } MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder() // Can help to prevent firewall disconnects inactive connection, option not available from URI .socketKeepAlive(true) // don't wait for ever by default, can be overridden using URI options .connectTimeout(MONGODB_OPTION_CONNECTION_TIMEOUT_MS) .socketTimeout(MONGODB_OPTION_SOCKET_TIMEOUT_MS).description("Nuxeo"); if (server.startsWith("mongodb://")) { // allow mongodb:// URI syntax for the server, to pass everything in one string ret = new MongoClient(new MongoClientURI(server, optionsBuilder)); } else { ret = new MongoClient(new ServerAddress(server), optionsBuilder.build()); } if (log.isDebugEnabled()) { log.debug("MongoClient initialized with options: " + ret.getMongoClientOptions().toString()); } return ret; }
From source file:org.opencb.commons.datastore.mongodb.MongoDataStoreManager.java
License:Apache License
private MongoDataStore create(String database, MongoDBConfiguration mongoDBConfiguration) { MongoDataStore mongoDataStore = null; MongoClient mc = null;//from w w w .ja v a 2 s . c o m logger.debug( "MongoDataStoreManager: creating a MongoDataStore object for database: '" + database + "' ..."); long t0 = System.currentTimeMillis(); if (database != null && !database.trim().equals("")) { // read DB configuration for that SPECIES.VERSION, by default // PRIMARY_DB is selected // String dbPrefix = applicationProperties.getProperty(speciesVersionPrefix + ".DB", "PRIMARY_DB"); // We create the MongoClientOptions MongoClientOptions mongoClientOptions; MongoClientOptions.Builder builder = new MongoClientOptions.Builder() .connectionsPerHost( mongoDBConfiguration.getInt(CONNECTIONS_PER_HOST, CONNECTIONS_PER_HOST_DEFAULT)) .connectTimeout(mongoDBConfiguration.getInt(CONNECT_TIMEOUT, CONNECT_TIMEOUT_DEFAULT)) .readPreference(ReadPreference.valueOf( mongoDBConfiguration.getString(READ_PREFERENCE, READ_PREFERENCE_DEFAULT.getValue()))); if (mongoDBConfiguration.getString(REPLICA_SET) != null && !mongoDBConfiguration.getString(REPLICA_SET).isEmpty()) { logger.debug("Setting replicaSet to " + mongoDBConfiguration.getString(REPLICA_SET)); builder = builder.requiredReplicaSetName(mongoDBConfiguration.getString(REPLICA_SET)); } if (mongoDBConfiguration.getBoolean(SSL_ENABLED)) { logger.debug("SSL connections enabled for " + database); builder = builder.sslEnabled(mongoDBConfiguration.getBoolean(SSL_ENABLED)); } mongoClientOptions = builder.build(); assert (dataStoreServerAddresses != null); // We create the MongoCredential object String user = mongoDBConfiguration.getString(USERNAME, ""); String pass = mongoDBConfiguration.getString(PASSWORD, ""); MongoCredential mongoCredential = null; if ((user != null && !user.equals("")) || (pass != null && !pass.equals(""))) { // final DB authenticationDatabase; if (mongoDBConfiguration.get(AUTHENTICATION_DATABASE) != null && !mongoDBConfiguration.getString(AUTHENTICATION_DATABASE).isEmpty()) { // authenticationDatabase = mc.getDB(mongoDBConfiguration.getString("authenticationDatabase")); mongoCredential = MongoCredential.createScramSha1Credential(user, mongoDBConfiguration.getString(AUTHENTICATION_DATABASE), pass.toCharArray()); } else { // authenticationDatabase = db; mongoCredential = MongoCredential.createScramSha1Credential(user, "", pass.toCharArray()); } // authenticationDatabase.authenticate(user, pass.toCharArray()); } mc = newMongoClient(mongoClientOptions, mongoCredential); // mc.setReadPreference(ReadPreference.secondary(new BasicDBObject("dc", "PG"))); // mc.setReadPreference(ReadPreference.primary()); // System.out.println("Replica Status: "+mc.getReplicaSetStatus()); logger.debug(mongoDBConfiguration.toString()); MongoDatabase db = mc.getDatabase(database); // db.setReadPreference(ReadPreference.secondary(new BasicDBObject("dc", "PG"))); // db.setReadPreference(ReadPreference.primary()); long t1 = System.currentTimeMillis(); logger.debug("MongoDataStoreManager: MongoDataStore object for database: '" + database + "' created in " + (t0 - t1) + "ms"); mongoDataStore = new MongoDataStore(mc, db, mongoDBConfiguration); } else { logger.debug("MongoDB database is null or empty"); } return mongoDataStore; }
From source file:org.opencb.commons.datastore.mongodb.MongoDataStoreManager.java
License:Apache License
private MongoClient newMongoClient() { return newMongoClient(new MongoClientOptions.Builder().build(), null); }
From source file:org.pentaho.mongo.MongoProp.java
License:Open Source License
public void setOption(MongoClientOptions.Builder builder, MongoProperties props, MongoPropToOption propToOption) throws MongoDbException { //default is do nothing since some of the Props are not a MongoClientOption }