如何使用reactivemongo检查MongoDB是否已启动并运行

时间:2016-02-11 18:27:14

标签: mongodb scala reactivemongo

我正在尝试编写一个帮助函数,它允许我的仪表板检查MongoDB是否正在运行。根据结果​​,它可以在可视化仪表板上警告管理员。我通读了反应性mongo docs here,到目前为止我有以下功能,但遗憾的是它没有用。

import reactivemongo.api.MongoDriver

def isMongoUp(host: String): Boolean = {

  var connected = false
  val driver = new MongoDriver()
  val connection = driver.connection(List(host))
  val db = connection("myDB")
  db.map {
    case Failure(e) => connected = false
    case Success(result) => connected = true
  }

}

更新1:

def isMongoUp(host: String): Boolean = {
  var connected = false
  val driver = new MongoDriver()
  val connection = driver.connection(List(host))
  val db = connection.database("myDB")
  db.map {
    case sth:DefaultDB => connected = true
    case _ => connected = false
  }
  connected

}

val result = isMongoUp("localhost")
result

1 个答案:

答案 0 :(得分:-1)

我这样做了:

def isMongoUp(host: String): Boolean = {

    val driver = new MongoDriver()
    val connection = driver.connection(List(host))
    val db = connection.database("myDB")
    val connectedFuture: Future[Boolean] = db.map { list =>
      list match {
        case sth: DefaultDB => true
        case _ => false
      }
    }.recover { case error: Throwable => false}
    val timeout = scala.concurrent.duration.Duration(5, "seconds")

    val result = Await.result(connectedFuture, timeout)
    result

  }