Same rules apply as for sender().   Capture the value before running a
Future.

On Tuesday, March 17, 2015, Andrey Kuznetsov <[email protected]> wrote:

> Methods called outside of receive may be called asynchronously, what they
> would expect in `currentMessage` method?
>
> On Tuesday, March 17, 2015 at 10:53:23 PM UTC+3, Eric Pederson wrote:
>>
>> 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,
>>
>  --
> >>>>>>>>>> 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
> [email protected]
> <javascript:_e(%7B%7D,'cvml','akka-user%[email protected]');>
> .
> To post to this group, send email to [email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>.
> 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

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

Reply via email to