Scala console application does not stop when using reactive mongo driver
I am playing with Mongo base via Reactive Mongo driver
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")
}
}
My build.sbt
file:
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"
)
)
When I run: sbt compile run
I am getting this output:
$ 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
And the application doesn't stop ....: /
I need to press Ctrl+ Cto kill him
I read what MongoDriver()
creates ActorSystem
, so I tried to close the connection manually with conn.close()
, but I get this:
[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'.
And the app doesn't exit either
So what am I doing wrong? I cannot find the answer ...
And it seems to me that the official docs don't explain if I should care about graceful completion at all.
I don't have much experience with console applications, I use the play framework in my projects, but I want to create a sub-project that works with mongodb
I see a lot of templates (in the activator) like: Play + Reactive Mongo, Play + Akka + Mongo, but there is no Scala + Reactive Mongo explaining how to work properly: /
source to share
This looks like a known issue with Reactive Mongo, see the related thread on GitHub
A fix for this was introduced in this pull request # 241 from reid-spencer , merged on Feb 3rd, 2015
You can fix this by using a newer version. If the release hasn't been released since February, you can try checking the version that includes this hotfix and building the code yourself.
As far as I can tell, there is no mention of this fix here in the release notes for version 0.10.5
- 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
- Kernel: fix connection leak in some cases
- GenericCollection: don't ignore WriteConcern in save ()
- GenericCollection: don't ignore WriteConcern in bulk inserts
- GridFS: fix DELETEED deserialization field
- Indexes: fix parsing for ascending and descending
- Macros: alias aliases
- Macros: Allow Custom Annotations
The committer name is also not displayed:
Here is a list of the commits included in this version (since 0.9, the top commit is the most recent):
$ 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
Looking at the commit history for 0.10.5.0.akka23
(the one you link to build.sbt
) , it seems that the fix was not merged into it.
source to share
I had the same problem. The solution I found caused closures for both the object and the driver:
val driver = new MongoDriver
val connection = driver.connection(List("localhost"))
...
connection.close()
driver.close()
If you only close the connection, the akka system stays alive.
Tested with ReactiveMongo 0.12
source to share