sending messages to an actor inside a future

48 views
Skip to first unread message

John Stanford

unread,
Mar 20, 2012, 12:19:21 PM3/20/12
to akka...@googlegroups.com
Hi,

Just want to make sure I understand this properly. Let's say I have this actor:


class MyActor extends Actor {

val otherActor = context.parent ? MyOtherActorRequest mapTo manifest[ActorRef]

def receive = {

case m: Message ⇒ otherActor map (_ forward m)

case _ ⇒ ()
}

And let's say there are two messages in MyActor's mailbox. Will the otherActor future need to be completed before the second message it taken from the mailbox for processing? Is there a more appropriate way to deal with this other actor to avoid the map operation on every incoming message? Clearly I could Await it, but that would violate the goal of non-blocking.

Thanks,
John

Derek Williams

unread,
Mar 20, 2012, 12:29:20 PM3/20/12
to akka...@googlegroups.com
On Tue, Mar 20, 2012 at 10:19 AM, John Stanford <jxsta...@gmail.com> wrote:
Hi,

Just want to make sure I understand this properly.  Let's say I have this actor:

class MyActor extends Actor {
 val otherActor = context.parent ? MyOtherActorRequest mapTo manifest[ActorRef]
 def receive = {
   case m: Message => otherActor map (_ forward m)
   case _              => ()

 }

And let's say there are two messages in MyActor's mailbox.  Will the otherActor future need to be completed before the second message it taken from the mailbox for processing?  Is there a more appropriate way to deal with this other actor to avoid the map operation on every incoming message?  Clearly I could Await it, but that would violate the goal of non-blocking.

Another way of handling it is like this:
 
class MyActor extends Actor {
 context.parent ? MyOtherActorRequest foreach (self !)
 var otherActor: Option[ActorRef] = None
 def receive = {
   case m: Message => otherActor map (_ forward m)
   case a: ActorRef => otherActor = Some(a)
   case _              =>
 }

The problem with this is the messages wont be forwarded until you receive the other actor. They could be queued up in that case, and when the other actor is received the queue gets drained.

--
Derek Williams

Roland Kuhn

unread,
Mar 20, 2012, 1:20:42 PM3/20/12
to akka...@googlegroups.com
Hi John,

On Mar 20, 2012, at 17:19 , John Stanford wrote:

> Hi,
>
> Just want to make sure I understand this properly. Let's say I have this actor:
>
>
> class MyActor extends Actor {
>
> val otherActor = context.parent ? MyOtherActorRequest mapTo manifest[ActorRef]
>
> def receive = {
>

> case m: Message => otherActor map (_ forward m)
>
> case _ => ()


> }
>
> And let's say there are two messages in MyActor's mailbox. Will the otherActor future need to be completed before the second message it taken from the mailbox for processing? Is there a more appropriate way to deal with this other actor to avoid the map operation on every incoming message? Clearly I could Await it, but that would violate the goal of non-blocking.
>

As you are getting that ActorRef from the parent, why not simply pass it in as constructor argument? If it is a cyclic dependency thing, I’d model this actor as a state machine which starts out “queueing” and will turn into “forwarding” upon reception of that ActorRef (thereby draining the queue). This is the only way to retain the order of messages; if that is not needed, your solution should work just fine (although I would use foreach instead of map).

Regards,

Roland Kuhn
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn


John Stanford

unread,
Mar 20, 2012, 2:31:30 PM3/20/12
to akka...@googlegroups.com
Hi Roland and Derek,

Thanks for the ideas. I did simplify the case quite a bit for the example, and do need to replace the otherActor if it is recreated because it's parent restarted. This is probably a good case for an FSM and as Viktor was kind enough to point out in another conversation, Stash. Here we go!

Thanks,
John

> --
> You received this message because you are subscribed to the Google Groups "Akka User List" group.
> To post to this group, send email to akka...@googlegroups.com.
> To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.
>

Reply all
Reply to author
Forward
0 new messages