How do I control the run-time execution of an actor system in Akka?

257 Views Asked by At

I have been trying to figure out a way to control the run-time message passing within an actor system with some sort of external (separate from the actor system) controller. In other words, given an actor system (that I do not want to change): how do I set up a sort of controller that controls the message passing within it?

For example, imagine that the given actor system have the following setup:

object Program extends App {
  val system = ActorSystem("system")
  val B = system.actorOf(Props[B], "B")
  val A = system.actorOf(Props(new A(B)), "A")
  A ! "Start"
  system.terminate()
}
class A(B: ActorRef) extends Actor {
  def receive  = {case "Start" => B ! "Message"}
}
class B extends Actor {
  def receive = {case "Message" => println("Some logic")}
}

I want to accomplish the following:

  • Run this program synchronously, on a single thread
  • For each message being passed within the system: examine the content, sender and recipient, and based on that; execute some logic.

In the above example, I would want a "controller" that would do something like:

  1. Actor A received message "Start" from the outside and sends "Message" to Actor B
  2. Perform some blocking logic on the controller, i.e the actor system will idly wait for this logic to be performed.
  3. Now that the logic has been executed, the controller sends a green light for the actor system to resume the message passing.
  4. Actor B receives "Message" and prints "Some Logic"
  5. Controller checks whether the actor system is terminated, which it is, and performs some additional logic.

In short, I want the external controller to be able to control the message passing within the actor system at run-time.

I was thinking that this controller could possibly be implemented using a dispatcher, router actor logic and futures. I didn't find any examples in the Akka documentation regarding this, so is this even possible to accomplish?

0

There are 0 best solutions below