score:5

Accepted answer

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

score:3

the akka documentation for 2.6.x indicates that actormaterializer is deprectated: "use the system wide materializer or materializer.apply(actorcontext) with stream attributes or configuration settings to change defaults."

use this instead:

import akka.done
import akka.actor.actorsystem
import akka.actor.typed.behavior
import akka.actor.typed.scaladsl.behaviors
import akka.stream.materializer


object main {

  def main(args: array[string]) : unit = {
    val guardian: behavior[done] = behaviors.setup(_ => {
      behaviors.receivemessage{
        case done => behaviors.stopped
      }
    })

    val as = actorsystem()
    val materializer = materializer(as)
  }
}

score:6

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.


Related Query

More Query from same tag