private static ClusterSettings getClusterSettings(final List<ServerAddress> seedList, final MongoClientOptions options, final ClusterConnectionMode clusterConnectionMode) { ClusterSettings.Builder builder = ClusterSettings.builder() .hosts(new ArrayList<ServerAddress>(seedList)) .mode(clusterConnectionMode) .requiredReplicaSetName(options.getRequiredReplicaSetName()) .serverSelectionTimeout(options.getServerSelectionTimeout(), MILLISECONDS) .localThreshold(options.getLocalThreshold(), MILLISECONDS) .serverSelector(options.getServerSelector()) .description(options.getDescription()) .maxWaitQueueSize(options.getConnectionPoolSettings().getMaxWaitQueueSize()); for (ClusterListener clusterListener: options.getClusterListeners()) { builder.addClusterListener(clusterListener); } return builder.build(); }
EmbeddedCluster(final MongoClientSettings mongoClientSettings) { this.server = new EmbeddedServer(mongoClientSettings); this.clusterSettings = ClusterSettings.builder().hosts(singletonList(new ServerAddress())).build(); this.clusterDescription = new ClusterDescription(ClusterConnectionMode.SINGLE, ClusterType.STANDALONE, singletonList(server.getDescription())); }
clusterBuilder.hosts(seeds);
/** * 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); }
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(); }
/** * 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 static ClusterSettings getClusterSettings(final List<ServerAddress> seedList, final MongoClientOptions options, final ClusterConnectionMode clusterConnectionMode) { ClusterSettings.Builder builder = ClusterSettings.builder() .hosts(new ArrayList<ServerAddress>(seedList)) .mode(clusterConnectionMode) .requiredReplicaSetName(options.getRequiredReplicaSetName()) .serverSelectionTimeout(options.getServerSelectionTimeout(), MILLISECONDS) .localThreshold(options.getLocalThreshold(), MILLISECONDS) .serverSelector(options.getServerSelector()) .description(options.getDescription()) .maxWaitQueueSize(options.getConnectionPoolSettings().getMaxWaitQueueSize()); for (ClusterListener clusterListener: options.getClusterListeners()) { builder.addClusterListener(clusterListener); } return builder.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); }