Akka: Several ways to kill an actor

Though it has been a few days since Halloween, today we will be able to satisfy our killer instinct.

17c8f21e452c8161a7f19b8fc3f7d1a5

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

tumblr_n6da8xJdLi1t4x1pbo1_500

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

jack-30-rock-heart-attack-study

Another option we have is to stop the actor. For this purpose, we use the stop method.

context.stop(seanBeanCharacter)

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

tumblr_mk2nondHhx1qasthro1_r1_500

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.

Last will

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 😛

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s