Mongodb 具有非默认akka执行上下文的Mongo scala驱动程序2.0

Mongodb 具有非默认akka执行上下文的Mongo scala驱动程序2.0,mongodb,scala,akka,akka-http,mongo-scala-driver,Mongodb,Scala,Akka,Akka Http,Mongo Scala Driver,我已经开始在我们的scala akka http项目中使用mongo scala驱动程序,它提供了很大的帮助,特别是v2.0.0中的case类支持非常好。我正试图了解如何使用mongo scala驱动程序和非默认执行上下文 由于java库依赖性的性质,我使用阻塞调用从MongoDB获取结果,如下所示。我使用下面的observeOn稍微修改了MongoDB的结果和headResult函数,但我注意到一些奇怪的竞争条件,我不知道如何解决 trait ImplicitObservable[C] {

我已经开始在我们的scala akka http项目中使用mongo scala驱动程序,它提供了很大的帮助,特别是v2.0.0中的case类支持非常好。我正试图了解如何使用mongo scala驱动程序和非默认执行上下文

由于java库依赖性的性质,我使用阻塞调用从MongoDB获取结果,如下所示。我使用下面的observeOn稍微修改了MongoDB的结果和headResult函数,但我注意到一些奇怪的竞争条件,我不知道如何解决

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

    def headResult()(implicit executionContext: ExecutionContext) = Await.result(observable.observeOn(executionContext).head(), Duration(10, TimeUnit.SECONDS))

    def results()(implicit executionContext: ExecutionContext): List[C] = Await.result(observable.observeOn(executionContext).toFuture(), Duration(20, TimeUnit.SECONDS)).toList
  }
results函数不会返回我期望的所有记录,并且每次的行为都不同,除了使用akka时,它只允许一个线程。由于这是一个阻塞操作,我想使用非默认的akka调度程序,这样它就不会阻塞我的HTTP请求。如果有人能帮我,我真的很感激

  # looking up dispatcher 
  val executionContext = system.dispatchers.lookup("mongo-dispatcher")

  # application.conf
  mongo-dispatcher {
    type = Dispatcher
    executor = "thread-pool-executor"
    thread-pool-executor {
      fixed-pool-size = 100
    }
    throughput = 1
  }
我的示例数据库客户端代码:

def getPersonById(personId: String): PersonCaseClass = {
    personCollection.find[PersonCaseClass](equal("_id", "person_12345")).first().headResult()
}

你确定没有两个东西同时消耗了
可观察的
,可能是通过调用
.results()
两次吗?我认为没有其他东西执行相同的可观察的,但竞争条件证明不是这样。下面是我调用mongo请求的方式
personCollection.find[PersonCaseClass](相等(“\u id”,“person\u 12345”)).first().headResult()