Access to current message in actor

105 views
Skip to first unread message

Eric Pederson

unread,
Mar 17, 2015, 3:53:23 PM3/17/15
to akka...@googlegroups.com
Hi there -

Somewhat regularly I have a need to access the current message in an actor method outside of the receive.  It's usually some case where I want to do something generic with it, like logging.

There are two general approaches that I've used.  The first approach is pass the message down to where ever it's needed, for example:

def receive = {
  case f @ Foo(a, b) =>
    handleFoo(f, a, b)
  case b @ Bar(x, y, z) =>

    handleBar(b, x, y, z)
  many other cases...
}

Somewhere down the call chain everything calls a generic method that is passed the current message.
 
The other approach is to create a Receive wrapper (a la LoggingReceive) that captures the message in isDefinedAt and makes it available as a variable.

For example:

class MyActor extends CurrentMessageActor {
  def receive = CaptureCurrentMessage {
    case Foo(a, b) =>
    ...

  def genericMethod() {
    println(currentMessage)
  }


Neither approach is very satisfactory because of the boilerplate.  I'm wondering, if we have access to the sender, why not the current message?

Is this something that would be useful for other people?  Is there a reason why we wouldn't want to expose this in the regular Akka API?

Thanks,

Andrey Kuznetsov

unread,
Mar 17, 2015, 4:49:35 PM3/17/15
to akka...@googlegroups.com
Methods called outside of receive may be called asynchronously, what they would expect in `currentMessage` method?

Eric Pederson

unread,
Mar 17, 2015, 5:42:03 PM3/17/15
to akka...@googlegroups.com
Same rules apply as for sender().   Capture the value before running a Future. 
--
>>>>>>>>>> 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 a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/oDg5k5dhR8A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


--
Sent from Gmail Mobile

Eric Pederson

unread,
Mar 18, 2015, 6:55:08 PM3/18/15
to akka...@googlegroups.com
Actually, though, methods in an actor really shouldn't be called asynchronously. That would circumvent the actor model. That's why things like pipeTo exist, to minimize the chance that a future could interact with the guts of an actor. An actor's methods should really only be called from a thread started from receive, with few exceptions.

schoefts

unread,
Mar 19, 2015, 8:54:40 AM3/19/15
to akka...@googlegroups.com
I don't have the time to actually try this now, but one way to achieve this comes to mind:

The ActorCell underneath should have access to the message currently in work.
AFAIK, you can cast the context to ActorCell and just read the current message out of it.

For good reason, however, the ActorCell is an akka-private. - So, you'd have to put the described functionality in the akka packge.
I am not sure about the full implications when working with ActorCell, though.

... not a very neat solution, I know.

hth
-Tom

Eric Pederson

unread,
Mar 19, 2015, 9:35:59 AM3/19/15
to akka...@googlegroups.com
Thanks. Coincidentally I've recently considered that (using an akka package) for a class similar to LoggingReceive. LoggingReceive gets the actor class from the ActorCell to set the source of the Debug message sent to the event stream. I've worked around it for now by passing the actor's log into my constructor but it's annoying boilerplate.

Eric Pederson

unread,
Mar 24, 2015, 2:37:52 PM3/24/15
to akka...@googlegroups.com
Dear Akka Team - could you comment on this?

Thanks,

(I realized after the fact that posting the original question during ScalaDays was a bad idea...)

Patrik Nordwall

unread,
Mar 25, 2015, 5:38:47 AM3/25/15
to akka...@googlegroups.com
Hi Eric,

sender() has caused a lot of trouble in practise, and we would do it differently if we would have known. We don't want to add a similar problem by adding currentMessage().

Personally I think the best way is to grab it explicitly and pass it down when needed, but I can understand that you sometimes want to use something with less boilerplate.

I wouldn't grab it from the ActorCell, since that is very internal API that may change at any time. It is better to grab it in aroundReceive (also private[akka]) or use the Receive Pipeline Pattern (only in master).

Cheers,
Patrik

--
>>>>>>>>>>      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+...@googlegroups.com.

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



--

Patrik Nordwall
Typesafe Reactive apps on the JVM
Twitter: @patriknw

Eric Pederson

unread,
Mar 25, 2015, 11:44:28 AM3/25/15
to akka...@googlegroups.com
Thanks Patrik - 

The Receive Pipeline Pattern looks nice.  Absent a currentMessage() method a trait mixin is probably the cleanest approach.  I'm looking forward to trying it out. 


-- Eric

You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/oDg5k5dhR8A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.

Ryan Tanner

unread,
Mar 25, 2015, 12:11:48 PM3/25/15
to akka...@googlegroups.com
ReceivePipeline looks really great.  When do you think it'll be released?

Patrik Nordwall

unread,
Mar 26, 2015, 11:18:08 AM3/26/15
to akka...@googlegroups.com
On Wed, Mar 25, 2015 at 5:11 PM, Ryan Tanner <ryan....@gmail.com> wrote:
ReceivePipeline looks really great.  When do you think it'll be released?

In 2.4-M1. No ETA, we must get streams 1.0 out first.
/Patrik
Reply all
Reply to author
Forward
0 new messages