
io.micronaut.configuration.mongo.reactive.health.MongoHealthIndicator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-mongo-reactive Show documentation
Show all versions of micronaut-mongo-reactive Show documentation
Integration between Micronaut and MongoDB
The newest version!
/*
* Copyright 2017-2018 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.configuration.mongo.reactive.health;
import static java.util.concurrent.TimeUnit.SECONDS;
import com.mongodb.BasicDBObject;
import com.mongodb.reactivestreams.client.MongoClient;
import io.micronaut.context.BeanContext;
import io.micronaut.context.BeanRegistration;
import io.micronaut.context.annotation.Requires;
import io.micronaut.health.HealthStatus;
import io.micronaut.management.health.aggregator.HealthAggregator;
import io.micronaut.management.health.indicator.HealthIndicator;
import io.micronaut.management.health.indicator.HealthResult;
import io.reactivex.Flowable;
import org.bson.Document;
import org.reactivestreams.Publisher;
import javax.inject.Singleton;
import java.util.*;
import java.util.stream.Collectors;
/**
* A {@link HealthIndicator} for MongoDB.
*
* @author graemerocher
* @since 1.0
*/
@Singleton
@Requires(beans = MongoClient.class)
public class MongoHealthIndicator implements HealthIndicator {
private static final String HEALTH_INDICATOR_NAME = "mongodb";
private final BeanContext beanContext;
private final HealthAggregator> healthAggregator;
private final MongoClient[] mongoClients;
/**
* @param beanContext beanContext
* @param healthAggregator healthAggregator
* @param mongoClients The mongo clients
*/
public MongoHealthIndicator(BeanContext beanContext, HealthAggregator> healthAggregator, MongoClient... mongoClients) {
this.beanContext = beanContext;
this.healthAggregator = healthAggregator;
this.mongoClients = mongoClients;
}
@Override
public Publisher getResult() {
List> registrations = getRegisteredConnections();
Flowable healthResults = Flowable.fromIterable(registrations)
.flatMap(this::checkRegisteredMongoClient)
.onErrorReturn(throwable -> buildStatusDown(throwable, HEALTH_INDICATOR_NAME));
return this.healthAggregator.aggregate(HEALTH_INDICATOR_NAME, healthResults);
}
private List> getRegisteredConnections() {
return Arrays.stream(mongoClients)
.map(beanContext::findBeanRegistration)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
}
private Publisher checkRegisteredMongoClient(BeanRegistration registration) {
MongoClient mongoClient = registration.getBean();
String databaseName = "mongodb (" + registration.getIdentifier().getName() + ")";
Flowable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy