Home » Akka Stopping Actors

Akka Stopping Actors

by Online Tutorials Library

Akka Stopping Actors

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.

The actual termination of the actor is performed asynchronously.

There are some other methods available in Akka, which are used to stop Actor. Some of which are PoisonPill, terminate() and gracefulStop() are used to stop Actor.


Akka Stopping Top Level Actor Example

You can simply call stop() method to stop top level Actor. The following example describes the uses of stop() method.

Output:

Message received: Hello  Actor stoped  

Akka Stopping Child Actor Example

Output:

Message received by RootActor: Hello  Message received by ChildActor: Hello child Actor  Child Actor stoped  

Akka Stopping ActorSystem Example

You can stop actor system by calling it’s terminate method. This method will stop the guardian actor, which in turn will recursively stop all its child actors.

Output:

Message received by RootActor: Hello  Message received by ChildActor: Hello child Actor  Child Actor stoped  Top Level Actor stoped  
Next TopicHTML Aside tag

You may also like