Reputation: 1367
I am trying to work with RoundRobinRoutingLogic in Akka 2.4 to work through Jonas Boner's Pi calculation example. The docs only show how to create a router and then use route to send a message back to the sender. This is just resulting in a long list of deadletters.
class Master(numOfWorkers: Int, numOfMessages: Int, numOfElements: Int, listener: ActorRef) extends Actor {
var pi: Double = _
var numOfResults: Int = _
val start: Long = System.currentTimeMillis
val workerRouter = {
val routees = Vector.fill(5) {
val r = context.actorOf(Props[Worker])
context watch r
ActorRefRoutee(r)
}
Router(RoundRobinRoutingLogic(), routees)
}
def receive = {
case Calculate => for (i <- 0 until numOfMessages) workerRouter.route(Work(i * numOfElements, numOfElements), sender())
case Result(value) =>
pi += value
numOfResults += 1
if (numOfResults == numOfMessages) {
listener ! PiApproximation(pi, duration = (System.currentTimeMillis - start).millis)
context.stop(self)
}
}
}
I have a Worker actor defined that is getting created and added to the routees of the workerRouter. For the Calculate case, how can I send a Work message to the Worker that would be chosen next by the RoundRobinRoutingLogic?
Upvotes: 0
Views: 190
Reputation: 17923
The problem is that you are passing sender()
to the underlying worker ActorRef
as the destination for Results, instead of sending the ref to Master. If you modify the following line it should fix the problem you are seeing:
//note the "self" instead of "sender()"
case Calculate => for (i <- 0 until numOfMessages) workerRouter.route(Work(i * numOfElements, numOfElements), self)
Also, I would "break up" your code a bit as follows:
def sendWork(i : Int) =
workerRouter.route(Work(i * numOfElements, numOfElements), self)
def receive = {
case Calculate => (1 until numOfMessages) foreach sendWork
...
}
Upvotes: 1