Akka-Java Routing Issue

41 views
Skip to first unread message

Vishwa Bhat

unread,
Apr 25, 2017, 3:12:28 AM4/25/17
to Akka User List
I have the following issue in Akka-Java.

I have one Parent Actor `MainActor.class` and this parent has 5 child routes. Following is the hierarchy:

My App => Main Actor => [Child Route-1,Child-Route-2,..]

Simple Use case is String input is parsed to Integer as output: 

MyApp ===ask[string input]===> Main Actor ==route==> Child(parses to Integer) === integer result===> Main Actor ===result==> MyApp

Here is the Main Actor snippet:


 class MainActor extends UntypedActor{
    
       Router router;
       {
         // ...routes are configured here
       }
    
       public void onReceive(Object message){
         if(message instanceof String){
             router.route(message,self()); // route it to child
         }else if(message instanceof Integer){
            // received from child, 'tell' this result to actual sender/parent i.e, MyApp
            sender().tell(message,self()); 
         }else unhandled(message);
        }
 }

And Child Actor does nothing but String parsing to Integer and takes the result and sends it back to it's sender by `sender().tell(result,getContext().parent())`

Issue

MainActor is sending the Parsed integer result sent by child back to child itself instead of `MyApp`. I also tried replacing `sender()` to `getContext().parent()` in `MainActor` but still it did not work.

Arnout Engelen

unread,
Apr 25, 2017, 4:08:09 AM4/25/17
to akka...@googlegroups.com
Hi Vishwa,

You correctly noticed that, when handling the Integer message that was sent to the 'MainActor' by your child actor, 'sender()' will refer to that child actor.

If you want the response to go to the 'original' sender ('MyApp'), one solution might be to use 'sender()' as the second parameter to 'route': this will tell the child to send its response directly to 'MyApp', effectively 'forwarding' the message.


Kind regards,

Arnout

--
>>>>>>>>>> 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+unsubscribe@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Arnout Engelen

Vishwa Bhat

unread,
Apr 25, 2017, 4:33:40 AM4/25/17
to Akka User List
Nice, That's one solution but what if I have to do some processing on 'MainActor' when I get response from child and then I have to send to 'MyApp'?

Regards,
Vishwa
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Arnout Engelen

unread,
Apr 25, 2017, 4:44:15 AM4/25/17
to akka...@googlegroups.com
There's a number of possibilities: if the processing is stateless you could use the 'ask' and 'pipe' patterns and 'map' ( http://doc.akka.io/docs/akka/current/java/futures.html#Use_with_Actors ).

Another approach might be to wrap the messages to and from the child actors in an 'envelope' that also contains some kind of unique 'correlation id', and in MainActor keep a Map from correlation id to original sender.


Arnout

To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Guido Medina

unread,
May 3, 2017, 5:27:37 AM5/3/17
to Akka User List
I'm not sure if you can "forward" instead of "route" to a router, if not you could probably set the router sender to sender() and just reply to the sender from the processing actor,
that way you won't need to correlate, it should be do-able using existing sender() reply mechanism.

The trick would be to:

router.route(message,sender());

HTH,

Guido.
Reply all
Reply to author
Forward
0 new messages