List of usage examples for com.mongodb MongoClientOptions.Builder threadsAllowedToBlockForConnectionMultiplier
int threadsAllowedToBlockForConnectionMultiplier
To view the source code for com.mongodb MongoClientOptions.Builder threadsAllowedToBlockForConnectionMultiplier.
Click Source Link
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); }//from w w w. ja va 2 s. 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.s1.mongodb.MongoDBConnectionHelper.java
License:Apache License
/** * * @param instance//from w ww . j a v a 2 s. c o m */ private static synchronized void initialize(String instance) { if (!connections.containsKey(instance)) { Map<String, Object> mopt = Options.getStorage().getMap(OPTIONS); Map<String, Object> m = Objects.get(mopt, "connections." + instance); if (Objects.isNullOrEmpty(m)) { m = Objects.get(mopt, "connections." + DEFAULT_INSTANCE); } MongoClientOptions.Builder b = MongoClientOptions.builder(); MongoClientOptions def_opt = MongoClientOptions.builder().build(); b.connectionsPerHost(Objects.get(m, "connectionsPerHost", def_opt.getConnectionsPerHost())); b.autoConnectRetry(Objects.get(m, "autoConnectRetry", def_opt.isAutoConnectRetry())); b.connectTimeout(Objects.get(m, "connectTimeout", def_opt.getConnectTimeout())); b.socketKeepAlive(Objects.get(m, "socketKeepAlive", def_opt.isSocketKeepAlive())); b.socketTimeout(Objects.get(m, "socketTimeout", def_opt.getSocketTimeout())); b.maxAutoConnectRetryTime( Objects.get(m, "maxAutoConnectRetryTime", def_opt.getMaxAutoConnectRetryTime())); b.maxWaitTime(Objects.get(m, "maxWaitTime", def_opt.getMaxWaitTime())); b.threadsAllowedToBlockForConnectionMultiplier( Objects.get(m, "threadsAllowedToBlockForConnectionMultiplier", def_opt.getThreadsAllowedToBlockForConnectionMultiplier())); b.writeConcern(WriteConcern.FSYNC_SAFE); MongoClientOptions opt = b.build(); MongoClient cl = null; try { cl = new MongoClient( new ServerAddress(Objects.get(m, "host", "localhost"), Objects.get(m, "port", 27017)), opt); } catch (UnknownHostException e) { throw S1SystemError.wrap(e); } String dbName = Objects.get(m, "name"); if (Objects.isNullOrEmpty(dbName)) throw new S1SystemError("Cannot initialize MongoDB connection, because name is not set"); DB db = cl.getDB(dbName); String user = Objects.get(m, "user"); String password = Objects.get(m, "password"); if (!Objects.isNullOrEmpty(user)) { if (!db.authenticate(user, password.toCharArray())) { throw new S1SystemError( "Cannot authenticate MongoDB connection " + dbName + " with user " + user); } } LOG.info("MongoDB connected " + cl.getAddress().getHost() + ":" + cl.getAddress().getPort()); connections.put(instance, db); } }
From source file:org.venice.piazza.servicecontroller.data.mongodb.accessors.MongoAccessor.java
License:Apache License
@PostConstruct private void initialize() { try {/*w w w . ja va2 s. co m*/ MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); // Enable SSL if the `mongossl` Profile is enabled if (Arrays.stream(environment.getActiveProfiles()).anyMatch(env -> env.equalsIgnoreCase("mongossl"))) { builder.sslEnabled(true); builder.sslInvalidHostNameAllowed(true); } // If a username and password are provided, then associate these credentials with the connection if ((!StringUtils.isEmpty(DATABASE_USERNAME)) && (!StringUtils.isEmpty(DATABASE_CREDENTIAL))) { mongoClient = new MongoClient(new ServerAddress(DATABASE_HOST, DATABASE_PORT), Arrays.asList(MongoCredential.createCredential(DATABASE_USERNAME, DATABASE_NAME, DATABASE_CREDENTIAL.toCharArray())), builder.threadsAllowedToBlockForConnectionMultiplier(mongoThreadMultiplier).build()); } else { mongoClient = new MongoClient(new ServerAddress(DATABASE_HOST, DATABASE_PORT), builder.threadsAllowedToBlockForConnectionMultiplier(mongoThreadMultiplier).build()); } } catch (Exception exception) { LOGGER.error(String.format("Error connecting to MongoDB Instance. %s", exception.getMessage()), exception); } }
From source file:org.wso2.carbon.dataservices.core.description.config.MongoConfig.java
License:Open Source License
private MongoClientOptions extractMongoOptions(Map<String, String> properties) { MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); String connectionsPerHost = properties.get(DBConstants.MongoDB.CONNECTIONS_PER_HOST); if (!DBUtils.isEmptyString(connectionsPerHost)) { builder.connectionsPerHost(Integer.parseInt(connectionsPerHost)); }// w ww . j a va 2s .co m String maxWaitTime = properties.get(DBConstants.MongoDB.MAX_WAIT_TIME); if (!DBUtils.isEmptyString(maxWaitTime)) { builder.maxWaitTime(Integer.parseInt(maxWaitTime)); } String connectTimeout = properties.get(DBConstants.MongoDB.CONNECT_TIMEOUT); if (!DBUtils.isEmptyString(connectTimeout)) { builder.connectTimeout(Integer.parseInt(connectTimeout)); } String socketTimeout = properties.get(DBConstants.MongoDB.SOCKET_TIMEOUT); if (!DBUtils.isEmptyString(socketTimeout)) { builder.socketTimeout(Integer.parseInt(socketTimeout)); } String threadsAllowedToBlockForConnectionMultiplier = properties .get(DBConstants.MongoDB.THREADS_ALLOWED_TO_BLOCK_CONN_MULTIPLIER); if (!DBUtils.isEmptyString(threadsAllowedToBlockForConnectionMultiplier)) { builder.threadsAllowedToBlockForConnectionMultiplier( Integer.parseInt(threadsAllowedToBlockForConnectionMultiplier)); } return builder.build(); }
From source file:org.wso2.extension.siddhi.store.mongodb.util.MongoTableUtils.java
License:Open Source License
/** * Utility method which can be used to create MongoClientOptionsBuilder from values defined in the * deployment yaml file.//from w w w . j a va2s . c om * * @param storeAnnotation the source annotation which contains the needed parameters. * @param configReader {@link ConfigReader} Configuration Reader * @return MongoClientOptions.Builder */ public static MongoClientOptions.Builder extractMongoClientOptionsBuilder(Annotation storeAnnotation, ConfigReader configReader) { MongoClientOptions.Builder mongoClientOptionsBuilder = MongoClientOptions.builder(); try { mongoClientOptionsBuilder.connectionsPerHost( Integer.parseInt(configReader.readConfig(MongoTableConstants.CONNECTIONS_PER_HOST, "100"))); mongoClientOptionsBuilder.connectTimeout( Integer.parseInt(configReader.readConfig(MongoTableConstants.CONNECT_TIMEOUT, "10000"))); mongoClientOptionsBuilder.heartbeatConnectTimeout(Integer .parseInt(configReader.readConfig(MongoTableConstants.HEARTBEAT_CONNECT_TIMEOUT, "20000"))); mongoClientOptionsBuilder.heartbeatSocketTimeout(Integer .parseInt(configReader.readConfig(MongoTableConstants.HEARTBEAT_SOCKET_TIMEOUT, "20000"))); mongoClientOptionsBuilder.heartbeatFrequency( Integer.parseInt(configReader.readConfig(MongoTableConstants.HEARTBEAT_FREQUENCY, "10000"))); mongoClientOptionsBuilder.localThreshold( Integer.parseInt(configReader.readConfig(MongoTableConstants.LOCAL_THRESHOLD, "15"))); mongoClientOptionsBuilder.maxWaitTime( Integer.parseInt(configReader.readConfig(MongoTableConstants.MAX_WAIT_TIME, "120000"))); mongoClientOptionsBuilder.minConnectionsPerHost( Integer.parseInt(configReader.readConfig(MongoTableConstants.MIN_CONNECTIONS_PER_HOST, "0"))); mongoClientOptionsBuilder.minHeartbeatFrequency( Integer.parseInt(configReader.readConfig(MongoTableConstants.MIN_HEARTBEAT_FREQUENCY, "500"))); mongoClientOptionsBuilder.serverSelectionTimeout(Integer .parseInt(configReader.readConfig(MongoTableConstants.SERVER_SELECTION_TIMEOUT, "30000"))); mongoClientOptionsBuilder.socketTimeout( Integer.parseInt(configReader.readConfig(MongoTableConstants.SOCKET_TIMEOUT, "0"))); mongoClientOptionsBuilder.threadsAllowedToBlockForConnectionMultiplier( Integer.parseInt(configReader.readConfig(MongoTableConstants.THREADS_ALLOWED_TO_BLOCK, "5"))); mongoClientOptionsBuilder.socketKeepAlive( Boolean.parseBoolean(configReader.readConfig(MongoTableConstants.SOCKET_KEEP_ALIVE, "false"))); mongoClientOptionsBuilder.sslEnabled( Boolean.parseBoolean(configReader.readConfig(MongoTableConstants.SSL_ENABLED, "false"))); mongoClientOptionsBuilder.cursorFinalizerEnabled(Boolean .parseBoolean(configReader.readConfig(MongoTableConstants.CURSOR_FINALIZER_ENABLED, "true"))); mongoClientOptionsBuilder.readPreference(ReadPreference .valueOf(configReader.readConfig(MongoTableConstants.READ_PREFERENCE, "primary"))); mongoClientOptionsBuilder.writeConcern(WriteConcern .valueOf(configReader.readConfig(MongoTableConstants.WRITE_CONCERN, "acknowledged"))); String readConcern = configReader.readConfig(MongoTableConstants.READ_CONCERN, "DEFAULT"); if (!readConcern.matches("DEFAULT")) { mongoClientOptionsBuilder.readConcern(new ReadConcern(ReadConcernLevel.fromString(readConcern))); } int maxConnectionIdleTime = Integer .parseInt(configReader.readConfig(MongoTableConstants.MAX_CONNECTION_IDLE_TIME, "0")); if (maxConnectionIdleTime != 0) { mongoClientOptionsBuilder.maxConnectionIdleTime(maxConnectionIdleTime); } int maxConnectionLifeTime = Integer .parseInt(configReader.readConfig(MongoTableConstants.MAX_CONNECTION_LIFE_TIME, "0")); if (maxConnectionIdleTime != 0) { mongoClientOptionsBuilder.maxConnectionLifeTime(maxConnectionLifeTime); } String requiredReplicaSetName = configReader.readConfig(MongoTableConstants.REQUIRED_REPLICA_SET_NAME, ""); if (!requiredReplicaSetName.equals("")) { mongoClientOptionsBuilder.requiredReplicaSetName(requiredReplicaSetName); } String applicationName = configReader.readConfig(MongoTableConstants.APPLICATION_NAME, ""); if (!applicationName.equals("")) { mongoClientOptionsBuilder.applicationName(applicationName); } String secureConnectionEnabled = storeAnnotation .getElement(MongoTableConstants.ANNOTATION_ELEMENT_SECURE_CONNECTION); secureConnectionEnabled = secureConnectionEnabled == null ? "false" : secureConnectionEnabled; if (secureConnectionEnabled.equalsIgnoreCase("true")) { mongoClientOptionsBuilder.sslEnabled(true); String trustStore = storeAnnotation.getElement(MongoTableConstants.ANNOTATION_ELEMENT_TRUSTSTORE); trustStore = trustStore == null ? configReader.readConfig("trustStore", DEFAULT_TRUST_STORE_FILE) : trustStore; trustStore = resolveCarbonHome(trustStore); String trustStorePassword = storeAnnotation .getElement(MongoTableConstants.ANNOTATION_ELEMENT_TRUSTSTOREPASS); trustStorePassword = trustStorePassword == null ? configReader.readConfig("trustStorePassword", DEFAULT_TRUST_STORE_PASSWORD) : trustStorePassword; String keyStore = storeAnnotation.getElement(MongoTableConstants.ANNOTATION_ELEMENT_KEYSTORE); keyStore = keyStore == null ? configReader.readConfig("keyStore", DEFAULT_KEY_STORE_FILE) : keyStore; keyStore = resolveCarbonHome(keyStore); String keyStorePassword = storeAnnotation .getElement(MongoTableConstants.ANNOTATION_ELEMENT_STOREPASS); keyStorePassword = keyStorePassword == null ? configReader.readConfig("keyStorePassword", DEFAULT_KEY_STORE_PASSWORD) : keyStorePassword; mongoClientOptionsBuilder.socketFactory(MongoTableUtils.extractSocketFactory(trustStore, trustStorePassword, keyStore, keyStorePassword)); } return mongoClientOptionsBuilder; } catch (IllegalArgumentException e) { throw new MongoTableException("Values Read from config readers have illegal values : ", e); } }
From source file:streamflow.datastore.mongodb.config.MongoDatastoreModule.java
License:Apache License
@Provides public Mongo providesMongoClient(DatastoreConfig datastoreConfig) { MongoClient mongoClient = null;/*w ww . ja v a 2 s .c o m*/ MongoClientOptions.Builder clientOptions = MongoClientOptions.builder(); String serverAddressHost = datastoreConfig.getProperty("host", String.class); if (serverAddressHost == null) { serverAddressHost = "localhost"; } Integer serverAddressPort = datastoreConfig.getProperty("port", Integer.class); if (serverAddressPort == null) { serverAddressPort = 27017; } Integer acceptableLatencyDifference = datastoreConfig.getProperty("acceptableLatencyDifference", Integer.class); if (acceptableLatencyDifference != null) { clientOptions.acceptableLatencyDifference(acceptableLatencyDifference); } Integer connectTimeout = datastoreConfig.getProperty("connectTimeout", Integer.class); if (connectTimeout != null) { clientOptions.connectTimeout(connectTimeout); } Integer connectionsPerHost = datastoreConfig.getProperty("connectionsPerHost", Integer.class); if (connectionsPerHost != null) { clientOptions.connectionsPerHost(connectionsPerHost); } Boolean cursorFinalizerEnabled = datastoreConfig.getProperty("acceptableLatencyDifference", Boolean.class); if (cursorFinalizerEnabled != null) { clientOptions.cursorFinalizerEnabled(cursorFinalizerEnabled); } Integer heartbeatConnectRetryFrequency = datastoreConfig.getProperty("heartbeatConnectRetryFrequency", Integer.class); if (heartbeatConnectRetryFrequency != null) { clientOptions.heartbeatConnectRetryFrequency(heartbeatConnectRetryFrequency); } Integer heartbeatConnectTimeout = datastoreConfig.getProperty("heartbeatConnectTimeout", Integer.class); if (heartbeatConnectTimeout != null) { clientOptions.heartbeatConnectTimeout(heartbeatConnectTimeout); } Integer heartbeatFrequency = datastoreConfig.getProperty("heartbeatFrequency", Integer.class); if (heartbeatFrequency != null) { clientOptions.heartbeatFrequency(heartbeatFrequency); } Integer heartbeatSocketTimeout = datastoreConfig.getProperty("heartbeatSocketTimeout", Integer.class); if (heartbeatSocketTimeout != null) { clientOptions.heartbeatSocketTimeout(heartbeatSocketTimeout); } Integer heartbeatThreadCount = datastoreConfig.getProperty("heartbeatThreadCount", Integer.class); if (heartbeatThreadCount != null) { clientOptions.heartbeatThreadCount(heartbeatThreadCount); } Integer maxConnectionIdleTime = datastoreConfig.getProperty("maxConnectionIdleTime", Integer.class); if (maxConnectionIdleTime != null) { clientOptions.maxConnectionIdleTime(maxConnectionIdleTime); } Integer maxConnectionLifeTime = datastoreConfig.getProperty("maxConnectionLifeTime", Integer.class); if (maxConnectionLifeTime != null) { clientOptions.maxConnectionLifeTime(maxConnectionLifeTime); } Integer maxWaitTime = datastoreConfig.getProperty("maxWaitTime", Integer.class); if (maxWaitTime != null) { clientOptions.maxWaitTime(maxWaitTime); } Integer minConnectionsPerHost = datastoreConfig.getProperty("minConnectionsPerHost", Integer.class); if (minConnectionsPerHost != null) { clientOptions.minConnectionsPerHost(minConnectionsPerHost); } Boolean socketKeepAlive = datastoreConfig.getProperty("socketKeepAlive", Boolean.class); if (socketKeepAlive != null) { clientOptions.socketKeepAlive(socketKeepAlive); } Integer socketTimeout = datastoreConfig.getProperty("socketTimeout", Integer.class); if (socketTimeout != null) { clientOptions.socketTimeout(socketTimeout); } Integer threadsAllowedToBlockForConnectionMultiplier = datastoreConfig .getProperty("threadsAllowedToBlockForConnectionMultiplier", Integer.class); if (threadsAllowedToBlockForConnectionMultiplier != null) { clientOptions .threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier); } try { ServerAddress serverAddress = new ServerAddress(serverAddressHost, serverAddressPort); // Initialize the Mongo connection with the specified address and options mongoClient = new MongoClient(serverAddress, clientOptions.build()); } catch (UnknownHostException ex) { LOG.error("Exception occurred while building Mongo client connection", ex); } return mongoClient; }