简体   繁体   中英

Find Actor Status Akka Scala

Currently I have rest-endpoint-api-playframework that contains main actor to run n number of worker-actors doing batch-insertion. while in the process of doing that, I'd love to get current process of the inserts, lets say 100/1000. in order to do that, obviously I'll have the actor to report to the sender what is the status of the process.

class Application (implicit inj : Injector) extends Controller with Injectable {
  implicit val timeout = Timeout(5 seconds)
  val mainActor = system.actorOf(RoundRobinPool(100).props(Props(new SupervisorActor(0))), name = "helloactor")

  val future = mainActor ? ProcessBatch  

  val workerFuture = mainActor ? CurrentProcessedItem
  result = Await.result(workerFuture, timeout.duration).asInstanceOf[String] 

  Ok(Json.obj("return"->result.toString))  
}

above, I'd expect that if helloActor ? ProcessBatch helloActor ? ProcessBatch has completed, then I'd just return the process is completed, otherwise I'd run the mainActor ? CurrentProcessedItem mainActor ? CurrentProcessedItem that returns a future of the currentProcessedItem .

So basically I will need an indicator whether mainActor ? ProcessBatch mainActor ? ProcessBatch has completed or not. is it possible?

"Ask" returns you are Future . Therefore, onComplete you can do whatever you want:

val future = helloActor ? ProcessBatch
future.onComplete {
    case Success(result) => // Do stuff
}

However, I'd rather run helloActor ? ProcessBatch helloActor ? ProcessBatch within mainActor and "pipe" the result to mainActor .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM