How to shut down the Akka Actor system (ActorSystem)

This is an excerpt from the Scala Cookbook. This is a very short recipe, Recipe 13.7, “How to shut down the Akka Actor system.”

Problem

You want to shut down the Akka actor system in a Scala application, typically because your application is finished, and you want to shut it down gracefully.

Solution

Call the Akka shutdown method on your ActorSystem instance:

object Main extends App {

    // create the ActorSystem
    val system = ActorSystem("HelloSystem")

    // put your actors to work here ...
    // shut down the ActorSystem when the work is finished
    system.shutdown

}

Update: I haven’t updated this example yet, but the shutdown method was deprecated and then removed, and has been replaced with terminate. See the ActorSystem documentation for more information.

Discussion

When you’re finished using actors in your application, you should call the shutdown method on your ActorSystem instance. As shown in the examples in this chapter, if you comment out the system.shutdown call, your application will continue to run indefinitely. In my SARAH application, which is a Swing application, I call actorSystem.shutdown when the user shuts down the GUI.

If you want to stop your actors before shutting down the actor system, such as to let them complete their current work, see the examples in Recipe 13.6, “Stopping Actors”. 13.8. Monitoring the Death of an Actor with watch