All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.breuninger.boot.mongo.MongoStatusDetailIndicator Maven / Gradle / Ivy

There is a newer version: 2.0.5.RELEASE
Show newest version
package com.breuninger.boot.mongo;

import static java.util.Collections.singletonList;

import static com.breuninger.boot.status.domain.Status.ERROR;
import static com.breuninger.boot.status.domain.Status.OK;
import static com.breuninger.boot.status.domain.StatusDetail.statusDetail;

import java.util.List;

import org.bson.Document;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import com.breuninger.boot.status.domain.StatusDetail;
import com.breuninger.boot.status.indicator.StatusDetailIndicator;
import com.mongodb.MongoTimeoutException;
import com.mongodb.client.MongoDatabase;

@Component
@ConditionalOnProperty(prefix = "breuninger.mongo.status", name = "enabled", havingValue = "true", matchIfMissing = true)
public class MongoStatusDetailIndicator implements StatusDetailIndicator {

  private final MongoDatabase mongoDatabase;

  public MongoStatusDetailIndicator(final MongoDatabase mongoDatabase) {
    this.mongoDatabase = mongoDatabase;
  }

  @Override
  public List statusDetails() {
    final var databaseStatusName = "MongoDB Status";
    final var document = new Document().append("ping", 1);
    final Document answer;
    try {
      answer = mongoDatabase.runCommand(document);
    } catch (final MongoTimeoutException e) {
      return singletonList(
        statusDetail(databaseStatusName, ERROR, "Mongo database check ran into timeout (" + e.getMessage() + ")."));
    } catch (final Exception other) {
      return singletonList(
        statusDetail(databaseStatusName, ERROR, "Exception during database check (" + other.getMessage() + ")."));
    }

    if (answer != null && answer.get("ok") != null && (Double) answer.get("ok") == 1.0d) {
      return singletonList(statusDetail(databaseStatusName, OK, "Mongo database is reachable."));
    }

    return singletonList(statusDetail(databaseStatusName, ERROR, "Mongo database unreachable or ping command failed."));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy