2

Current play framework implementation for flow implementation for an actor class is written without back-pressure. I would like to implement with back-pressure replacing actorRef with actorRefWithAck. But the actor flow is written in Scala, which I'm not familiar with. Can anyone explain how this class can be written in java ?

ActorFlow class as follows:

object ActorFlow {
      /**
       * Create a flow that is handled by an actor.
       *
       * Messages can be sent downstream by sending them to the actor passed into the props function.  This actor meets
       * the contract of the actor returned by [[http://doc.akka.io/api/akka/current/index.html#akka.stream.scaladsl.Source$@actorRef[T](bufferSize:Int,overflowStrategy:akka.stream.OverflowStrategy):akka.stream.scaladsl.Source[T,akka.actor.ActorRef] akka.stream.scaladsl.Source.actorRef]].
       *
       * The props function should return the props for an actor to handle the flow. This actor will be created using the
       * passed in [[http://doc.akka.io/api/akka/current/index.html#akka.actor.ActorRefFactory akka.actor.ActorRefFactory]]. Each message received will be sent to the actor - there is no back pressure,
       * if the actor is unable to process the messages, they will queue up in the actors mailbox. The upstream can be
       * cancelled by the actor terminating itself.
       *
       * @param props A function that creates the props for actor to handle the flow.
       * @param bufferSize The maximum number of elements to buffer.
       * @param overflowStrategy The strategy for how to handle a buffer overflow.
       */
      def actorRef[In, Out](
          props: ActorRef => Props,
          bufferSize: Int = 16,
          overflowStrategy: OverflowStrategy = OverflowStrategy.dropNew
      )(implicit factory: ActorRefFactory, mat: Materializer): Flow[In, Out, _] = {
        val (outActor, publisher) = Source
          .actorRef[Out](bufferSize, overflowStrategy)
          .toMat(Sink.asPublisher(false))(Keep.both)
          .run()
    
        Flow.fromSinkAndSource(
          Sink.actorRef(
            factory.actorOf(Props(new Actor {
              val flowActor = context.watch(context.actorOf(props(outActor), "flowActor"))
    
              def receive = {
                case Status.Success(_) | Status.Failure(_) => flowActor ! PoisonPill
                case Terminated(_)                         => context.stop(self)
                case other                                 => flowActor ! other
              }
    
              override def supervisorStrategy = OneForOneStrategy() {
                case _ => SupervisorStrategy.Stop
              }
            })),
            Status.Success(())
          ),
          Source.fromPublisher(publisher)
        )
      }
}
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
sasankad
  • 3,543
  • 3
  • 24
  • 31

0 Answers0