0
votes

I have been learning about Scala recently and got stuck on the routing concept. Overall I get the idea of what a router is all about, but I don't understand the distinction between standalone routers and self contained router actors.

I've read the documentation back and forth, and this is still very confusing to me: https://doc.akka.io/docs/akka/2.5.3/scala/routing.html

Could someone please explain me (with some code or reference if possible) what is the difference between standalone routers and self contained router actors?

Thank you in advance.

EDIT:

According the documentation:

A Router can be used inside or outside of an actor, and you can manage the routees yourselves or use a self contained router actor with configuration capabilities

So I guess, it's all about how routees are managed. In one case, they are managed manually, while in the other,...well I don't see yet how they are managed with self contained router actors.

EDIT 2:

Once again, according the documentation, this is what they refer to when they talk about self-contained actors. I actually always used routers like this, but then what are standalone routers? :

val router1: ActorRef = 
   context.actorOf(FromConfig.props(Props[Worker]), "router1")

EDIT 3:

I guess my confusion come from the following code. This is referred to as a Standalone router:

Router(RoundRobinRoutingLogic(), routees)

What is the difference compared to this?:

val router1: ActorRef = 
   context.actorOf(FromConfig.props(Props[Worker]), "router1")

Is there any difference in terms of performances or capabilities?

1

1 Answers

0
votes

The main difference lies how the routee are managed. You can manage the routees yourselves or use a self contained router actor with configuration capabilities.

Case 1- For managing the routees, you have to create the routee actors and add to the routers. Watch for routee actors being terminated, so they can be added again to the routers. Eg- picked from akka doc

var router = {
    val routees = Vector.fill(5) {
      val r = context.actorOf(Props[Worker])
      context watch r
      ActorRefRoutee(r)
    }
    Router(RoundRobinRoutingLogic(), routees)

As you can see the actor which creates the router has to make sure routees are added.

Case 2- Self Managed router are just like Thread Pool(Java Thread pool). It is collection of routee actors managed by router Pool , just like thread pool mange the threads. There are some advantages like dynamically resizing capability under load. Routee creation done automaticaly.

new RoundRobinPool(5, new DefaultResizer(1, 10))

Basically self contained router(pool or group) can be considered as a wrapper.

Edit-> Group router holds the path of the routees, not the actor ref.So irrespective of routee being dead or not, will route the message to it. Probable use case of group router can be multiple group routers sharing the routees.