How does .withRouter(new SmallestMailboxPool()) work?

Hi,
I am confused how .withRouter(new SmallestMailboxPool(size)) works.

I am looking at some code where the actor is spawned like so -

system.actorOf(ActorA.props(5), "ActorA")

within the ActorA class


public ActorA() {
 log.info("created")
}

public static Props props(int poolSize) {

 return Props.create(ActorA.class, () -> new actorA())
             .withRouter(new SmallestMailboxPool(poolsize)).withDispatcher("custom_dispatcher");
}

According to the documentation, new SmallestMailboxPool(poolSize) is a way of routing the messages to other actors. However in this code snippet I have shared above, there are no routees being spawned in the constructor, so what is the use of .withRouter() here? I noticed that the log statement inside the constructor gets logged the number of times as poolsize. So does .withRouter(new SmallestMailboxPool(poolsize)) create the routees as well? Because if something inside the constructor is getting logged, I would assume it means that multiple instances are being created.

Confused about this…

The props created with Props.create(...).withRouter() makes it a classic actor pool when started, spawning off poolsize routee children to route messages to.

If you are new to Akka I’d recommend that you do not invest time in the classic router APIs unless you have a specific need to understand them and instead reach for the new typed APIs.

1 Like

Oh, so if I understood correctly, it creates the poolsize number of routees, with the “SmallestMailboxPool” routing methodology?

Yes, an older project is using Akka-classic. We are migrating to Akka-typed, is much more easier to understand.

Yes, that sounds like you understood it correctly. :+1: