我试图更好地了解Scala MongoDB src
使用scala mongodb驱动程序(api doc:http://mongodb.github.io/mongo-scala-driver/)
当我使用
时 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()
})
}
来自:
/**
* 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
调用subscribe似乎调用了一个新线程(因为它被称为subscribe),但我不知道从src调用这个新线程的位置?
Implicits用于实现这种布线'我使用observable.subscribe(new Observer[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
mongoClient.close();在insertOne方法完成之前调用。
所以insertOne或subscribe方法是异步的吗?
答案 0 :(得分:0)
不,subscribe(doOnNext, doOnError, doOnComplete)
来电subscribe(observer)
(您可以从问题中引用的实施中看到)。因此,如果它也是从那里调用的,那么你将获得无限循环。 "布线"在你写observer.subscribe(x => println(s"next = $x"), error => error.printStackTrace(), () => {})
之类的东西时使用。
不,subscribe
没有创建新主题。实现Observable
的类主要是从Java MongoDB驱动程序中包装类并调用它们自己的subscribe
方法,例如override def subscribe(observer: Observer[_ >: TResult]): Unit = observe(wrapped).subscribe(observer)
。这些subscribe
方法也不会启动新主题:有关解释,请参阅https://mongodb.github.io/mongo-java-driver/3.1/driver-async/reference/observables/。