How to create routers in ank with parameterized participants?

I am trying to use a broadcast router in Scala, if I am not mistaken, it should look like this:

val system = ActorSystem("My beautiful system") val workerRouter = system.actorOf(Props[Agent].withRouter(BroadcastRouter(individualDefinitions.size)), name = "agentRouter") 

This is what I understand from the tutorial that I am following .

A working router acts like another actor, and I can send messages to this router, which will send them to all Agents (as many as I have separate definitions).

The problem is that I would like to use separate definitions for assembling agents, they actually take some parameters in the constructor, and these parameters are in individual definitions.

Q: How can I tell the router to pass these parameters to each of them as part of the constructor?

Please note that each actor must receive one individual definition, and they are all different. I cannot use the solution in a related question where the constructor gets constants: In the Java actor model Akka, can a router create actors with a non-default constructor?

Please note that each actor must have different parameters, if one of them is restarted, he should get the same parameters that he received in the first place. I do not know if this solution can be changed for this.

A possible solution may be to use the actor as a router, to separate the creation (constructor) and routing, as in the question of Akka (java), without blocking for all children .

I am not sure if this is the β€œright” approach in this case. Using an actor because the router has several problems (besides elegance). I’m worried about an actor who works like a router that restarts and loses all of his subscribers. If an actor restarts in half a cycle, some participants may also skip some messages if I am not mistaken.

Thanks.

+8
constructor scala akka routing
source share
2 answers

You can create routers by specifying as routes some already created participants built by any logic.

In the following example, two active participants will be created, which will be created in different ways, and then we will create a circular router that will send them messages.

 class MyActor(param1: String) extends Actor with ActorLogging { def receive: Actor.Receive = { case msg => log.info("Message from {}: {}", param1, msg) } } object MyActor { def apply(param: String): Props = Props(new MyActor(param)) } object Main extends App { val system = ActorSystem() val a1 = system.actorOf(MyActor("actor1")) val a2 = system.actorOf(MyActor("actor2")) val routerProps = Props.empty.withRouter(RoundRobinRouter(routees = Vector(a1, a2))) val router = system.actorOf(routerProps) for (i <- 1 to 10) { router ! i } readLine() system.shutdown() } 

More details here: http://doc.akka.io/docs/akka/2.2.0/scala/routing.html

+7
source share
 public class Master extends UntypedActor { ----- ----- public Master() { workerRouter = this.getContext().actorOf(Worker.createWorker().withRouter(new RoundRobinRouter(8)), "workerRouter"); } 

With Akka 2.4.2, we can simply use:

 workerRouter = this.getContext().actorOf(new RoundRobinPool(noOfWorkers).props(Props.create(Worker.class)), "workerRouter"); 

This is the best example of code executed in min. time in acc.

+1
source share

All Articles