@Override protected MongoClientSettings createInstance() throws Exception { return MongoClientSettings.builder() // .readPreference(readPreference) // .writeConcern(writeConcern) // .readConcern(readConcern) // .credentialList(credentialList) // .streamFactoryFactory(streamFactoryFactory) // .codecRegistry(codecRegistry) // .clusterSettings(clusterSettings) // .socketSettings(socketSettings) // .heartbeatSocketSettings(heartbeatSocketSettings) // .connectionPoolSettings(connectionPoolSettings) // .serverSettings(serverSettings) // .sslSettings(sslSettings) // .build(); } }
static MongoClientSettings.Builder settings(final ConnectionString cstr, final Config conf) { MongoClientSettings.Builder settings = MongoClientSettings.builder(); settings.clusterSettings(cluster(cstr, conf)); settings.connectionPoolSettings(pool(cstr, conf)); settings.heartbeatSocketSettings(socket("heartbeat", cstr, conf)); withStr("readConcern", conf, v -> settings.readConcern(readConcern(v))); withStr("readPreference", conf, v -> settings.readPreference(ReadPreference.valueOf(v))); settings.serverSettings(server(conf)); settings.socketSettings(socket("socket", cstr, conf)); settings.sslSettings(ssl(cstr, conf)); withStr("writeConcern", conf, v -> settings.writeConcern(writeConcern(v))); return settings; }
@Override protected MongoClientSettings createInstance() throws Exception { return MongoClientSettings.builder() // .readPreference(readPreference) // .writeConcern(writeConcern) // .readConcern(readConcern) // .credentialList(credentialList) // .streamFactoryFactory(streamFactoryFactory) // .codecRegistry(codecRegistry) // .clusterSettings(clusterSettings) // .socketSettings(socketSettings) // .heartbeatSocketSettings(heartbeatSocketSettings) // .connectionPoolSettings(connectionPoolSettings) // .serverSettings(serverSettings) // .sslSettings(sslSettings) // .build(); } }
/** * Initializes the persistence with a passed in parameters. Does NOT allow to specify credentials, is useful for * testing purposes. * * @param host the host name of the mongoDB * @param port the port of the mongoDB * @param dbName the database of the mongoDB * @param maxPoolSize the max pool size of the db. * @param maxPoolWaitQueueSize the max queue size of the pool. * @param maxPoolWaitTimeSecs the max wait time in the pool. * @return a new {@code MongoClientWrapper} object. * @see #newInstance(Config) for production purposes */ public static MongoClientWrapper newInstance(final String host, final int port, final String dbName, final int maxPoolSize, final int maxPoolWaitQueueSize, final long maxPoolWaitTimeSecs) { final MongoClientSettings.Builder builder = MongoClientSettings.builder() .readPreference(ReadPreference.secondaryPreferred()) .clusterSettings(ClusterSettings.builder() .hosts(Collections.singletonList(new ServerAddress(host, port))) .build()); final MongoClientSettings mongoClientSettings = buildClientSettings(builder, maxPoolSize, maxPoolWaitQueueSize, Duration.of(maxPoolWaitTimeSecs, ChronoUnit.SECONDS), false, null); return new MongoClientWrapper(dbName, mongoClientSettings); }
/** * Initializes the persistence with a passed in parameters. Does NOT allow to specify credentials, is useful for * testing purposes. * * @param host the host name of the mongoDB * @param port the port of the mongoDB * @param dbName the database of the mongoDB * @param maxPoolSize the max pool size of the db. * @param maxPoolWaitQueueSize the max queue size of the pool. * @param maxPoolWaitTimeSecs the max wait time in the pool. * @return a new {@code MongoClientWrapper} object. * @see #newInstance(Config) for production purposes */ public static MongoClientWrapper newInstance(final String host, final int port, final String dbName, final int maxPoolSize, final int maxPoolWaitQueueSize, final long maxPoolWaitTimeSecs) { final MongoClientSettings.Builder builder = MongoClientSettings.builder() .readPreference(ReadPreference.secondaryPreferred()) .clusterSettings(ClusterSettings.builder() .hosts(Collections.singletonList(new ServerAddress(host, port))) .build()); final MongoClientSettings mongoClientSettings = buildClientSettings(builder, maxPoolSize, maxPoolWaitQueueSize, Duration.of(maxPoolWaitTimeSecs, ChronoUnit.SECONDS), false, null); return new MongoClientWrapper(dbName, mongoClientSettings); }
@Bean public MongoClient mongoClient() { MongoClient mongoClient; if ((this.configuration.getUri() != null) && (!this.configuration.getUri().isEmpty())) { mongoClient = MongoClients.create(this.configuration.getUri()); } else { ServerAddress serverAddress = new ServerAddress(this.configuration.getHost(), this.configuration.getPort()); ClusterSettings clusterSettings = ClusterSettings.builder().hosts(asList(serverAddress)).build(); MongoClientSettings.Builder settings = MongoClientSettings.builder().clusterSettings(clusterSettings); if (this.configuration.isEnableCredentials()) { MongoCredential credential = MongoCredential.createCredential(this.configuration .getUsernameCredentials(), this.configuration .getDatabaseCredentials(), this.configuration .getPasswordCredentials().toCharArray()); settings.credential(credential); } mongoClient = MongoClients.create(settings.build()); } return mongoClient; }
@Bean public MongoClient mongoClient() { MongoClient mongoClient; if ((this.configuration.getUri() != null) && (!this.configuration.getUri().isEmpty())) { mongoClient = MongoClients.create(this.configuration.getUri()); } else { ServerAddress serverAddress = new ServerAddress(this.configuration.getHost(), this.configuration.getPort()); ClusterSettings clusterSettings = ClusterSettings.builder().hosts(asList(serverAddress)).build(); MongoClientSettings.Builder settings = MongoClientSettings.builder().clusterSettings(clusterSettings); if (this.configuration.isEnableCredentials()) { MongoCredential credential = MongoCredential.createCredential(this.configuration .getUsernameCredentials(), this.configuration .getDatabaseCredentials(), this.configuration .getPasswordCredentials().toCharArray()); settings.credential(credential); } mongoClient = MongoClients.create(settings.build()); } return mongoClient; }
private MongoClientSettings buildMongoClientSettings(MongoDbConfig.ClientConfig clientConfig, String clientName, Coffig coffig) { MongoClientSettings.Builder settingsBuilder = MongoClientSettings.builder(); AllSettings allSettings = coffig.get(AllSettings.class, String.format("mongoDb.clients.%s.settings", clientName)); // Apply hosts List<String> hosts = clientConfig.getHosts(); if (hosts.size() > 0) { allSettings.cluster.get().hosts(buildServerAddresses(clientName, hosts)); } // Apply credentials settingsBuilder.credentialList(buildMongoCredentials(clientName, clientConfig.getCredentials())); // Apply global settings Optional.ofNullable(allSettings.readPreference).ifPresent(settingsBuilder::readPreference); Optional.ofNullable(allSettings.writeConcern).ifPresent(settingsBuilder::writeConcern); Optional.ofNullable(allSettings.codecRegistry).map(Classes::instantiateDefault).ifPresent(settingsBuilder::codecRegistry); // Apply sub-settings settingsBuilder.clusterSettings(allSettings.cluster.get().build()); settingsBuilder.socketSettings(allSettings.socket.get().build()); settingsBuilder.heartbeatSocketSettings(allSettings.heartbeatSocket.get().build()); settingsBuilder.connectionPoolSettings(allSettings.connectionPool.get().build()); settingsBuilder.serverSettings(allSettings.server.get().build()); settingsBuilder.sslSettings(allSettings.ssl.get().build()); return settingsBuilder.build(); }
/** * Create an asynchronous MongoDB client * @return the client */ private com.mongodb.async.client.MongoClient createAsyncClient() { ClusterSettings clusterSettings = ClusterSettings.builder() .hosts(Arrays.asList(mongoConnector.serverAddress)) .build(); MongoClientSettings settings = MongoClientSettings.builder() .clusterSettings(clusterSettings).build(); return MongoClients.create(settings); }
static MongoClientSettings.Builder settings(final ConnectionString cstr, final Config conf) { MongoClientSettings.Builder settings = MongoClientSettings.builder(); settings.clusterSettings(cluster(cstr, conf)); settings.connectionPoolSettings(pool(cstr, conf)); settings.heartbeatSocketSettings(socket("heartbeat", cstr, conf)); withStr("readConcern", conf, v -> settings.readConcern(readConcern(v))); withStr("readPreference", conf, v -> settings.readPreference(ReadPreference.valueOf(v))); settings.serverSettings(server(conf)); settings.socketSettings(socket("socket", cstr, conf)); settings.sslSettings(ssl(cstr, conf)); withStr("writeConcern", conf, v -> settings.writeConcern(writeConcern(v))); return settings; }
MongoClientSettings.builder() .readPreference(ReadPreference.secondaryPreferred()) .clusterSettings(ClusterSettings.builder().applyConnectionString(connectionString).build());
MongoClientSettings.builder() .readPreference(ReadPreference.secondaryPreferred()) .clusterSettings(ClusterSettings.builder().applyConnectionString(connectionString).build());