Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 插入文档时,WritableServerSelector没有从群集描述中选择服务器_Mongodb_Scala_Observable_Future - Fatal编程技术网

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

Mongodb 插入文档时,WritableServerSelector没有从群集描述中选择服务器,mongodb,scala,observable,future,Mongodb,Scala,Observable,Future,我正在按照指南,并试图插入一个文档。但每当我这样做的时候,我总是看到下面的信息 信息:WritableServerSelector未从群集选择任何服务器 description ClusterDescription{type=UNKNOWN,connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=ds155695.mlab.com:55695, 类型=未知,状态=正在连接}]}。在计时之前等待30000毫秒 出去

我正在按照指南,并试图插入一个文档。但每当我这样做的时候,我总是看到下面的信息

信息:WritableServerSelector未从群集选择任何服务器 description ClusterDescription{type=UNKNOWN,connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=ds155695.mlab.com:55695, 类型=未知,状态=正在连接}]}。在计时之前等待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,我会收到相同的消息。但是,如果我使用wait来显式地等待一段时间,它就可以工作了。根据快速教程的建议,在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()
但我发现这有点不太理想,我想用期货或可观察的。有人能指出我做错了什么吗

这是完整的堆栈跟踪

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)

应用程序和heroku群集之间是否有防火墙?超时最有可能是由于数据包在被允许进入heroku服务器之前被防火墙丢弃。我认为情况并非如此。我可以使用其他客户端插入文档。另外,如果我使用了相同的应用程序,并且我只是尝试将数据库切换到本地数据库,那么我可以使用相同的应用程序插入文档。它给了我同样的错误,所以我高度怀疑这是否是Heroku或mlab特定的问题。在应用程序和Heroku群集之间是否有防火墙?超时最有可能是由于数据包在被允许进入heroku服务器之前被防火墙丢弃。我认为情况并非如此。我可以使用其他客户端插入文档。另外,如果我使用了相同的应用程序,并且我只是尝试将数据库切换到本地数据库,那么我可以使用相同的应用程序插入文档。它给了我同样的错误,所以我高度怀疑这是Heroku还是mlab的具体问题