【发布时间】:2020-09-26 02:58:37
【问题描述】:
我正在尝试使用带有 akka-http 的 scala 启动 REST API。我是 akka 和 actor 模型范例的新手,所以我想实现一个类型化的 actor 系统,但我得到了这个 sbt 编译错误:
could not find implicit value for parameter system: akka.actor.ActorSystem (implicit ActorRefFactory required: if outside of an Actor you need an implicit ActorSystem, inside of an actor this should be the implicit ActorContext)
[error] val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
知道这里发生了什么吗?这是我基于 akka 文档中的示例的代码:
object FeedAggregatorServer {
def apply(): Behavior[Nothing] =
Behaviors.setup[Nothing](context => new FeedAggregatorServer(context))
}
class FeedAggregatorServer(context: ActorContext[Nothing]) extends AbstractBehavior[Nothing](context) {
context.log.info("Application started")
override def onMessage(msg: Nothing): Behavior[Nothing] = {
// No need to handle any messages
Behaviors.unhandled
}
override def onSignal: PartialFunction[Signal, Behavior[Nothing]] = {
case PostStop =>
context.log.info("Application stopped")
this
}
}
object FeedAggregatorApp {
def main(args: Array[String]): Unit = {
ActorSystem[Nothing](FeedAggregatorServer(), "FeedAggregatorServer")
val route =
concat (
path("") {
complete("Hello, World!")
},
path("feed") {
complete("Hello!")
}
)
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
}
路由应该在 FeedAggregatorServer 中,或者...?
【问题讨论】:
-
Akka Http 使用经典的actorsystem,您可能需要将类型化系统转换为经典系统。 doc.akka.io/docs/akka/current/typed/coexisting.html