akka system.shutdown和awaitTermination

时间:2016-02-15 15:45:44

标签: akka jruby shutdown

在我的程序的最后调用以下代码(它是用JRuby编写的):

@na.tell(PoisonPill) if defined? @na   # @na, @sa and @pe are Actors
@sa.tell(PoisonPill) if defined? @sa
@pe.tell(PoisonPill) if defined? @pe

@@system.shutdown    # @@system is the ActorSystem
@@system.awaitTermination

我找到了这种方法here,但我不明白为什么会这样做。

awaitTermination等待所有Actors终止吗?

在调用@@system之前,awaitTermination被关闭了吗?

编辑:我注意到我甚至不需要致电tell(PoisonPill)。我评论了它,它仍然有用......

1 个答案:

答案 0 :(得分:0)

好的,我现在解决了。当我呼叫system.shutdown时,所有演员都会在当前任务之后终止。这不是我想要的,因为队列中可能有更多的任务。

所以我在主线程结束时向每个actor发送一个PoisonPill,然后等待它们终止。我还在每个actor中使用函数postStop设置finished标志,并在所有演员完成后关闭system

import Actors   # needed for Java-style poisonPill

actor1.tell(Actors::poisonPill)  # for Akka 2.0.0
actor2.tell(Actors::poisonPill)
system.awaitTermination