Search code examples
mongodbscalamongo-scala-driver

Scala script wait for mongo to complete task


I'm writing a simple scala-based script which supposed to insert some data into Mongo collection. The problem is, that script exits before mongo finishes it's task. What is the idiomatic/best approach to deal with the problem, considering following script:

#!/usr/bin/env scalas
/***
scalaVersion := "2.12.2"

libraryDependencies ++= {
  Seq(
    "org.mongodb.scala" %% "mongo-scala-driver" % "2.1.0"
  )
}
*/
import org.mongodb.scala._

val mongoClient: MongoClient = MongoClient("mongodb://localhost")
val database: MongoDatabase = mongoClient.getDatabase("dev")

val doc: Document = Document("name" -> "MongoDB", "type" -> "database",
  "count" -> 1, "info" -> Document("x" -> 203, "y" -> 102))

val collection: MongoCollection[Document] = database.getCollection("test")

val subscription = new Observer[Completed] {
  override def onNext(result: Completed): Unit = println("Inserted")

  override def onError(e: Throwable): Unit = println("Failed"+e.toString)

  override def onComplete(): Unit = println("Completed")
}

collection.insertOne(doc).subscribe(subscription)

The script above produces follwoing error when executed:

com.mongodb.MongoInterruptedException: Interrupted acquiring a permit to retrieve an item from the pool 

However, if I add Thread.sleep(3000) in the end it completes just fine.


Solution

  • I recommend using Promise object to notify completion of asynchronous jobs.

    http://www.scala-lang.org/api/2.12.1/scala/concurrent/Promise.html

    After asynchronous jobs finishing or after timeout, the program would exit.

    val promise = Promise[Boolean]
    
    ...
    
    override def onError(e: Throwable): Unit = {
      println("Failed"+e.toString)
      promise.success(false)
    }
    override def onComplete(): Unit = {
      println("Completed")
      promise.success(true)
    }
    
    val future = promise.future
    Await.result(future, Duration(10, java.util.concurrent.TimeUnit.SECONDS))
    //after completion, the program would exit.