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,