Correctly terminate the acc actors in scala

I wrote a code sample that launches an actor, kills it, and finishes executing.

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 he refuses to stop until I kill the process with Ctrl-C

What awaits the application? How can I stop it properly?

+5
source share
1 answer

Perhaps calling ActorSystem.shutdown () might do the trick.

According to akka docs :

abstract def shutdown(): Unit

. -, , , , ( ) (. ActorSystem.registerOnTermination).

+8

All Articles