akka getSender()将来会丢失

编程入门 行业动态 更新时间:2024-10-22 11:01:11
本文介绍了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()将来会丢失

本文发布于:2023-11-25 02:51:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628023.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将来   akka   getSender

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!