Though it has been a few days since Halloween, today we will be able to satisfy our killer instinct.
In this post we go back to playing with Akka, the toolkit designed to create distributed applications using actors. Actors work very well and are very helpful but the time comes when you have to kill them. How can we do it? Can we make it look like an accident? Are they highly resistant to cyanide? Today we hope to answer some of these questions.
First way: Poisoning
The simplest thing you can do in order to kill an actor is send a message of PoisonPill type. As soon as the message is handled, the actor dies.
seanBeanCharacter ! PoisonPill
This mechanism does not only work for murder, but also for suicide. An actor can send a PoisonPill message to himself.
self ! PoisonPill
Second way: Cardiac arrest
Another option we have is to stop the actor. For this purpose, we use the stop method.
The fundamental difference between using a PoisonPill and calling the stop method, is that the method stop performs an immediate stop of the actor. In the case of PoisonPill , because it is just another message in the mailbox, it is necessary to wait for it to be handled.
Third way: Cause an internal bleeding
Finally, we can make an actor throw an exception, by sending him a message of type Kill.
seanBeanCharacter ! Kill
This message, when handled by the actor, forces an exception of type ActorKilledException to be thrown. We can use this kind of killing when we want to handle this type of emergency in the supervision policy. You are allowed to decide what action has to be performed by a supervisor when a player throws an exception of this kind.
All actors, no matter how bad the film is, are entitled to a last will. This last will is materialized by a series of actions, such as stopping the actors it has under its supervision. It is also possible to add customized functionality by overwriting the poststop method.
And that’s all for now. Just remember one thing. May you use any of these techniques, you don’t know the Scalera guys at all 😛