13.7. Shutting Down the Akka Actor System

Problem

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

Solution

Call the 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
}

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.

Get Scala Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.