Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 200
NicuMarasoiu Avatar asked Dec 08 '22 10:12

NicuMarasoiu


2 Answers

Previous answers are halfways to the indended new API.

The typed ActorSystem can be implicitly transformed to provide the system materialiser as well, so just having it available as an implicit should be enough.

For example:

Behaviors.setup { ctx =>
 implicit val system = ctx.system

 Source(1 to 10).runForeach(println)

 ...
}

The inner working of this is an implicit conversion available from the Materializer companion object that extracts the system materializer of a ClassicActorSystemProvider into a Materializer. Both the typed and the classic ActorSystem implements ClassicActorSystemProvider.

like image 54
johanandren Avatar answered Apr 12 '23 22:04

johanandren


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
like image 43
Levi Ramsey Avatar answered Apr 12 '23 23:04

Levi Ramsey