Akka getSender () lost in the future

I have a problem in akka (java) with a sender link, which after a while disappears in the future. Here is the code:

class MyActor extends UntypedActor { @Override public void onReceive(Object msg){ Future<Integer> future = Futures.future(new Callable<Integer>(){ @Override public Integer call() throws Exception { System.out.println(getSender()); //works fine Thread.sleep(1000); System.out.println(getSender()); //show deadLetter return 42; } },getContext().dispatcher()); //do something with the future, pipe it or whatever Patterns.pipe(future,getContext().dispatcher(),getSender()); } } 

Maybe I missed something in the dock.

+2
source share
1 answer

This is explained in the Actors section, the section of documents with a large warning sign:

A warning. When using future callbacks inside participants, you should carefully avoid closing the link to the contained actors, i.e. do not call methods or access to mutable state on the surrounding actor from in the callback. This will violate the encapsulation of the actor and may introduce synchronization errors and race conditions, since the callback will be scheduled at the same time as the surrounding actor. Unfortunately, there is as yet no way to detect these illegal calls at compile time. See Also: Actors and General Mutable State

It also explains:

http://doc.akka.io/docs/akka/2.0.2/general/jmm.html#jmm-shared-state

When you close "getSender ()", you really close "this.getSender ()", which means that you are closing the internal state of the actor, which is not mentioned in the docs above.

I will add this to our FAQ.

Happy hakking,

√

+4
source

Source: https://habr.com/ru/post/1444868/


All Articles