This is kind of the number-one FAQ for Akka: you must never, ever use
"sender" in the results of a Future.  "sender" is only set *synchronously*,
while the receive method is running -- after that (in your Future's
onComplete), it's probably not set to anything, and *might* be set to
something wildly wrong.

This is why Futures are always kind of dangerous inside of Actors.  The
elegant solution is to use the Requester library
<https://github.com/jducoeur/Requester> to loopback the results of the
Future into your receive loop, but that may be overkill for such a simple
situation.  The easy solution is to simply store the value of sender in a
val before the Future, and use that val instead of explicitly using sender
in your onComplete.

On Fri, Oct 7, 2016 at 9:49 AM, Jegan <jega...@gmail.com> wrote:

> Hello Friends,
>
> I need your help in figuring out what is going wrong here. I have posted
> this question in SO too.
>
> I am using Akka Cluster (version 2.4.10) with few nodes designated for
> "front-end" role and few others as "workers". The workers are on remote
> machines. The incoming work is distributed by the front-end actor to
> workers by round-robin routing. The issue is sending back the response from
> the "workers" back to the front-end actor. I can see that the work is
> getting completed by the workers. But the message sent back by the workers
> to front-end does not reach and ends up as dead-letters. I see the below
> error in the log.
>
> [Cluster-akka.actor.default-dispatcher-21] [akka://Cluster/deadLetters]
> Message [scala.collection.immutable.$colon$colon] from
> Actor[akka://Cluster/user] to Actor[akka://Cluster/deadLetters] was not
> delivered. [6] dead letters encountered.
>
> The round-robin router configuration is as below.
>
> akka.actor.deployment {
>   /frontEnd/hm = {
>     router = round-robin-group
>     nr-of-instances = 5
>     routees.paths = ["/user/hmWorker"]
>     cluster {
>       enabled = on
>       use-role = backend
>       allow-local-routees = on
>     }
>   }
> }
>
> The router is instantiated in front-end actor like below.
>
> val router = context.actorOf(FromConfig.props(), name = "hm")
> val controller = context.actorOf(Props(classOf[Controller], router))
>
> The controller and the worker codes are below.
>
> // Node 1 : Controller routes requests using round-robin
> class Controller(router: ActorRef) extends Actor {
> val list = List("a", "b") // Assume this is a big list
> val groups = list.grouped(500)
>
> override def receive: Actor.Receive = {
> val futures = groups.map(grp => (router ? Message(grp)).mapTo[List[
> String]]))
> val future = Future.sequence(futures).map(_.flatten)
> val result = Await.result(future, 50 seconds)
> println(s"Result is $result")
> }
> }
>
> // Node 2
> class Worker extends Actor {
>     override def receive: Actor.Receive = {
> case Message(lst) =>
> val future: Future[List[String]] = // Do Something asynchronous
> future onComplete {
>    case Success(r) => sender.!(r)(context.parent) // This message is not
> delivered to Controller actor.
>    case Failure(th) => // Some error handling
> }
>     }
> }
>
> Appreciate your help.
>
> --
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/
> current/additional/faq.html
> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to akka-user+unsubscr...@googlegroups.com.
> To post to this group, send email to akka-user@googlegroups.com.
> Visit this group at https://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to