List of usage examples for com.mongodb MongoClientOptions builder
public static Builder builder()
From source file:com.ait.tooling.server.mongodb.support.spring.MongoDBDescriptor.java
License:Open Source License
private final boolean init() { try {/*from w w w . j av a 2s . com*/ if (null == m_baseprop) { m_baseprop = MongoDBContextInstance.getMongoDBContextInstance().getMongoDBProvider() .getMongoDBDefaultPropertiesBase(); } final IPropertiesResolver prop = ServerContextInstance.getServerContextInstance() .getPropertiesResolver(); setName(prop.getPropertyByName(m_baseprop + ".name")); setDefaultDB(prop.getPropertyByName(m_baseprop + ".db")); setReplicas(Boolean.valueOf(prop.getPropertyByName(m_baseprop + ".replicas", "false"))); setCreateID(Boolean.valueOf(prop.getPropertyByName(m_baseprop + ".createid", "false"))); final ArrayList<ServerAddress> addrlist = new ArrayList<ServerAddress>(); for (String name : StringOps.requireTrimOrNull(prop.getPropertyByName(m_baseprop + ".host.list")) .split(",")) { name = StringOps.toTrimOrNull(name); if (null != name) { final String addr = StringOps .requireTrimOrNull(prop.getPropertyByName(m_baseprop + ".host." + name + ".addr")); final String port = StringOps .requireTrimOrNull(prop.getPropertyByName(m_baseprop + ".host." + name + ".port")); addrlist.add(new ServerAddress(addr, Integer.valueOf(port))); } } if (addrlist.isEmpty()) { throw new IllegalArgumentException("no MongoDB server address"); } m_addrlist = addrlist; m_authlist = new ArrayList<MongoCredential>(); final String temp = StringOps.toTrimOrNull(prop.getPropertyByName(m_baseprop + ".auth.list")); if (null != temp) { for (String name : temp.split(",")) { name = StringOps.toTrimOrNull(name); if (null != name) { final String user = StringOps .requireTrimOrNull(prop.getPropertyByName(m_baseprop + ".auth." + name + ".user")); final String pass = StringOps .requireTrimOrNull(prop.getPropertyByName(m_baseprop + ".auth." + name + ".pass")); final String data = StringOps .requireTrimOrNull(prop.getPropertyByName(m_baseprop + ".auth." + name + ".db")); m_authlist.add(MongoCredential.createCredential(user, data, pass.toCharArray())); } } } if (null == getClientOptions()) { setClientOptions(MongoClientOptions.builder().connectionsPerHost(getConnectionPoolSize()) .threadsAllowedToBlockForConnectionMultiplier(getConnectionMultiplier()) .connectTimeout(getConnectionTimeout()).build()); } m_doptions = new LinkedHashMap<String, IMongoDBOptions>(); final String conf = StringOps.toTrimOrNull(prop.getPropertyByName(m_baseprop + ".dbconfig.list")); if (null != conf) { for (String name : conf.split(",")) { name = StringOps.toTrimOrNull(name); if (null != name) { if (null == m_doptions.get(name)) { boolean doid = isCreateID(); final ArrayList<IMongoDBCollectionOptions> list = new ArrayList<IMongoDBCollectionOptions>(); final String dbid = StringOps.toTrimOrNull( prop.getPropertyByName(m_baseprop + ".dbconfig." + name + ".createid")); if (null != dbid) { doid = Boolean.valueOf(dbid); } final String base = m_baseprop + ".dbconfig." + name + ".collections"; final String cols = StringOps.toTrimOrNull(prop.getPropertyByName(base)); if (null != cols) { for (String coln : cols.split(",")) { coln = StringOps.toTrimOrNull(coln); if (null != coln) { final String icid = StringOps.toTrimOrNull( prop.getPropertyByName(base + "." + coln + ".createid")); if (null != icid) { list.add(new MongoDBCollectionOptions(coln, Boolean.valueOf(icid))); } else { list.add(new MongoDBCollectionOptions(coln, doid)); } } } } m_doptions.put(name, new MongoDBOptions(name, doid, list)); } } } } return true; } catch (Exception e) { logger.error("error initializing MongoDBDescriptor", e); return false; } }
From source file:com.almende.eve.state.mongo.MongoStateFactory.java
License:Apache License
private static MongoClient createClient(String databaseUri, int port) throws UnknownHostException { MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(100) .threadsAllowedToBlockForConnectionMultiplier(1500).build(); MongoClient client = new MongoClient(new ServerAddress(databaseUri, port), options); return client; }
From source file:com.apifest.oauth20.MongoUtil.java
License:Apache License
public static MongoClient getMongoClient() { if (mongoClient == null) { try {/* w ww . jav a 2 s . co m*/ MongoClientOptions.Builder options = new MongoClientOptions.Builder().connectionsPerHost(100) .connectTimeout(2).threadsAllowedToBlockForConnectionMultiplier(1); final MongoClientURI mongoClientURI = new MongoClientURI(OAuthServer.getDbURI(), options); mongoClient = new MongoClient(mongoClientURI); if (mongoClientURI.getDatabase() != null) { database = mongoClientURI.getDatabase(); } } catch (UnknownHostException e) { log.error("Cannot connect to DB", e); } } return mongoClient; }
From source file:com.apifest.oauth20.persistence.mongodb.MongoUtil.java
License:Apache License
public static MongoClient getMongoClient(String uri) { if (mongoClient == null) { try {/* w w w . j av a 2 s. co m*/ MongoClientOptions.Builder options = new MongoClientOptions.Builder().connectionsPerHost(100) .connectTimeout(2000).threadsAllowedToBlockForConnectionMultiplier(1); final MongoClientURI mongoClientURI = new MongoClientURI(uri, options); mongoClient = new MongoClient(mongoClientURI); if (mongoClientURI.getDatabase() != null) { database = mongoClientURI.getDatabase(); } } catch (UnknownHostException e) { log.error("Cannot connect to DB", e); } } return mongoClient; }
From source file:com.appdynamics.monitors.mongo.MongoDBMonitor.java
License:Apache License
private MongoClientOptions getMongoClientSSLOptions(Configuration config) throws MongoMonitorException { MongoClientOptions clientOpts = null; if (config.isSsl()) { String filePath = config.getPemFilePath(); if (!Strings.isNullOrEmpty(filePath)) { try { clientOpts = new MongoClientOptions.Builder().socketFactory(getSocketFactoryFromPEM(filePath)) .build();/*from ww w .j av a 2 s.c o m*/ } catch (Exception e) { logger.error("Error establishing ssl socket factory", e); throw new MongoMonitorException("Error establishing ssl socket factory"); } } else { String msg = "The argument pemFilePath is null or empty in config.yml"; logger.error(msg); throw new MongoMonitorException(msg); } } return clientOpts; }
From source file:com.bacic5i5j.framework.database.MongoContext.java
License:Open Source License
public void init(String db, String username, String password, List servers) { this.dbname = db; MongoCredential mc = MongoCredential.createMongoCRCredential(username, dbname, password.toCharArray()); List mcs = new ArrayList(); mcs.add(mc);//from w ww . ja v a 2s .co m MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(100).connectTimeout(10 * 1000) .readPreference(ReadPreference.secondaryPreferred()).build(); this.mongoClient = new MongoClient(servers, mcs, options); this.threadLocal = new ThreadLocal<DB>(); }
From source file:com.baifendian.swordfish.common.job.struct.datasource.MongoDatasource.java
License:Apache License
@Override public void isConnectable() throws Exception { MongoClient mongoClient = new MongoClient(new MongoClientURI(this.address)); try {//from w ww.j a v a 2 s. co m MongoClientOptions options = MongoClientOptions.builder().connectTimeout(10).socketKeepAlive(false) .build(); MongoDatabase db = mongoClient.getDatabase(this.database); for (Document doc : db.listCollections()) { logger.debug("{}", doc); } } finally { mongoClient.close(); } }
From source file:com.bluedragon.mongo.MongoDSN.java
License:Open Source License
public static MongoClient newClient(String server, String user, String pass, String db) throws UnknownHostException { MongoClientOptions options = MongoClientOptions.builder() .readPreference(ReadPreference.secondaryPreferred()).build(); List<InetSocketAddress> serverList = AddrUtil.getAddresses(server); List<ServerAddress> addrs = new ArrayList<ServerAddress>(); Iterator<InetSocketAddress> it = serverList.iterator(); while (it.hasNext()) { InetSocketAddress isa = it.next(); addrs.add(new ServerAddress(isa.getAddress(), isa.getPort())); }/*from w w w .j av a 2 s .c o m*/ if (user != null) { MongoCredential cred = MongoCredential.createCredential(user, db, pass.toCharArray()); List<MongoCredential> creds = new ArrayList<MongoCredential>(); creds.add(cred); return new MongoClient(addrs, creds, options); } else { return new MongoClient(addrs, options); } }
From source file:com.bnrc.sdn.properties.MongoProperties.java
License:Apache License
/** * Creates a {@link MongoClient} using the given {@code options} and * {@code environment}. If the configured port is zero, the value of the * {@code local.mongo.port} property retrieved from the {@code environment} is used * to configure the client. //from www .j a v a 2 s. c o m * * @param options the options * @param environment the environment * @return the Mongo client * @throws UnknownHostException if the configured host is unknown */ public MongoClient createMongoClient(MongoClientOptions options, Environment environment) throws UnknownHostException { try { if (hasCustomAddress() || hasCustomCredentials()) { if (options == null) { options = MongoClientOptions.builder().build(); } List<MongoCredential> credentials = null; if (hasCustomCredentials()) { String database = this.authenticationDatabase == null ? getMongoClientDatabase() : this.authenticationDatabase; credentials = Arrays.asList( MongoCredential.createMongoCRCredential(this.username, database, this.password)); } String host = this.host == null ? "localhost" : this.host; int port = determinePort(environment); return new MongoClient(Arrays.asList(new ServerAddress(host, port)), credentials, options); } // The options and credentials are in the URI System.out.println("uri=" + uri); return new MongoClient(new MongoClientURI(this.uri, builder(options))); } finally { clearPassword(); } }
From source file:com.bnrc.sdn.properties.MongoProperties.java
License:Apache License
private Builder builder(MongoClientOptions options) { Builder builder = MongoClientOptions.builder(); if (options != null) { builder.alwaysUseMBeans(options.isAlwaysUseMBeans()); builder.connectionsPerHost(options.getConnectionsPerHost()); builder.connectTimeout(options.getConnectTimeout()); builder.cursorFinalizerEnabled(options.isCursorFinalizerEnabled()); builder.dbDecoderFactory(options.getDbDecoderFactory()); builder.dbEncoderFactory(options.getDbEncoderFactory()); builder.description(options.getDescription()); builder.maxWaitTime(options.getMaxWaitTime()); builder.readPreference(options.getReadPreference()); builder.socketFactory(options.getSocketFactory()); builder.socketKeepAlive(options.isSocketKeepAlive()); builder.socketTimeout(options.getSocketTimeout()); builder.threadsAllowedToBlockForConnectionMultiplier( options.getThreadsAllowedToBlockForConnectionMultiplier()); builder.writeConcern(options.getWriteConcern()); }/*from ww w . j av a2 s . c o m*/ return builder; }