Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a Scala / Akka actor is terminated

Tags:

scala

akka

actor

While executing code within one actor, I need to check if the actor is still alive. Would this be advisable or is there a better way?

if (self != akka.actor.DeadLetter) {
  // do something
}

Thanks!

EDIT---

Thanks for all your inputs. What happens is the following. I am using Play. As my actor starts, upon an incoming request, a timeout is scheduled.

Promise.timeout({
     Logger.info(s"$self, timeout expired")
     // do something
}, timeoutValue)

Sometimes the actor is stopped for other reasons before the timeout expires (for instance, the client disconnects). In that case, what I see then in the logs is

Actor[akka://application/deadLetters], timeout expired.

To my understanding, this means that the default deadLetters actor is executing that code. So my question really is: what is the best way to check if the Promise code is executed after the actor is terminated, and prevent it from going further if that is the case?

like image 363
ticofab Avatar asked Jul 25 '14 13:07

ticofab


People also ask

Can an Akka actor stop itself?

An actor can stop itself by returning. Behaviors. stopped as the next behavior. A child actor can be forced to stop after it finishes processing its current message by using the stop method of the ActorContext from the parent actor.

How do I terminate Akka actor?

In Akka, you can stop Actors by invoking the stop() method of either ActorContext or ActorSystem class. ActorContext is used to stop child actor and ActorSystem is used to stop top level Actor.

What happens when an actor fails in Akka?

The child actors are stopped to avoid resource leaks of creating new child actors each time the parent is restarted. It is possible to override this so that child actors are not influenced when the parent actor is restarted. The restarted parent instance will then have the same children as before the failure.

Are Akka actors single threaded?

Behind the scenes Akka will run sets of actors on sets of real threads, where typically many actors share one thread, and subsequent invocations of one actor may end up being processed on different threads.


3 Answers

You should familiarize yourself with the actor lifecycle: http://doc.akka.io/docs/akka/2.3.4/scala/actors.html#Actor_Lifecycle

From inside an actor you can implement the postStop() callback that will be called immediately before your actor is going to be stopped. If you want to monitor the lifecycle of the actor from another actor you should use DeathWatch: http://doc.akka.io/docs/akka/2.3.4/scala/actors.html#Lifecycle_Monitoring_aka_DeathWatch

like image 102
Endre Varga Avatar answered Oct 04 '22 00:10

Endre Varga


If your actor is dead, no code within the actor will be running.

You can check if specific actor is available, with actorSelection, and then send a message to whatever is returned as a result (if there will be nothing, no message will be sent).

ActorContext.actorSelection(<your-actor-name>) ! someMessage
like image 43
Ashalynd Avatar answered Oct 04 '22 00:10

Ashalynd


I think you can watch your actor and if you receive message Terminated you are sure that your actor is not running.

ActorContext.watch(self)
like image 36
chalimartines Avatar answered Oct 04 '22 00:10

chalimartines