correctly terminate akka actors in scala

I've write sample code that starts an actor, kills it and finishes execution.

object PureAkka {
  def main(argv : Array[String]) = {
    val actorSystem : ActorSystem = ActorSystem("main")
    val actor : ActorRef = actorSystem.actorOf(Props( new Actor {
      override def receive = {
        case x => println(x)
      }
      override def preStart() = println("prestart")
      override def postStop() = println("poststop")
    } ) )
    Thread.sleep(15000)
    actor ! PoisonPill
  }
}

This code prints:

[info] prestart
[info] poststop

But it refuses to stop until I kill the process with Ctrl-C

What does application wait for? How can I stop it in a proper way?

Answers


Perhaps making a call to ActorSystem.shutdown() would do the trick.

According to the akka docs:

abstract def shutdown(): Unit

Stop this actor system. This will stop the guardian actor, which in turn will recursively stop all its child actors, then the system guardian (below which the logging actors reside) and the execute all registered termination handlers (see ActorSystem.registerOnTermination).


Need Your Help

How smart is Java when detecting an uninitialized local variable?

java initialization

I know Java forces you to initialize every local variable before you use it. There are some cases where it gets tricky, though. Consider this:

Comparison of Entity Framework compatible providers for Oracle?

oracle entity-framework entity-framework-4 comparison dataprovider

I have read similar questions and their answers, however, it seems none deal with this exact question except for one, which is out of date (from 2009 - pre EF4).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.