Search code examples
scalaakkaakka-streamakka-typed

How to instantiate a materializer for AkkaStreams when I have a reference to the typed actor system?


The code below does not compile, it says that ActorMaterializer is missing an implicit ActorRefFactory. How should I provide one?

val guardian: Behavior[Done] = Behaviors.setup(_ => {
  Behaviors.receiveMessage{
    case Done => Behaviors.stopped
  }
})
implicit val sys = ActorSystem(guardian, "sys")
implicit val materializer: Materializer = ActorMaterializer()

Solution

  • Akka Streams at this point requires a "classic" (untyped) ActorSystem which can be implicitly converted into a materializer.

    So if materializing a stream inside an Akka Typed Behavior, one would

    implicit val materializer = context.classicActorContext.system
    

    And if materializing a stream outside of an actor but where you have a typed ActorSystem:

    implicit val materializer = typedActorSystem.classicSystem
    

    As mentioned by @johanandren, one can also put the Typed ActorSystem in the implicit scope, which will allow the implicit conversion to Materializer to take effect.

    implicit val system = context.system