5
votes
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)

compiles okay, but when I run my code I get

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

Is there some simple solution to this? How can I give Http() what it needs for an ActorSystem?

Akka Typed does not seem to play well yet with other Akka libraries

1
Take a look at this exampleBranislav Lazic

1 Answers

6
votes

Your code seems to be casting a typed ActorSystem to an untyped ActorSystem, hence the error. Instead use the adapter:

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

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

Here's an example that tests that a similar thing works:

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