/** * Intialize the mongo database connection * @param mongoHost * @param mongoPort */ public static Mongo initMongo(String mongoHost, int mongoPort) { try { ServerAddress address = new ServerAddress(mongoHost, mongoPort); MongoOptions options = new MongoOptions(); options.autoConnectRetry = true; options.connectionsPerHost = GlobalConfig.getInstance().getIntProperty("mongdb.connectionsPerHost"); options.connectTimeout = GlobalConfig.getInstance().getIntProperty("mongdb.connectTimeout"); options.maxWaitTime = GlobalConfig.getInstance().getIntProperty("mongdb.maxWaitTime"); options.socketTimeout = GlobalConfig.getInstance().getIntProperty("mongdb.socketTimeout"); options.threadsAllowedToBlockForConnectionMultiplier = GlobalConfig.getInstance().getIntProperty("mongdb.threadsAllowedToBlockForConnectionMultiplier"); Mongo mongo = new Mongo(address, options); if ( logger.isInfoEnabled() ) { logger.info("MongoDB initialized OK: host:{}", mongoHost); } refreshMongoDatabase(mongo); return mongo; } catch (Exception e) { logger.info("Failed to open mongodb", e); } return null; }
/** * * @param mongoHost * @param mongoPort * @return */ public static Mongo initCfgMongo(String mongoHost, int mongoPort) { try { ServerAddress address = new ServerAddress(mongoHost, mongoPort); MongoOptions options = new MongoOptions(); options.autoConnectRetry = true; Mongo mongo = new Mongo(address, options); if ( logger.isInfoEnabled() ) { logger.info("MongoDB for cfg is initialized OK: host:{}", mongoHost); } for ( String cfgColl : CFG_COLL ) { String key = StringUtil.concat(cfgNamespace, Constant.DOT, cfgColl); if ( !mongoMap.containsKey(key) ) { //Put it into our cache mongoMap.put(key, mongo); logger.debug("Put the cfg db mongo key: {} for server", key); } else { logger.warn("Key:{} is duplicate in mongo database", key); } } return mongo; } catch (Exception e) { logger.info("Failed to open mongodb", e); } return null; }
@Bean public MongoFactoryBean mongoFactory() throws Exception { MongoOptions mongoOptions = new MongoOptions(); mongoOptions.connectionsPerHost = connections; MongoFactoryBean mongoFactoryBean = new MongoFactoryBean(); //check multiple urls for replica sets if(isReplicaSet()) { logger.debug("connecting to mongo replica sets"); mongoFactoryBean.setReplicaSetSeeds(getServerAddresses()); mongoOptions.autoConnectRetry = true; mongoOptions.connectTimeout = connectionTimeout; mongoOptions.socketTimeout = socketTimeout; } else { logger.debug("connecting to mongo master-slave"); mongoFactoryBean.setHost(dbUrl); } mongoFactoryBean.setMongoOptions(mongoOptions); return mongoFactoryBean; }
/** * Configure Mongo with the given config and open a connection, storing the DBCollection object statically. * @param conf Hadoop configuration object. */ protected static void setMongoParams(Configuration conf) { String host = conf.get(MONGO_HOST); String db = conf.get(MONGO_DB_NAME); String collection = conf.get(MONGO_COLL_NAME); int port = Integer.parseInt(conf.get(MONGO_PORT)); try { MongoOptions options = new MongoOptions(); options.autoConnectRetry = true; options.connectTimeout = 10000; // timeout is 10s options.connectionsPerHost = 50; // default is 10 options.threadsAllowedToBlockForConnectionMultiplier = 500; // default is 5 options.fsync = false; // don't wait for disk flush options.w = 1; // ensure write to memory succeeded Mongo mongo = new Mongo(new ServerAddress(host, port), options); mongoColl = mongo.getDB(db).getCollection(collection); } catch (Exception e) { throw new RuntimeException("Could not connect to mongo host " + host + ":" + port, e); } }
private MongoOptions getMongoOptions(NaviPoolConfig poolConfig) { if (poolConfig instanceof NaviMongoPoolConfig) { return ((NaviMongoPoolConfig) poolConfig).getOptions(); } MongoOptions options = new MongoOptions(); options.connectionsPerHost = poolConfig.getMaxActive(); options.connectTimeout = poolConfig.getConnectTimeout(); options.safe = true; return options; }
/** * Intialize the mongo database connection * @param mongoHost * @param mongoPort */ public static Mongo initUserMongo(String mongoHost, int mongoPort) { try { ServerAddress address = new ServerAddress(mongoHost, mongoPort); MongoOptions options = new MongoOptions(); options.autoConnectRetry = true; options.connectionsPerHost = GlobalConfig.getInstance().getIntProperty("mongdb.connectionsPerHost"); options.connectTimeout = GlobalConfig.getInstance().getIntProperty("mongdb.connectTimeout"); options.maxWaitTime = GlobalConfig.getInstance().getIntProperty("mongdb.maxWaitTime"); options.socketTimeout = GlobalConfig.getInstance().getIntProperty("mongdb.socketTimeout"); options.threadsAllowedToBlockForConnectionMultiplier = GlobalConfig.getInstance().getIntProperty("mongdb.threadsAllowedToBlockForConnectionMultiplier"); Mongo mongo = new Mongo(address, options); if ( logger.isInfoEnabled() ) { logger.info("MongoDB for users is initialized OK: host:{}", mongoHost); } for ( String userColl : USER_COLL ) { String key = StringUtil.concat(userNamespace, Constant.DOT, userColl); if ( !mongoMap.containsKey(key) ) { //Put it into our cache mongoMap.put(key, mongo); logger.debug("Put the user db mongo key: {} for server", key); } else { logger.warn("Key:{} is duplicate in mongo database", key); } } return mongo; } catch (Exception e) { logger.info("Failed to open mongodb", e); } return null; }
/** * Intialize the mongo database connection * @param mongoHost * @param mongoPort */ public static Mongo initUserMongo(String mongoHost, int mongoPort) { try { ServerAddress address = new ServerAddress(mongoHost, mongoPort); MongoOptions options = new MongoOptions(); options.autoConnectRetry = true; options.connectionsPerHost = GlobalConfig.getInstance().getIntProperty("mongdb.connectionsPerHost"); options.connectTimeout = GlobalConfig.getInstance().getIntProperty("mongdb.connectTimeout"); options.maxWaitTime = GlobalConfig.getInstance().getIntProperty("mongdb.maxWaitTime"); options.socketTimeout = GlobalConfig.getInstance().getIntProperty("mongdb.socketTimeout"); options.threadsAllowedToBlockForConnectionMultiplier = GlobalConfig.getInstance().getIntProperty("mongdb.threadsAllowedToBlockForConnectionMultiplier"); Mongo mongo = new Mongo(address, options); if ( logger.isInfoEnabled() ) { logger.info("MongoDB for users is initialized OK: host:{}", mongoHost); } for ( String userColl : USER_COLL ) { String key = StringUtil.concat(userNamespace, Constant.DOT, userColl); if ( !mongoMap.containsKey(key) ) { //Put it into our cache mongoMap.put(key, mongo); logger.debug("Put the user db mongo key: {} for server", key); } else { logger.warn("Key:{} is duplicate in mongo database", key); } if ( !mongoMap.containsKey("guildbagevents") ) { } } return mongo; } catch (Exception e) { logger.info("Failed to open mongodb", e); } return null; }
/** * Init the mongo instance * */ private void init() { try { mongo = new Mongo(host, port); MongoOptions options = mongo.getMongoOptions(); options.connectionsPerHost = poolSize; options.threadsAllowedToBlockForConnectionMultiplier = blockSize; } catch (UnknownHostException e) { e.printStackTrace(); } }
@Override public MongoOptions mongoOptions() { MongoClientOptions mongoClientOptions = MongoClientOptions.builder() .autoConnectRetry(true) .connectionsPerHost(50) .connectTimeout(1000 * 10) .maxAutoConnectRetryTime(1000 * 60 * 30) .socketFactory(mongoSocketFactory) .socketKeepAlive(true) .socketTimeout(1000 * 60) .build(); return new MongoOptions(mongoClientOptions); }
public MongoOptions getDefaultMongoOptions() { MongoClientOptions mongoClientOptions = MongoClientOptions.builder() .autoConnectRetry(true) .connectionsPerHost(50) .connectTimeout(1000 * 10) .maxAutoConnectRetryTime(1000 * 60 * 30) .socketFactory(mongoSocketFactory) .socketKeepAlive(true) .socketTimeout(1000 * 60) .build(); return new MongoOptions(mongoClientOptions); }
/** * MongoオペレーションをDIコンテナに登録します。 * @return Mongoオペレーション */ @Bean public MongoOptions mongoOptions() { MongoOptions options = new MongoOptions(); options.setConnectionsPerHost(connectionsPerHost); options.setThreadsAllowedToBlockForConnectionMultiplier(threadAllowedToBlockForConnectionMultiplier); options.setConnectTimeout(connectTimeout); options.setMaxWaitTime(maxWaitTime); options.setAutoConnectRetry(autoConnectRetry); options.setSocketKeepAlive(socketKeepAlive); options.setSocketTimeout(socketTimeout); options.setFsync(fsync); return options; }
@Bean public MongoOptions mongoOptions() { MongoOptions result = new MongoOptions(); // adjust write concert for better performance result.setWriteConcern(WriteConcern.UNACKNOWLEDGED); return result; }
public MongoOptions getOptions() { return options; }
public void setOptions(MongoOptions options) { this.options = options; }
@SuppressWarnings("deprecation") public void afterPropertiesSet() throws Exception { options = new MongoOptions(); /** * 设置获取连接池句柄的最大等待时间s */ options.maxWaitTime = (int) getMaxWait(); /** * 写安全设置,如果为true,驱动每次update后会发出一个getLastError命令来保证成功, If true the driver * will use a WriteConcern of WriteConcern.SAFE for all operations. */ options.safe = isSafe(); /** * 系统在发生连接错误时是否重试 ,默认为true */ options.autoConnectRetry = isAutoConnectRetry(); /** * 自动重连时间间隔 */ if (maxAutoConnectRetryTime != 0) { options.maxAutoConnectRetryTime = getMaxAutoConnectRetryTime(); } /** * 设置最大线程阻塞数,乘数 */ if (getMaxBlockingThread() != 0) { options.threadsAllowedToBlockForConnectionMultiplier = getMaxBlockingThread(); } /** * 设置socket通讯超时时间 */ if (getSocketTimeout() != 0) { options.socketTimeout = getSocketTimeout(); System.setProperty("com.mongodb.updaterConnectTimeoutMS", String.valueOf(getSocketTimeout())); } /** * 设置socket连接超时时间 */ if (getConnectTimeout() != 0) { options.connectTimeout = getConnectTimeout(); System.setProperty("com.mongodb.updaterSocketTimeoutMS", String.valueOf(getConnectTimeout())); } /** * 设置连接池最大连接数 */ if (getMaxActive() != 0) { options.connectionsPerHost = getMaxActive(); } options.slaveOk = isSlaveOk(); options.socketKeepAlive = isSocketKeepAlive(); }
@Override @Bean public MongoOptions mongoOptions() { return getDefaultMongoOptions(); }
@Bean @Override public MongoOptions mongoOptions() { return getDefaultMongoOptions(); }
public MongoOptions getMongoOptions() { return mongoOptions; }
public void setMongoOptions(MongoOptions mongoOptions) { this.mongoOptions = mongoOptions; }
public abstract MongoOptions mongoOptions();