def apiCallback(e: ApiEvent) { this ! e }
I'm looking at migrations of scala actors onto akka ones. One (very) common paradigm I've used is the wrapping of an external callback-style API within an actor. That is, my actor class has some method in it which is called by (a Thread managed by) the external API:def apiCallback(e: ApiEvent) { this ! e }
So I simply pipe the event onto the mailbox of my actor. Obviously this exact snippet is not valid in Akka because "this" is not an ActorRef. I had *assumed* that it was *not* safe to replace "this" with "self" because it was not safe to use self from some thread not managed by Akka. But from playing around, this does not seem to be the case (i.e. it works just fine). However, I cannot find any documentation to the extent that it is always safe to call "self" from any thread. However, looking at the code, it's a val, so I presume that it is just fine. Can someone confirm?
Cheers,Chris--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/vnE6bSvIU1EJ.
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.
On Fri, May 25, 2012 at 5:50 PM, oxbow_lakes <oxbow...@gmail.com> wrote:I'm looking at migrations of scala actors onto akka ones. One (very) common paradigm I've used is the wrapping of an external callback-style API within an actor. That is, my actor class has some method in it which is called by (a Thread managed by) the external API:def apiCallback(e: ApiEvent) { this ! e }Problem is that if you close over self, you implcitly close over "this", which ay point to an old instance of the actor if it has been restarted since.so in these cases, just assign a val to self before and refer to the val instead, to avoid the capture.So I simply pipe the event onto the mailbox of my actor. Obviously this exact snippet is not valid in Akka because "this" is not an ActorRef. I had *assumed* that it was *not* safe to replace "this" with "self" because it was not safe to use self from some thread not managed by Akka. But from playing around, this does not seem to be the case (i.e. it works just fine). However, I cannot find any documentation to the extent that it is always safe to call "self" from any thread. However, looking at the code, it's a val, so I presume that it is just fine. Can someone confirm?Considered confirmed?Cheers,√
Cheers,Chris
--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/vnE6bSvIU1EJ.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.
def apiCallback(e: ApiEvent) { val me = self; me ! e }
val me = selfdef apiCallback(e: ApiEvent) { me ! e }
Problem is that if you close over self, you implcitly close over "this", which ay point to an old instance of the actor if it has been restarted since.so in these cases, just assign a val to self before and refer to the val instead, to avoid the capture.
"just assign a val to self before and refer to the val instead""Before" in the method, or in the class?Option1def apiCallback(e: ApiEvent) { val me = self; me ! e }
Option 2val me = selfdef apiCallback(e: ApiEvent) { me ! e }If self is a val itself, how can this make any difference?
On Friday, 25 May 2012 16:56:16 UTC+1, √ wrote:
Problem is that if you close over self, you implcitly close over "this", which ay point to an old instance of the actor if it has been restarted since.
so in these cases, just assign a val to self before and refer to the val instead, to avoid the capture.Akka Tech Lead
--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/yG2e-FajXOEJ.
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.
val me = selfdef apiCallback(e: ApiEvent) { me ! e }
To unsubscribe from this group, send email to akka-user+unsubscribe@googlegroups.com.
I assumed 'me' as a local variable, i.e. not a member.
Cheers,
V
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/8-h86cyXsiwJ.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
I'm looking at migrations of scala actors onto akka ones. One (very) common paradigm I've used is the wrapping of an external callback-style API within an actor. That is, my actor class has some method in it which is called by (a Thread managed by) the external API:def apiCallback(e: ApiEvent) { this ! e }
I still don't understand the difference. In the code:val me = selfdef apiCallback(e: ApiEvent) { me ! e }me is an "instance variable" - so I am still doing "outer.this.me" aren't I? In any case, won't the value of "self" from the "old" instance still be the "correct" ActorRef?
--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/ziGouHsc8dkJ.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+unsubscribe@googlegroups.com.
On May 26, 2012 11:59 AM, "oxbow_lakes" <oxbow...@gmail.com> wrote:
>
> Do you think it's a good idea to use reflection to circumvent the language in this way?
Yes. See archives as to why.
Cheers,
V
>>> 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.
>
> --
> You received this message because you are subscribed to the Google Groups "Akka User List" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/6A335TMNMcgJ.
>
> To post to this group, send email to akka...@googlegroups.com.
> To unsubscribe from this group, send email to akka-user+...@googlegroups.com.