akka getSender()将来会丢失 [英] akka getSender() lost in future

查看:392
本文介绍了akka getSender()将来会丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在akka(java)中遇到发件人的引用问题,该问题以后会消失。这是代码:

I have a problem in akka (java) with the reference of the sender which disappears after a while in a 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());
  }
}

我可能错过了文档中的某些内容。

I might have missed something in the doc.

推荐答案

在演员部分的文档中对此进行了解释,并带有较大的警告标志:

It is explained in the Actors, section of the docs with a big warning sign:


警告使用将来的回调时,在actor中,您需要
小心避免关闭包含该actor的引用,即
不要调用方法或访问可变状态回调中
的封闭参与者。这将破坏actor的封装,并且
可能会引入同步错误和竞争条件,因为
回调将同时调度到封闭的actor。
不幸的是,还没有一种在编译时检测到这些非法访问
的方法。另请参见:参与者和共享的可变状态

Warning When using future callbacks, inside actors you need to carefully avoid closing over the containing actor’s reference, i.e. do not call methods or access mutable state on the enclosing actor from within the callback. This would break the actor encapsulation and may introduce synchronization bugs and race conditions because the callback will be scheduled concurrently to the enclosing actor. Unfortunately there is not yet a way to detect these illegal accesses at compile time. See also: Actors and shared mutable state

此处也有解释:

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

在 getSender()上关闭时您实际上是在关闭 this.getSender(),这意味着您正在关闭演员的内部状态,上面的文档告诉您不要这么做。

When you close over "getSender()" you're really closing over "this.getSender()", which means that you're closing over the internal state of the actor, which the docs above tells you not to.

我会将其添加到我们的常见问题解答中。

I will add this to our FAQ.

祝您好运,

这篇关于akka getSender()将来会丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆