Following code is called at the very end of my program (it's written in 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
I found this approach here but I don't understand why it works.
Does awaitTermination
wait for all Actors to terminate?
Isn't @@system
shutted down before awaitTermination
is called?
edit: I noticed that I doesn't even need to call tell(PoisonPill)
. I commented it out and it still works...
Okay I solved it now. When I call system.shutdown
all actors terminate after their current task. That's not what I want because there could be more tasks in the queue.
So I send a PoisonPill to each actor at the end of my main thread and then wait for them to terminate. I also use the function postStop
in each actor to set a finished
flag and shut down the system
when all actors have finished.
import Actors # needed for Java-style poisonPill
actor1.tell(Actors::poisonPill) # for Akka 2.0.0
actor2.tell(Actors::poisonPill)
system.awaitTermination