gpt4 book ai didi

scala - 是否可以在 Actor 内发出 Akka HTTP 核心客户端请求?

转载 作者:行者123 更新时间:2023-12-04 10:36:33 25 4
gpt4 key购买 nike

下面是一个简单的 actor,它需要进行 HTTP 调用以从 API 接收数据。根据Akka HTTP Core Request-Level Client-Side API只有 ActorSystemActorMaterializer隐含地需要。

class MyActor extends Actor {

import context.system
implicit val materializer = ActorMaterializer()

override def receive: Receive = {
case _ => {
val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = "http://akka.io"))
responseFuture onComplete {
case Success(res) => println(res)
case Failure(t) => println("An error has occured: " + t.getMessage)
}
}
}
}

但是,在尝试编译应用程序时,我收到以下错误消息:

Error:(18, 48) ambiguous implicit values: both value context in trait Actor of type => akka.actor.ActorContext and method system in trait ActorContext of type => akka.actor.ActorSystem match expected type akka.actor.ActorRefFactory
implicit val materializer = ActorMaterializer()

Error:(18, 48) implicit ActorRefFactory required: if outside of an Actor you need an implicit ActorSystem, inside of an actor this should be the implicit ActorContext
implicit val materializer = ActorMaterializer()

Error:(18, 48) not enough arguments for method apply: (implicit context: akka.actor.ActorRefFactory)akka.stream.ActorMaterializer in object ActorMaterializer. Unspecified value parameter context.
implicit val materializer = ActorMaterializer()

Error:(22, 70) could not find implicit value for parameter fm: akka.stream.Materializer
val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = "http://akka.io"))

Error:(22, 70) not enough arguments for method singleRequest: (implicit fm: akka.stream.Materializer)scala.concurrent.Future[akka.http.scaladsl.model.HttpResponse]. Unspecified value parameter fm.
val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = "http://akka.io"))

Error:(23, 22) Cannot find an implicit ExecutionContext. You might pass an (implicit ec: ExecutionContext) parameter to your method or import scala.concurrent.ExecutionContext.Implicits.global.
responseFuture onComplete {

Error:(23, 22) not enough arguments for method onComplete: (implicit executor: scala.concurrent.ExecutionContext)Unit. Unspecified value parameter executor.
responseFuture onComplete {

这是在 Akka Actor 内进行 HTTP 调用的正确方法吗?

编辑

已包含 import ExecutionContext.Implicits.global修复最后两个 ExecutionContext错误。

最佳答案

创建 ActorMaterializer 需要一个隐式的 ActorRefFactory。 Actor trait 中定义的上下文属性扩展了ActorRefFactory,并且它是隐式的。您显式导入的上下文系统属性是 ActorRefFactory 的另一个隐式候选对象,因为 ActorSystem 扩展了 ActorRefFactory。

我的建议是删除导入并将其明确传递到需要的地方。

class MyActor extends Actor {

// Do not import context.system
// import context.system
implicit val materializer = ActorMaterializer()

override def receive: Receive = {
case _ => {
// use context.system explicitly
val responseFuture: Future[HttpResponse] = Http(context.system)
.singleRequest(HttpRequest(uri = "http://akka.io"))
responseFuture onComplete {
case Success(res) => println(res)
case Failure(t) => println("An error has occured: " + t.getMessage)
}
}
}
}

关于scala - 是否可以在 Actor 内发出 Akka HTTP 核心客户端请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32240359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com