使用反应式 mongo 驱动程序时,控制台 scala 应用程序不会停止

Console scala app doesn't stop when using reactive mongo driver

我正在通过 Reactive Mongo 驱动程序Mongo 数据库

import org.slf4j.LoggerFactory

import reactivemongo.api.MongoDriver
import reactivemongo.api.collections.default.BSONCollection
import reactivemongo.bson.BSONDocument

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global

object Main {

  val log = LoggerFactory.getLogger("Main")

  def main(args: Array[String]): Unit = {

    log.info("Start")

    val conn = new MongoDriver().connection(List("localhost"))
    val db = conn("test")

    log.info("Done")

  }
}

我的 build.sbt 文件:

lazy val root = (project in file(".")).
  settings(
    name := "simpleapp",
    version := "1.0.0",
    scalaVersion := "2.11.4",
    libraryDependencies ++= Seq(
      "org.reactivemongo" %% "reactivemongo" % "0.10.5.0.akka23",
      "ch.qos.logback" % "logback-classic" % "1.1.2"
    )
  )

当我运行: sbt compile run

我得到这个输出:

$ sbt compile run
[success] Total time: 0 s, completed Apr 25, 2015 5:36:51 PM
[info] Running Main 
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
17:36:52.328 [run-main-0] INFO  Main - Start
17:36:52.333 [run-main-0] INFO  Main - Done

并且应用程序不会停止.... :/

我必须按 Ctrl + C 才能杀死它

我读到 MongoDriver() 创建 ActorSystem 所以我尝试手动关闭与 conn.close() 的连接,但我得到了这个:

[info] Running Main 
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
17:42:23.252 [run-main-0] INFO  Main - Start
17:42:23.258 [run-main-0] INFO  Main - Done
17:42:23.403 [reactivemongo-akka.actor.default-dispatcher-2] ERROR reactivemongo.core.actors.MongoDBSystem - (State: Closing) UNHANDLED MESSAGE: ChannelConnected(-973180998)
[INFO] [04/25/2015 17:42:23.413] [reactivemongo-akka.actor.default-dispatcher-3] [akka://reactivemongo/deadLetters] Message [reactivemongo.core.actors.Closed$] from Actor[akka://reactivemongo/user/$b#-1700211063] to Actor[akka://reactivemongo/deadLetters] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [04/25/2015 17:42:23.414] [reactivemongo-akka.actor.default-dispatcher-3] [akka://reactivemongo/user/$a] Message [reactivemongo.core.actors.Close$] from Actor[akka://reactivemongo/user/$b#-1700211063] to Actor[akka://reactivemongo/user/$a#-1418324178] was not delivered. [2] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'. 

而且应用程序也不会退出

所以,我做错了什么?我找不到答案...

在我看来,官方文档根本没有解释我是否应该关心正常关机。

我对控制台应用程序没有太多经验,我在我的项目中使用 play 框架,但我想创建与 mongodb

一起使用的子项目

我看到很多模板(在激活器中),例如:Play + Reactive MongoPlay + Akka + Mongo 但没有 Scala + Reactive Mongo 可以解释如何正常工作:/

这看起来像是 Reactive Mongo、see the relevant thread on GitHub

的一个已知问题

在此 pull request #241 by reid-spencer 中引入了对此的修复,于 2015 年 2 月 3 日合并

您应该可以使用较新的版本来修复它。如果自 2 月以来未发布任何版本,您可以尝试查看包含此修复的版本并自行构建代码。

据我所知,release notes for version 0.10.5

中没有提及此错误修复
  1. Bugfixes:
    • BSON library: fix BSONDateTimeNumberLike typeclass
    • Cursor: fix exception propagation
    • Commands: fix ok deserialization for some cases
    • Commands: fix CollStatsResult
    • Commands: fix AddToSet in aggregation
    • Core: fix connection leak in some cases
    • GenericCollection: do not ignore WriteConcern in save()
    • GenericCollection: do not ignore WriteConcern in bulk inserts
    • GridFS: fix uploadDate deserialization field
    • Indexes: fix parsing for Ascending and Descending
    • Macros: fix type aliases
    • Macros: allow custom annotations

提交者的名字也没有出现:

Here is the list of the commits included in this release (since 0.9, the top commit is the most recent one):

$ git shortlog -s -n refs/tags/v0.10.0..0.10.5.x.akka23
39  Stephane Godbillon
 5  Andrey Neverov
 4  lucasrpb
 3  Faissal Boutaounte
 2  杨博 (Yang Bo)
 2  Nikolay Sokolov
 1  David Liman
 1  Maksim Gurtovenko
 1  Age Mooij
 1  Paulo "JCranky" Siqueira
 1  Daniel Armak
 1  Viktor Taranenko
 1  Vincent Debergue
 1  Andrea Lattuada
 1  pavel.glushchenko
 1  Jacek Laskowski

查看 commit history for 0.10.5.0.akka23 (the one you reference in build.sbt),修复似乎没有合并到其中。

我遇到了同样的问题。我找到的解决方案是在对象、驱动程序和连接上调用关闭:

val driver = new MongoDriver
val connection = driver.connection(List("localhost"))
...
connection.close()
driver.close()

如果您只关闭连接,那么 akka 系统仍然存在。

使用 ReactiveMongo 0.12 测试