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()
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With