List of usage examples for com.mongodb MongoClientOptions.Builder MongoClientOptions.Builder
MongoClientOptions.Builder
From source file:org.apache.tapestry5.internal.mongodb.MongoDBSourceImpl.java
License:Apache License
public MongoDBSourceImpl(Logger logger, @Symbol(MongoDBSymbols.CONNECTIONS_PER_HOSTS) int connectionPerHost, @Symbol(MongoDBSymbols.READ_PREFERENCE) ReadPreference readPreference, @Symbol(MongoDBSymbols.WRITE_CONCERN) WriteConcern writeConcern, List<ServerAddress> serverAddresses) { this.logger = logger; MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(connectionPerHost) .writeConcern(writeConcern).readPreference(readPreference).build(); if (serverAddresses.isEmpty()) { try {/*from ww w . j a va 2 s . c o m*/ mongoClient = new MongoClient(new ServerAddress(), options); } catch (UnknownHostException uhe) { throw new RuntimeException(uhe); } } else { mongoClient = new MongoClient(serverAddresses, options); } }
From source file:org.infinispan.loaders.mongodb.MongoDBCacheStore.java
License:Open Source License
@Override public void start() throws CacheLoaderException { super.start(); try {/*w w w . j a v a 2 s. co m*/ MongoClientOptions.Builder optionBuilder = new MongoClientOptions.Builder(); optionBuilder.connectTimeout(this.cfg.getTimeout()); WriteConcern writeConcern = new WriteConcern(this.cfg.getAcknowledgment()); optionBuilder.writeConcern(writeConcern); log.connectingToMongo(this.cfg.getHost(), this.cfg.getPort(), this.cfg.getTimeout(), this.cfg.getAcknowledgment()); ServerAddress serverAddress = new ServerAddress(this.cfg.getHost(), this.cfg.getPort()); this.mongo = new MongoClient(serverAddress, optionBuilder.build()); } catch (UnknownHostException e) { throw log.mongoOnUnknownHost(this.cfg.getHost()); } catch (RuntimeException e) { throw log.unableToInitializeMongoDB(e); } mongoDb = extractDatabase(); this.collection = mongoDb.getCollection(this.cfg.getCollectionName()); }
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 ww w.j a v a 2 s .co 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.socialhistoryservices.dao.MongoDBSingleton.java
License:Open Source License
public MongoDBSingleton(String[] hosts, int connectionsPerHost, int writeConcern) { final MongoClientOptions.Builder builder = new MongoClientOptions.Builder() .connectionsPerHost(connectionsPerHost).writeConcern(new WriteConcern(writeConcern)); setMongo(hosts, builder.build());/*from w ww.jav a2 s.com*/ }
From source file:thingynet.mongo.MongoConfig.java
License:Apache License
@Bean MongoClient mongoClient() throws IOException { ArrayList<ServerAddress> servers = new ArrayList<>(); for (int i = 0; i < mongoHosts.length; i++) { servers.add(new ServerAddress(mongoHosts[i], mongoPorts[i])); }/*from w w w . ja v a2s .c o m*/ WriteConcern writeConcern = WriteConcern.valueOf(mongoWriteConcern); ReadPreference readPreference = ReadPreference.valueOf(mongoReadPreference); MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(mongoConnectionsPerHost) .writeConcern(writeConcern).readPreference(readPreference).build(); return new MongoClient(servers, options); }