Creating a flow from actor in Akka Streams

后端 未结 3 443
天涯浪人
天涯浪人 2020-12-07 14:58

It\'s possible to create sources and sinks from actors using Source.actorPublisher() and Sink.actorSubscriber() methods respectively. But is it pos

3条回答
  •  無奈伤痛
    2020-12-07 15:20

    Konrad's solution demonstrates how to create a custom stage that utilizes Actors, but in most cases I think that is a bit overkill.

    Usually you have some Actor that is capable of responding to questions:

    val actorRef : ActorRef = ???
    
    type Input = ???
    type Output = ???
    
    val queryActor : Input => Future[Output] = 
      (actorRef ? _) andThen (_.mapTo[Output])
    

    This can be easily utilized with basic Flow functionality which takes in the maximum number of concurrent requests:

    val actorQueryFlow : Int => Flow[Input, Output, _] =
      (parallelism) => Flow[Input].mapAsync[Output](parallelism)(queryActor)
    

    Now actorQueryFlow can be integrated into any stream...

提交回复
热议问题