基本的な質問は事前にお詫びください。私はhttp4sでScalaを学び始めています。ルータハンドラでは、MongoDBへのエントリを入力しようとしています。私が言うことができる限り、insertOne
はObservable[Completed]
を返します。http4s、Service ExecutorとMongodb:insertOneの終了を待つ方法
私はobservalbeが応答を返す前に完了するのをどのように待つことができますか?
私のコードは次のとおりです。 - あなたは少しそれをフォークと依存関係までする必要がありますが、scalaz 7.1および7.2は、バイナリ互換性がありません
class Routes {
val service: HttpService = HttpService {
case r @ GET -> Root/"hello" => {
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("scala")
val collection: MongoCollection[Document] = database.getCollection("tests")
val doc: Document = Document("_id" -> 0, "name" -> "MongoDB", "type" -> "database",
"count" -> 1, "info" -> Document("x" -> 203, "y" -> 102))
collection.insertOne(doc)
mongoClient.close()
Ok("Hello.")
}
}
}
class GomadApp(host: String, port: Int) {
private val pool = Executors.newCachedThreadPool()
println(s"Starting server on '$host:$port'")
val routes = new Routes().service
// Add some logging to the service
val service: HttpService = routes.local { req =>
val path = req.uri
val start = System.nanoTime()
val result = req
val time = ((System.nanoTime() - start)/1000)/1000.0
println(s"${req.remoteAddr.getOrElse("null")} -> ${req.method}: $path in $time ms")
result
}
// Construct the blaze pipeline.
def build(): ServerBuilder =
BlazeBuilder
.bindHttp(port, host)
.mountService(service)
.withServiceExecutor(pool)
}
object GomadApp extends ServerApp {
val ip = "127.0.0.1"
val port = envOrNone("HTTP_PORT") map (_.toInt) getOrElse (8787)
override def server(args: List[String]): Task[Server] =
new GomadApp(ip, port)
.build()
.start
}
OK、私はドキュメントを読んだ後にそれを手に入れたと思います。応答はタスクそのものであるはずです。 –
0.15文書を読んでください。 – Reactormonk