/** * <p>Creates an instance based on a list of replica set members or mongos servers. For a replica set it will discover all members. * For a list with a single seed, the driver will still discover all members of the replica set. For a direct * connection to a replica set member, with no discovery, use the {@link #MongoClient(ServerAddress, List)} * constructor instead.</p> * * <p>When there is more than one server to choose from based on the type of request (read or write) and the read preference (if it's a * read request), the driver will randomly select a server to send a request. This applies to both replica sets and sharded clusters. * The servers to randomly select from are further limited by the local threshold. See * {@link MongoClientOptions#getLocalThreshold()}</p> * * @param seeds Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod * servers in the same replica set or a list of mongos servers in the same sharded cluster. * @param credentialsList the list of credentials used to authenticate all connections * @see MongoClientOptions#getLocalThreshold() * @since 2.11.0 * @deprecated Prefer {@link #MongoClient(List, MongoCredential, MongoClientOptions)} */ @Deprecated public MongoClient(final List<ServerAddress> seeds, final List<MongoCredential> credentialsList) { this(seeds, credentialsList, new MongoClientOptions.Builder().build()); }
MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder(); if (configuration.getConnectionsPerHost() != null) { optionsBuilder.connectionsPerHost(configuration.getConnectionsPerHost());
@Override public void evaluate() throws Throwable { MongoDbAvailable mongoAvailable = method.getAnnotation(MongoDbAvailable.class); if (mongoAvailable != null) { try { MongoClientOptions options = new MongoClientOptions.Builder() .serverSelectionTimeout(100) .build(); MongoClient mongo = new MongoClient(ServerAddress.defaultHost(), options); mongo.listDatabaseNames() .first(); } catch (Exception e) { logger.warn("MongoDb is not available. Skipping the test: " + target.getClass().getSimpleName() + "." + method.getName() + "()"); return; } } base.evaluate(); } };
@Setup public void createMongoClient() throws Exception { MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); builder.socketKeepAlive(spec.keepAlive()); builder.maxConnectionIdleTime(spec.maxConnectionIdleTime()); client = new MongoClient(new MongoClientURI(spec.uri(), builder)); }
private MongoClientOptions getConfOptions() { return new MongoClientOptions.Builder().socketKeepAlive(true) .connectTimeout(Integer.valueOf(PropertyUtils.getPropertyString(bundleResourceString, "mongo.db.connection.timeout"))) .socketTimeout(Integer.valueOf(PropertyUtils.getPropertyString(bundleResourceString, "mongo.db.socket.timeout"))) .readPreference(ReadPreference.primary()) .autoConnectRetry(false) .connectionsPerHost(Integer.valueOf(PropertyUtils.getPropertyString(bundleResourceString, "mongo.db.connections.perhost"))) .maxWaitTime(Integer.valueOf(PropertyUtils.getPropertyString(bundleResourceString, "mongo.db.max.wait.time"))) .threadsAllowedToBlockForConnectionMultiplier(Integer.valueOf(PropertyUtils.getPropertyString(bundleResourceString, "mongo.db.threads.allowed.block.connection.multiplier"))) .writeConcern(WriteConcern.NORMAL).build(); }
private static MongoClientOptions getOption(){ MongoClientOptions.Builder build = new MongoClientOptions.Builder(); build.connectionsPerHost(CONNECTIONS_PER_HOST); build.threadsAllowedToBlockForConnectionMultiplier(THREADS_FOR_CONNECTION_MULTIPLIER); build.connectTimeout(CONNECT_TIMEOUT); build.maxWaitTime(MAX_WAIT_TIME); build.socketTimeout(SOCKET_TIMEOUT); build.writeConcern(WriteConcern.UNACKNOWLEDGED); return build.build(); } }
/** * Creates a Mongo instance based on a (single) mongodb node * * @param addr the database address * @see com.mongodb.ServerAddress */ public MongoClient(final ServerAddress addr) { this(addr, new MongoClientOptions.Builder().build()); }
@Override public MongoClientOptions getObject() throws Exception { return new MongoClientOptions.Builder() .connectionsPerHost(connectionsPerHost) .threadsAllowedToBlockForConnectionMultiplier( threadsAllowedToBlockForConnectionMultiplier) .writeConcern(writeConcern). build(); }
/** * Creates a builder instance. * * @return a builder * @since 3.0.0 */ public static Builder builder() { return new Builder(); }
MongoClientOptions mco = new MongoClientOptions.Builder() .connectionsPerHost(100) .threadsAllowedToBlockForConnectionMultiplier(10) .build(); MongoClient client = new MongoClient(addresses, mco); //addresses is a pre-populated List of ServerAddress objects
/** * Constructs a builder with default options set. These can be overridden later * * @return builder with default options set */ public static MongoClientOptions.Builder getDefaultBuilder() { return new MongoClientOptions.Builder() .description("MongoConnection for Oak DocumentMK") .maxWaitTime(DEFAULT_MAX_WAIT_TIME) .heartbeatFrequency(DEFAULT_HEARTBEAT_FREQUENCY_MS) .threadsAllowedToBlockForConnectionMultiplier(100); }
protected static MongoClient getMongoClient() throws UnknownHostException { ServerAddress serverAddress = new ServerAddress( properties.getProperty( "host" ), 27017 ); MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder(); optionsBuilder.connectTimeout( 1000 ); optionsBuilder.writeConcern( WriteConcern.ACKNOWLEDGED ); optionsBuilder.readPreference( ReadPreference.primary() ); MongoClientOptions clientOptions = optionsBuilder.build(); MongoClient mongo = new MongoClient( serverAddress, clientOptions ); return mongo; } }
/** * Creates a MongoURI from the given string. * * @param uri the URI */ public MongoClientURI(final String uri) { this(uri, new MongoClientOptions.Builder()); }
/** * Creates a builder instance. * * @param options existing MongoClientOptions to default the builder settings on. * @return a builder * @since 3.0.0 */ public static Builder builder(final MongoClientOptions options) { return new Builder(options); }
/** * Constructs a builder with default options set. These can be overridden later * * @return builder with default options set */ public static MongoClientOptions.Builder getDefaultBuilder() { return new MongoClientOptions.Builder() .description("MongoConnection for Oak DocumentMK") .maxWaitTime(DEFAULT_MAX_WAIT_TIME) .heartbeatFrequency(DEFAULT_HEARTBEAT_FREQUENCY_MS) .threadsAllowedToBlockForConnectionMultiplier(100); }
/** * <p>Creates an instance based on a list of replica set members or mongos servers. For a replica set it will discover all members. * For a list with a single seed, the driver will still discover all members of the replica set. For a direct * connection to a replica set member, with no discovery, use the {@link #MongoClient(ServerAddress)} constructor instead.</p> * * <p>When there is more than one server to choose from based on the type of request (read or write) and the read preference (if it's a * read request), the driver will randomly select a server to send a request. This applies to both replica sets and sharded clusters. * The servers to randomly select from are further limited by the local threshold. See * {@link MongoClientOptions#getLocalThreshold()}</p> * * @param seeds Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod * servers in the same replica set or a list of mongos servers in the same sharded cluster. * @see MongoClientOptions#getLocalThreshold() */ public MongoClient(final List<ServerAddress> seeds) { this(seeds, new MongoClientOptions.Builder().build()); }
/** * Creates a builder instance. * * @param options existing MongoClientOptions to default the builder settings on. * @return a builder * @since 3.0.0 */ public static Builder builder(final MongoClientOptions options) { return new Builder(options); }
/** * Creates a builder instance. * * @return a builder * @since 3.0.0 */ public static Builder builder() { return new Builder(); }
/** * Creates a MongoURI from the given string. * * @param uri the URI */ public MongoClientURI(final String uri) { this(uri, new MongoClientOptions.Builder()); }
/** * Creates a Mongo instance based on a (single) mongodb node * * @param addr the database address * @see com.mongodb.ServerAddress */ public MongoClient(final ServerAddress addr) { this(addr, new MongoClientOptions.Builder().build()); }