插入文档时,WritableServerSelector从群集描述中选择的服务器没有

时间:2017-11-10 20:54:32

标签: mongodb scala observable future

我正在关注MongoDB Scala Drive Quick Tour指南并尝试插入文档。但是每当我这样做时,我都会看到以下信息

  

信息:WritableServerSelector从群集中选择的服务器没有   description ClusterDescription {type = UNKNOWN,connectionMode = SINGLE,   serverDescriptions = [{ServerDescription地址= ds155695.mlab.com:55695,   type = UNKNOWN,state = CONNECTING}]}。在计时之前等待30000毫秒   出

这是我的代码看起来像

val url: String = "mongodb://heroku_#######:#############@ds155695.mlab.com:55695/heroku_#########"
val mongoClient: MongoClient = MongoClient(url)
val db: MongoDatabase = mongoClient.getDatabase("heroku_#####")
val collection: MongoCollection[Document] = db.getCollection("omens")
println(collection)

val doc: Document = Document("_id" -> 3434, "name" -> "xxxxxx", "type" -> "yyyyyy")
val observable: Observable[Completed] = collection.insertOne(doc)
observable.subscribe(new Observer[Completed] {
  override def onNext(result: Completed): Unit = println("Inserted")
  override def onError(e: Throwable): Unit = println("Failed")
  override def onComplete(): Unit = println("Completed")
})

如果我更改我的代码以使用Futures,我会得到相同的消息。但是,如果我使用Await显式等待一段时间,它的工作原理。以下是快速浏览建议的Helpers.scala中定义的。

  trait ImplicitObservable[C] {
    val observable: Observable[C]
    val converter: (C) => String

    def results(): Seq[C] = Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))
    def headResult() = Await.result(observable.head(), Duration(10, TimeUnit.SECONDS))
    def printResults(initial: String = ""): Unit = {
      if (initial.length > 0) print(initial)
      results().foreach(res => println(converter(res)))
    }
    def printHeadResult(initial: String = ""): Unit = println(s"${initial}${converter(headResult())}")
  }

如果我执行以下操作,它将起作用并插入文档。

val result = collection.insertOne(doc).results()

但我觉得有点不理想,想要使用Futures或Observable。有人能指出我做错了吗?

这是完整的堆栈跟踪

INFO: Exception in monitor thread while connecting to server ds155695.mlab.com:55695
com.mongodb.MongoInterruptedException: Opening the AsynchronousSocketChannelStream failed
    at com.mongodb.connection.FutureAsyncCompletionHandler.get(FutureAsyncCompletionHandler.java:59)
    at com.mongodb.connection.FutureAsyncCompletionHandler.getOpen(FutureAsyncCompletionHandler.java:44)
    at com.mongodb.connection.AsynchronousSocketChannelStream.open(AsynchronousSocketChannelStream.java:62)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.InterruptedException
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1302)
    at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:231)
    at com.mongodb.connection.FutureAsyncCompletionHandler.get(FutureAsyncCompletionHandler.java:57)

0 个答案:

没有答案