理解mongodb Scala src中的隐式和订阅

理解mongodb Scala src中的隐式和订阅,mongodb,scala,Mongodb,Scala,我试图更好地理解Scala MongoDB src 使用scala mongodb驱动程序(api文档:) 当我使用 val collection: MongoCollection[Document] = database.getCollection("mycollection"); val observable: Observable[Completed] = collection.insertOne(doc) observable.subscribe(new

我试图更好地理解Scala MongoDB src

使用scala mongodb驱动程序(api文档:)

当我使用

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

      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")
      })
这是隐式方法吗

/**
     * Subscribes to the [[Observable]] and requests `Long.MaxValue`.
     *
     * Uses the default or overridden `onNext`, `onError`, `onComplete` partial functions.
     *
     * @param doOnNext anonymous function to apply to each emitted element.
     * @param doOnError anonymous function to apply if there is an error.
     * @param doOnComplete anonymous function to apply on completion.
     */
    def subscribe(doOnNext: T => Any, doOnError: Throwable => Any, doOnComplete: () => Any): Unit = {
      observable.subscribe(new Observer[T] {
        override def onSubscribe(subscription: Subscription): Unit = subscription.request(Long.MaxValue)

        override def onNext(tResult: T): Unit = doOnNext(tResult)

        override def onError(throwable: Throwable): Unit = doOnError(throwable)

        override def onComplete(): Unit = doOnComplete()

      })
    }
src:

致电:

 /**
   * Request `Observable` to start streaming data.
   *
   * This is a "factory method" and can be called multiple times, each time starting a new [[Subscription]].
   * Each `Subscription` will work for only a single [[Observer]].
   *
   * If the `Observable` rejects the subscription attempt or otherwise fails it will signal the error via [[Observer.onError]].
   *
   * @param observer the `Observer` that will consume signals from this `Observable`
   */
  def subscribe(observer: Observer[_ >: T]): Unit
src:

似乎调用subscribe调用了一个新线程(它被称为subscribe),但我看不出这个新线程是从src调用的

隐式连接用于实现这种“连接”,当我使用
observable.subscribe时,它调用隐式subscribe方法(newobservator[Completed]{..

更新:

使用此代码:

import org.mongodb.scala.MongoClient;
import org.mongodb.scala.bson.collection.immutable.Document;
import org.mongodb.scala._
import org.scalatest._
import Matchers._
import org.mongodb.scala._

class MongoSpec extends FlatSpec with Matchers {

  "Test MongoDb" should "insert" in {
    {
      val mongoClient: MongoClient = MongoClient()
      val database: MongoDatabase = mongoClient.getDatabase("scala-poc");

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

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

      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(" \n\nFailed " + e + "\n\n")
        override def onComplete(): Unit = println("Completed")
      })

      mongoClient.close();

    }

  }
}
导致以下异常的原因:

Failed com.mongodb.MongoClientException: Shutdown in progress
在insertOne方法完成之前,正在调用mongoClient.close()

那么insertOne或subscribe方法是异步的

  • 不,
    subscribe(doOnNext,doOnError,doOnComplete)
    调用
    subscribe(observer)
    (正如您在问题中引用的实现所示)。因此,如果也从那里调用它,您将得到一个无限循环。当您编写类似
    observer.subscribe(x=>println(s“next=$x”)的内容时,将使用“连线”,error=>error.printStackTrace(),()=>{})

  • 不,
    subscribe
    不会创建新线程。实现
    Observable
    的类主要从Java MongoDB驱动程序包装类,并调用它们自己的
    subscribe
    方法,例如,这些
    subscribe
    方法也不会启动新线程:请参阅以获取一些解释

  • 不,
    subscribe(doOnNext,doOnError,doOnComplete)
    调用
    subscribe(observer)
    (正如您在问题中引用的实现所示)。因此,如果也从那里调用它,您将得到一个无限循环。当您编写类似
    observer.subscribe(x=>println(s“next=$x”)的内容时,将使用“连线”,error=>error.printStackTrace(),()=>{})

  • 不,
    subscribe
    不会创建新线程。实现
    Observable
    的类主要从Java MongoDB驱动程序包装类,并调用它们自己的
    subscribe
    方法,例如,这些
    subscribe
    方法也不会启动新线程:请参阅以获取一些解释


  • 请参阅更新,似乎发生了异步操作,我不确定在何处。请参阅更新,似乎发生了异步操作,我不确定在何处。为此,您应该查看Java驱动程序源代码,而不是Scala,它是一个简单的包装器。当然,
    insertOne
    是异步的,因为Scala驱动程序使用的是异步Java驱动程序。为此,您应该查看Java驱动程序源代码,而不是Scala,它是一个简单的包装器。但是当然,
    insertOne
    是异步的,因为Scala驱动程序使用的是异步Java驱动程序。