Java 类com.mongodb.connection.ConnectionPoolSettings 实例源码

项目:vertx-mongo-client    文件:ConnectionPoolSettingsParserTest.java   
@Test
public void testConnectionPoolSettings() {
  int maxPoolSize = 42;
  int minPoolSize = maxPoolSize / 2; // min needs to be less then max
  long maxIdleTimeMS = Math.abs(randomLong());
  long maxLifeTimeMS = Math.abs(randomLong());
  // Do not use long because of rounding.
  int waitQueueMultiple = 5432;
  long waitQueueTimeoutMS = Math.abs(randomLong());
  long maintenanceInitialDelayMS = Math.abs(randomLong());
  long maintenanceFrequencyMS = Math.abs(randomLong());

  JsonObject config = new JsonObject();
  config.put("maxPoolSize", maxPoolSize);
  config.put("minPoolSize", minPoolSize);
  config.put("maxIdleTimeMS", maxIdleTimeMS);
  config.put("maxLifeTimeMS", maxLifeTimeMS);
  config.put("waitQueueMultiple", waitQueueMultiple);
  config.put("waitQueueTimeoutMS", waitQueueTimeoutMS);
  config.put("maintenanceInitialDelayMS", maintenanceInitialDelayMS);
  config.put("maintenanceFrequencyMS", maintenanceFrequencyMS);

  ConnectionPoolSettings settings = new ConnectionPoolSettingsParser(null, config).settings();
  assertEquals(maxPoolSize, settings.getMaxSize());
  assertEquals(minPoolSize, settings.getMinSize());
  assertEquals(maxIdleTimeMS, settings.getMaxConnectionIdleTime(MILLISECONDS));
  assertEquals(maxLifeTimeMS, settings.getMaxConnectionLifeTime(MILLISECONDS));
  assertEquals(waitQueueMultiple, 5432);
  assertEquals(waitQueueTimeoutMS, settings.getMaxWaitTime(MILLISECONDS));
  assertEquals(maintenanceInitialDelayMS, settings.getMaintenanceInitialDelay(MILLISECONDS));
  assertEquals(maintenanceFrequencyMS, settings.getMaintenanceFrequency(MILLISECONDS));
}
项目:jooby    文件:MongodbRxTest.java   
private Block pool(final String db) {
  return unit -> {
    ConnectionPoolSettings settings = unit.mock(ConnectionPoolSettings.class);
    unit.registerMock(ConnectionPoolSettings.class, settings);

    ConnectionPoolSettings.Builder builder = unit.mock(ConnectionPoolSettings.Builder.class);
    expect(builder.applyConnectionString(new ConnectionString(db))).andReturn(builder);
    expect(builder.build()).andReturn(settings);

    unit.mockStatic(ConnectionPoolSettings.class);
    expect(ConnectionPoolSettings.builder()).andReturn(builder);
  };
}
项目:dragoman    文件:MongoProviderImpl.java   
/**
 * Lazily instantiate the {@link MongoClient} instance.
 *
 * @return
 */
private MongoClient createMongoClient() {
  String host = applicationConfiguration.getMongoHost();
  int port = applicationConfiguration.getMongoPort();
  ConnectionString connectionString = new ConnectionString("mongodb://" + host + ":" + port);

  logger.info("Creating Mongo client for: {}:{}", host, port);

  MongoClientSettings mongoClientSettings =
      MongoClientSettings.builder()
          .applicationName("dragoman")
          .serverSettings(
              ServerSettings.builder()
                  .applyConnectionString(connectionString)
                  .addServerMonitorListener(new LoggingServerMonitorListener())
                  .addServerListener(new LoggingServerListener())
                  .build())
          .clusterSettings(
              ClusterSettings.builder()
                  .applyConnectionString(connectionString)
                  .serverSelectionTimeout(
                      applicationConfiguration.getMongoServerSelectionTimeout(), MILLISECONDS)
                  .addClusterListener(new LoggingClusterListener())
                  .build())
          .connectionPoolSettings(
              ConnectionPoolSettings.builder()
                  .applyConnectionString(connectionString)
                  .maxWaitTime(
                      applicationConfiguration.getConnectionPoolMaxWaitTime(), MILLISECONDS)
                  .minSize(applicationConfiguration.getConnectionPoolMinSize())
                  .maxSize(applicationConfiguration.getConnectionPoolMaxSize())
                  .addConnectionPoolListener(new LoggingConnectionPoolListener())
                  .build())
          .socketSettings(
              SocketSettings.builder()
                  .applyConnectionString(connectionString)
                  .connectTimeout(
                      applicationConfiguration.getMongoSocketConnectionTimeout(), MILLISECONDS)
                  .readTimeout(applicationConfiguration.getMongoReadTimeout(), MILLISECONDS)
                  .build())
          .build();

  return MongoClients.create(mongoClientSettings);
}
项目:vertx-mongo-client    文件:ConnectionPoolSettingsParser.java   
public ConnectionPoolSettingsParser(ConnectionString connectionString, JsonObject config) {
  ConnectionPoolSettings.Builder settings = ConnectionPoolSettings.builder();
  if (connectionString != null) {
    settings.applyConnectionString(connectionString);
  } else {
    Integer maxPoolSize = config.getInteger("maxPoolSize");
    if (maxPoolSize != null) {
      settings.maxSize(maxPoolSize);
    }
    Integer minPoolSize = config.getInteger("minPoolSize");
    if (minPoolSize != null) {
      settings.minSize(minPoolSize);
    }
    Long maxIdleTimeMS = config.getLong("maxIdleTimeMS");
    if (maxIdleTimeMS != null) {
      settings.maxConnectionIdleTime(maxIdleTimeMS, MILLISECONDS);
    }
    Long maxLifeTimeMS = config.getLong("maxLifeTimeMS");
    if (maxLifeTimeMS != null) {
      settings.maxConnectionLifeTime(maxLifeTimeMS, MILLISECONDS);
    }
    Integer waitQueueMultiple = config.getInteger("waitQueueMultiple");
    if (waitQueueMultiple != null) {
     Integer waitQueueSize = waitQueueMultiple * DEFAULT_MONGO_DRIVER_WAIT_Q_SIZE;
      settings.maxWaitQueueSize(waitQueueSize);
    }
    Long waitQueueTimeoutMS = config.getLong("waitQueueTimeoutMS");
    if (waitQueueTimeoutMS != null) {
      settings.maxWaitTime(waitQueueTimeoutMS, MILLISECONDS);
    }
    Long maintenanceInitialDelayMS = config.getLong("maintenanceInitialDelayMS");
    if (maintenanceInitialDelayMS != null) {
      settings.maintenanceInitialDelay(maintenanceInitialDelayMS, MILLISECONDS);
    }
    Long maintenanceFrequencyMS = config.getLong("maintenanceFrequencyMS");
    if (maintenanceFrequencyMS != null) {
      settings.maintenanceFrequency(maintenanceFrequencyMS, MILLISECONDS);
    }
  }

  this.settings = settings.build();
}
项目:vertx-mongo-client    文件:ConnectionPoolSettingsParser.java   
public ConnectionPoolSettings settings() {
  return settings;
}
项目:activecheck    文件:MongodbReporter.java   
private void connect() throws ActivecheckReporterException {
    if (mongoClient == null) {
        logger.debug("Cannot run query. MongoDB is not connected. Trying to (re)connect.");
        try {
            // configure credentials
            List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
            String username = properties
                    .getString("mongodb.username", null);
            String password = properties
                    .getString("mongodb.password", null);
            if (username != null && password != null) {
                credentialsList.add(MongoCredential.createPlainCredential(
                        username, "*", password.toCharArray()));
            }

            // configure server addresses
            List<ServerAddress> addressList = new ArrayList<ServerAddress>();
            String socketPath = properties.getString("socket", null);
            if (socketPath != null) {
                addressList.add(new ServerAddress(new AFUNIXSocketAddress(
                        new File(socketPath))));
            } else {
                String url = properties.getString("url",
                        ServerAddress.defaultHost());
                int port = ServerAddress.defaultPort();
                String[] urlParts = url.split(":");
                if (urlParts.length > 1) {
                    port = Integer.parseInt(urlParts[1]);
                }
                addressList.add(new ServerAddress(urlParts[0], port));
            }
            ServerSelector serverSelector = new ReadPreferenceServerSelector(
                    MONGO_READ_PREFERENCE);
            ClusterSettings clusterSettings = ClusterSettings.builder()
                    .hosts(addressList).serverSelector(serverSelector)
                    .build();

            // actually configure and (re)create mongoClient
            ConnectionPoolSettings connectionPoolSettings = ConnectionPoolSettings
                    .builder().maxSize(MONGO_POOL_SIZE).build();
            MongoClientSettings settings = MongoClientSettings.builder()
                    .readPreference(MONGO_READ_PREFERENCE)
                    .credentialList(credentialsList)
                    .clusterSettings(clusterSettings)
                    .connectionPoolSettings(connectionPoolSettings).build();
            mongoClient = MongoClients.create(settings);
        } catch (Exception e) {
            mongoClient = null;
            String errorMessage = "MongodbReporter Configuration Error for service '"
                    + getOverallServiceName() + "': " + e.getMessage();

            logger.error(errorMessage);
            logger.trace(e.getMessage(), e);

            // set report and status
            setOverallServiceReport(NagiosServiceStatus.CRITICAL,
                    errorMessage);
            throw new ActivecheckReporterException(e);
        }
    }
}