[英]Why does MongoDB observeOn not use the specified execution context?
在Scala中,我編寫了兩個MongoDB可觀察對象,並在傳遞自定義執行上下文時調用了observeOn。 對observeOn的調用是在第一個可觀察對象上進行的,但是自定義執行上下文不會傳播到第二個可觀察對象。
為了說明這一點,我編寫了下面的獨立代碼:
import java.util.concurrent.{LinkedBlockingQueue, ThreadPoolExecutor, TimeUnit}
import org.apache.commons.lang3.concurrent.BasicThreadFactory.Builder
import org.mongodb.scala.bson.collection.immutable.Document
import org.mongodb.scala.{MongoClient, Observable}
import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext}
object Test extends App {
val client = MongoClient("mongodb://localhost")
def insertObs = {
client.getDatabase("test").getCollection("test").insertOne(Document("test" -> 1))
}
val threadPool = new ThreadPoolExecutor(2, 2, 0L,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue[Runnable],
new Builder().namingPattern("Custom pool").build())
val executionContext = ExecutionContext fromExecutor (threadPool)
val obs = Observable(List(1, 2, 3))
val res =
obs.observeOn(executionContext).map {
i =>
println("OBS " + Thread.currentThread().getName)
i
}.flatMap(_ => insertObs.map {
i =>
println("INSERT " + Thread.currentThread().getName)
i
})
Await.result(res.toFuture(), Duration(20, TimeUnit.SECONDS))
}
其輸出如下:
OBS Custom pool
INSERT Thread-2
OBS Custom pool
INSERT Thread-2
OBS Custom pool
INSERT Thread-4
我期望在執行“ INSERT”可觀察對象時,僅將“自定義池”用作執行上下文,而不是將Thread-2和Thread-4用作執行上下文。 如此處文檔所述:
具體來說,它表示:將特定的執行上下文用於將來的操作
為什么自定義線程池不用於“插入”可觀察對象?
這似乎按預期工作:請參閱此票證: https : //jira.mongodb.org/browse/SCALA-437
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.