0

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?

  • Hi @Randyll , if you want to accomplish something in synchronous way, then I would suggest make use of Futures and then use map or flatmap on them, I can write up a solution for u in Futures – zenwraight Mar 25 '18 at 16:09
  • So, you want something like an aspect-oriented akka?.. I'm not sure why someone would sit down and reimplement essentially the entire akka infrastructure with such unconventional constraints. Could it be an XY-problem, what is it that you are actually trying to accomplish? – Andrey Tyukin Mar 25 '18 at 16:17
  • @zenwraight Thank you for the response. I have no constraints on how I implement the controller, i.e, I could do it entirely using futures. I do not however want to change the given actor system. Could you show me a way to implement the controller on that simple actor system? – Randyll Tarly Mar 25 '18 at 16:25
  • @AndreyTyukin That is exactly what I'm trying to accomplish. This is not an XY-problem: I'm actually trying to do what I asked. Basically, I want to analyze a given Akka program by implementing above controller logic. Using the controller, I would like to change the behaviour of the program at run-time (i.e killing certain actors or creating new ones). Thus, I'm not trying to rewrite the akka program, I'm merely trying to change the behaviour of it at run-time. – Randyll Tarly Mar 25 '18 at 16:33

0 Answers0