Akka HTTP是否适用于Akka Typed?

时间:2018-04-03 03:56:32

标签: scala akka akka-http akka-typed

val behavior: Behavior[Message] = Behaviors.setup {
  actorContext ⇒
    logger.info("starting...")

    implicit val actorSystem = actorContext.system.asInstanceOf[ActorSystem]
    implicit val materializer = ActorMaterializer()(actorContext.asInstanceOf[ActorContext])
    implicit val executionContext = actorContext.executionContext

    val route =
      path("hello") {
        get {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
        }
      }

    val bindingFuture = Http().bindAndHandle(route, "localhost", 8888)

编译好了,但是当我运行我的代码时,我得到了

20:44:22.586 [Leaderboard-akka.actor.default-dispatcher-3] INFO net.kolotyluk.leaderboard.service.REST$ - starting...
[ERROR] [04/02/2018 20:44:22.592] [Leaderboard-akka.actor.default-dispatcher-2] [akka://Leaderboard/user/Http]
akka.actor.typed.internal.adapter.ActorSystemAdapter cannot be cast to akka.actor.ActorSystem
akka.actor.ActorInitializationException: akka://Leaderboard/user/Http: exception during creation

这有一些简单的解决方案吗?我怎样才能给Http()一个ActorSystem所需要的东西?

Akka Typed似乎与其他Akka图书馆一起玩得不好

1 个答案:

答案 0 :(得分:4)

您的代码似乎是将类型化的ActorSystem转换为无类型的ActorSystem,因此出错。而是使用适配器:

import akka.actor.typed.scaladsl.adapter._

val bindingFuture = Http(actorContext.system.toUntyped).bindAndHandle(route, "localhost", 8888)

以下是测试类似内容的示例:

https://github.com/akka/akka-http/blob/bb682d39b0eb570e74f837829d0f9c13eeea2299/akka-http-tests/src/test/scala/akka/http/scaladsl/TypedActorSystemSpec.scala#L17