You should take a look at Akka Routing Capabilities. SmallestMailboxRouter may be what you are looking for.
Alternatively, you can simply create actors on demand, i.e. For each task, a new actor is dynamically created. The central actor keeps track of all current actors. After the working actor is completed, he sends PoisonPill and informs the owner about its shutdown (actively or through the standard Terminate message, which Akka will send to the controlling player). When there are no active active participants, i.e. More tasks, an actor-performer shuts down the system.
Adding after reading the comment: Take a look at the sources of SmallestMailboxLike , a Scala mixed in SmallestMailboxRouter . Warning: you must have basic Scala knowledge. But this is generally a good idea if you want to use Akka ... The isProcessingMessage(ActorRef) method can be understood as isNotIdle(ActorRef)
// Returns true if the actor is currently processing a message. // It will always return false for remote actors. // Method is exposed to subclasses to be able to implement custom // routers based on mailbox and actor internal state. protected def isProcessingMessage(a: ActorRef): Boolean = a match { case x: LocalActorRef ? val cell = x.underlying cell.mailbox.isScheduled && cell.currentMessage != null case _ ? false } // Returns true if the actor currently has any pending messages // in the mailbox, ie the mailbox is not empty. // It will always return false for remote actors. // Method is exposed to subclasses to be able to implement custom // routers based on mailbox and actor internal state. protected def hasMessages(a: ActorRef): Boolean = a match { case x: LocalActorRef ? x.underlying.mailbox.hasMessages case _ ? false }
Robert Petermeier
source share