Search in sources :

Example 26 with MongoClient

use of com.mongodb.reactivestreams.client.MongoClient in project micronaut-mongodb by micronaut-projects.

the class MongoHealthIndicator method checkRegisteredMongoClient.

private Publisher<HealthResult> checkRegisteredMongoClient(BeanRegistration<MongoClient> registration) {
    MongoClient mongoClient = registration.getBean();
    String databaseName = "mongodb (" + registration.getIdentifier().getName() + ")";
    Flux<Map<String, String>> databasePings = Flux.from(pingMongo(mongoClient)).map(this::getVersionDetails).timeout(Duration.of(10, ChronoUnit.SECONDS)).retry(3);
    return databasePings.map(detail -> buildStatusUp(databaseName, detail)).onErrorResume(throwable -> Flux.just(buildStatusDown(throwable, databaseName)));
}
Also used : Document(org.bson.Document) java.util(java.util) BeanRegistration(io.micronaut.context.BeanRegistration) BeanContext(io.micronaut.context.BeanContext) Publisher(org.reactivestreams.Publisher) BasicDBObject(com.mongodb.BasicDBObject) HealthAggregator(io.micronaut.management.health.aggregator.HealthAggregator) Singleton(jakarta.inject.Singleton) HealthIndicator(io.micronaut.management.health.indicator.HealthIndicator) Collectors(java.util.stream.Collectors) MongoClient(com.mongodb.reactivestreams.client.MongoClient) HealthStatus(io.micronaut.health.HealthStatus) Flux(reactor.core.publisher.Flux) ChronoUnit(java.time.temporal.ChronoUnit) Duration(java.time.Duration) Requires(io.micronaut.context.annotation.Requires) HealthResult(io.micronaut.management.health.indicator.HealthResult) MongoClient(com.mongodb.reactivestreams.client.MongoClient)

Example 27 with MongoClient

use of com.mongodb.reactivestreams.client.MongoClient in project gravitee-access-management by gravitee-io.

the class MongoAuthenticationProviderConfiguration method 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().applyToClusterSettings(clusterBuilder -> clusterBuilder.applySettings(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;
}
Also used : MongoClient(com.mongodb.reactivestreams.client.MongoClient) ClusterSettings(com.mongodb.connection.ClusterSettings) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) MongoClientSettings(com.mongodb.MongoClientSettings) Bean(org.springframework.context.annotation.Bean)

Example 28 with MongoClient

use of com.mongodb.reactivestreams.client.MongoClient in project gravitee-access-management by gravitee-io.

the class EmbeddedClient method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    final IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).build();
    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger).processOutput(ProcessOutput.getDefaultInstanceSilent()).build();
    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
    MongodExecutable mongodExecutable = runtime.prepare(mongodConfig);
    mongod = mongodExecutable.start();
    // cluster configuration
    ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(mongodConfig.net().getServerAddress().getHostName(), mongodConfig.net().getPort()))).build();
    // codec configuration
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build()));
    MongoClientSettings settings = MongoClientSettings.builder().applyToClusterSettings(clusterBuilder -> clusterBuilder.applySettings(clusterSettings)).codecRegistry(pojoCodecRegistry).writeConcern(WriteConcern.ACKNOWLEDGED).build();
    mongoClient = MongoClients.create(settings);
    mongoDatabase = mongoClient.getDatabase(databaseName);
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) LoggerFactory(org.slf4j.LoggerFactory) MongoClients(com.mongodb.reactivestreams.client.MongoClients) MongodExecutable(de.flapdoodle.embed.mongo.MongodExecutable) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) InitializingBean(org.springframework.beans.factory.InitializingBean) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ProcessOutput(de.flapdoodle.embed.process.config.io.ProcessOutput) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Version(de.flapdoodle.embed.mongo.distribution.Version) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) CodecRegistries.fromRegistries(org.bson.codecs.configuration.CodecRegistries.fromRegistries) ServerAddress(com.mongodb.ServerAddress) RuntimeConfigBuilder(de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder) Logger(org.slf4j.Logger) PojoCodecProvider(org.bson.codecs.pojo.PojoCodecProvider) CodecRegistries.fromProviders(org.bson.codecs.configuration.CodecRegistries.fromProviders) Command(de.flapdoodle.embed.mongo.Command) MongodProcess(de.flapdoodle.embed.mongo.MongodProcess) MongodStarter(de.flapdoodle.embed.mongo.MongodStarter) DisposableBean(org.springframework.beans.factory.DisposableBean) IRuntimeConfig(de.flapdoodle.embed.process.config.IRuntimeConfig) MongoClientSettings(com.mongodb.MongoClientSettings) WriteConcern(com.mongodb.WriteConcern) Collections(java.util.Collections) MongodExecutable(de.flapdoodle.embed.mongo.MongodExecutable) ClusterSettings(com.mongodb.connection.ClusterSettings) ServerAddress(com.mongodb.ServerAddress) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) MongodStarter(de.flapdoodle.embed.mongo.MongodStarter) MongoClientSettings(com.mongodb.MongoClientSettings) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) IRuntimeConfig(de.flapdoodle.embed.process.config.IRuntimeConfig) RuntimeConfigBuilder(de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder)

Example 29 with MongoClient

use of com.mongodb.reactivestreams.client.MongoClient in project gravitee-access-management by gravitee-io.

the class MongoReporterConfiguration method mongoClient.

@Bean
public MongoClient mongoClient() {
    // Client settings
    com.mongodb.MongoClientSettings.Builder builder = com.mongodb.MongoClientSettings.builder();
    builder.writeConcern(WriteConcern.ACKNOWLEDGED);
    // codec configuration for pojo mapping
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build()));
    builder.codecRegistry(pojoCodecRegistry);
    if ((this.configuration.getUri() != null) && (!this.configuration.getUri().isEmpty())) {
        // The builder can be configured with default options, which may be overridden by options specified in
        // the URI string.
        com.mongodb.MongoClientSettings settings = builder.codecRegistry(pojoCodecRegistry).applyConnectionString(new ConnectionString(this.configuration.getUri())).build();
        return MongoClients.create(settings);
    } else {
        // Manual configuration
        // Servers host
        ServerAddress serverAddress = new ServerAddress(this.configuration.getHost(), this.configuration.getPort());
        ClusterSettings clusterSettings = ClusterSettings.builder().hosts(asList(serverAddress)).build();
        // Mongo credentials
        if (this.configuration.isEnableCredentials()) {
            MongoCredential credential = MongoCredential.createCredential(this.configuration.getUsernameCredentials(), this.configuration.getDatabaseCredentials(), this.configuration.getPasswordCredentials().toCharArray());
            builder.credential(credential);
        }
        com.mongodb.MongoClientSettings settings = builder.applyToClusterSettings(builder1 -> builder1.applySettings(clusterSettings)).build();
        return MongoClients.create(settings);
    }
}
Also used : ServerAddress(com.mongodb.ServerAddress) MongoCredential(com.mongodb.MongoCredential) PojoCodecProvider(org.bson.codecs.pojo.PojoCodecProvider) CodecRegistries.fromProviders(org.bson.codecs.configuration.CodecRegistries.fromProviders) ClusterSettings(com.mongodb.connection.ClusterSettings) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoClients(com.mongodb.reactivestreams.client.MongoClients) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) MongoClient(com.mongodb.reactivestreams.client.MongoClient) Configuration(org.springframework.context.annotation.Configuration) ConnectionString(com.mongodb.ConnectionString) Arrays.asList(java.util.Arrays.asList) Bean(org.springframework.context.annotation.Bean) WriteConcern(com.mongodb.WriteConcern) CodecRegistries.fromRegistries(org.bson.codecs.configuration.CodecRegistries.fromRegistries) ClusterSettings(com.mongodb.connection.ClusterSettings) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) ConnectionString(com.mongodb.ConnectionString) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Bean(org.springframework.context.annotation.Bean)

Aggregations

MongoClient (com.mongodb.reactivestreams.client.MongoClient)29 MongoClientSettings (com.mongodb.MongoClientSettings)10 Test (org.junit.Test)10 ServerAddress (com.mongodb.ServerAddress)8 InsertOneResult (com.mongodb.client.result.InsertOneResult)7 Document (org.bson.Document)7 OperationSubscriber (reactivestreams.helpers.SubscriberHelpers.OperationSubscriber)7 MongoClients (com.mongodb.reactivestreams.client.MongoClients)6 ConnectionString (com.mongodb.ConnectionString)5 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)5 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)5 ClusterSettings (com.mongodb.connection.ClusterSettings)4 SecureRandom (java.security.SecureRandom)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 CodecRegistries.fromProviders (org.bson.codecs.configuration.CodecRegistries.fromProviders)4 CodecRegistries.fromRegistries (org.bson.codecs.configuration.CodecRegistries.fromRegistries)4 PojoCodecProvider (org.bson.codecs.pojo.PojoCodecProvider)4 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)3 MongoCredential (com.mongodb.MongoCredential)3