private MongoClient createMongoClient(ServerConfiguration configuration) { List<String> serverUrls = configuration.getServerUrls(); if (serverUrls.isEmpty()) { throw new ConfigurationException("server host is not set"); } List<ServerAddress> serverAddresses = new LinkedList<>(); if (SshTunnelingConfiguration.isEmpty(configuration.getSshTunnelingConfiguration())) { for (String serverUrl : serverUrls) { ServerConfiguration.HostAndPort hostAndPort = ServerConfiguration.extractHostAndPort(serverUrl); serverAddresses.add(new ServerAddress(hostAndPort.host, hostAndPort.port)); } } else { serverAddresses.add(new ServerAddress(DEFAULT_TUNNEL_LOCAL_HOST, DEFAULT_TUNNEL_LOCAL_PORT)); } MongoClientOptions options = MongoClientOptions.builder() .sslEnabled(configuration.isSslConnection()) .readPreference(configuration.getReadPreference()) .codecRegistry(MongoClient.getDefaultCodecRegistry()) .build(); if (StringUtils.isEmpty(configuration.getUsername())) { return new MongoClient(serverAddresses, options); } else { MongoCredential credential = getMongoCredential(configuration); return new MongoClient(serverAddresses, Collections.singletonList(credential), options); } }
@Bean public MongoClientURI uri(@Value("${mongo.uri}") String uri, CodecRegistry registry) { return new MongoClientURI(uri, MongoClientOptions.builder().codecRegistry(registry)); }
optionsBuilder.writeConcern(connectionSpec.getWriteConcern()); optionsBuilder.codecRegistry(MongoCodecs.codecRegistry()); MongoClientOptions options = optionsBuilder.build(); if (servers.isEmpty()) {
MongoClient.getDefaultCodecRegistry() ); clientOptions.codecRegistry(codecRegistry); return clientOptions.build();
public MongoClientOptions toMongoClientOptions(final CodecRegistry codecRegistry) { return builder() .sslEnabled(sslEnabled) .codecRegistry(codecRegistry) .readPreference(ReadPreference.valueOf(readPreference)) .connectTimeout(connectTimeout) .serverSelectionTimeout(serverSelectionTimeout) .cursorFinalizerEnabled(true) .maxWaitTime(maxWaitTime) .maxConnectionLifeTime(connectionpool.getMaxLifeTime()) .threadsAllowedToBlockForConnectionMultiplier(connectionpool.getBlockedConnectionMultiplier()) .maxConnectionIdleTime(connectionpool.getMaxIdleTime()) .minConnectionsPerHost(connectionpool.getMinSize()) .connectionsPerHost(connectionpool.getMaxSize()) .build(); }
private Builder getMongoClientOptionsBuilder(final CodecRegistry codecRegistry) { return builder() .sslEnabled(sslEnabled) .codecRegistry(codecRegistry) .readPreference(ReadPreference.valueOf(readPreference)) .connectTimeout(connectTimeout) .socketTimeout(socketTimeout) .serverSelectionTimeout(serverSelectionTimeout) .cursorFinalizerEnabled(true) .maxWaitTime(maxWaitTime) .maxConnectionLifeTime(connectionpool.getMaxLifeTime()) .threadsAllowedToBlockForConnectionMultiplier(connectionpool.getBlockedConnectionMultiplier()) .maxConnectionIdleTime(connectionpool.getMaxIdleTime()) .minConnectionsPerHost(connectionpool.getMinSize()) .connectionsPerHost(connectionpool.getMaxSize()); }