2

Is there a mechanism I can use to kill an actor after it has been created for 20 seconds ?

Reading about the various SuperVisor strategies at http://doc.akka.io/docs/akka/snapshot/java/fault-tolerance.html

it seems killing an actor is all based on message passing of exceptions ?

blue-sky
  • 51,962
  • 152
  • 427
  • 752

4 Answers4

9

You can use the Scheduler to send a PoisonPill message to the actor after 20s, that will kill the actor automatically when the actor processes that message; no extra code required within the actor. If you want to kill the actor straight away, and not wait for the poison pill to work its way through the message queue then use stop instead.

system.scheduler().scheduleOnce(Duration.create(20, TimeUnit.SECONDS),
    new Runnable() {
        @Override
        public void run() {
            testActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
        }
    }, system.dispatcher());
Community
  • 1
  • 1
Chris K
  • 11,622
  • 1
  • 36
  • 49
  • thanks, how can I check when testActor has the PoisonPill sent to it ? Is there a type within onReceive that allows me to check if a PoisonPill message has been sent ? – blue-sky Oct 07 '14 at 16:17
  • @blue-sky The actor will be dead, so little point in the actor asking that question. There are shutdown hooks that one can use to trigger extra behavior, or you can have the actor schedule its own death. I have answered the question that you originally asked, but your last comment makes me wonder what it is that you are actually trying to do? – Chris K Oct 07 '14 at 16:20
  • I just want a method of monitoring what actors are killed. I accept your point, the question is answered but I don't know how to verify an actor is killed. – blue-sky Oct 07 '14 at 16:24
  • @blue-sky. I remember having a similar problem awhile back. I cannot remember how I solved it, I will do some digging for you tomorrow. However if I recall correctly it all changed when Akka added clustering, which I have not used in anger yet. – Chris K Oct 07 '14 at 19:47
  • 1
    @blue-sky it looks like the `DeathWatch` component will tell you when actors die; it is a subscription based service. Have a read of http://doc.akka.io/docs/akka/2.3.6/java/untyped-actors.html#deathwatch-java. – Chris K Oct 08 '14 at 08:13
  • thanks, approaches I have tried do not seem to notify actor when child actor terminates. I've opened a new question case your interested http://stackoverflow.com/questions/26255802/message-not-being-sent-when-akka-actor-is-killed-using-deathwatch – blue-sky Oct 08 '14 at 11:30
3

Similar to Chris K's answer, but taking a slightly different approach, you could make the actor schedule the PoisonPill message to be sent to itself after 20 seconds during execution of the preStart hook e.g.

@Override
public void preStart() {
    getContext().system().scheduler().scheduleOnce(
            Duration.create(20, TimeUnit.SECONDS),
            getSelf(),
            PoisonPill.getInstance(),
            getContext().dispatcher(),
            ActorRef.noSender());
}

This keeps the behaviour within the definition of the UntypedActor, if that is desirable.

nickebbitt
  • 1,711
  • 1
  • 13
  • 13
0

There seems to be a feature in the akka library to solve this. Did not use it on my own, but sound's promissing: after[T]

Documentation about it here

If the Future is completed Successfully, the actor can send itselfe a poison pill.

self ! PoisonPill
andreas
  • 1,483
  • 1
  • 15
  • 36
0

There are below approaches:

  1. Send the Poison Pill with the Scheduler as described in above answers

  2. Send "STOP" with the Scheduler

    @Override
    public void preStart() {
        getContext().system().scheduler().scheduleOnce(
            Duration.create(20, TimeUnit.SECONDS),
                getSelf(),
                "STOP",
                getContext().dispatcher(),
                ActorRef.noSender());
    }
    

For the stop message simply do:

   if (message.toString().equals("STOP")) {
        getContext().stop(getSelf());
   } 

The difference between the above approaches is given in the following StackOverflow thread: Akka Kill vs. Stop vs. Poison Pill?

Note: You can't stop an actor in the middle of what it's doing.

azec-pdx
  • 4,790
  • 6
  • 56
  • 87