score:1

one solution is to add an actor to your actorsystem that listens for a particular signal and calls shutdown:

import akka.actor.{actor, props}

object shutdownmessage

object killswitchactor {
  def props : props = props[killswitchactor]
}

class killswitchactor extends actor {
  def receive = {
    case shutdownmessage => context.system.shutdown()
    case _ => {}
  }
}//end class killswitchactor

then you simply setup your killswitchactor:

import akka.actor.actorsystem

val actorsystem : actorsystem = actorsystem("testkillswitch")

val killref = actorsystem actorof killswitchactor.props

//"say hello to my little friend!" - tony montana 
if(someterminatingcondition) { killref ! shutdownmessage }

score:4

you could add to your main method

runtime.getruntime.addshutdownhook(new thread() {
  override def run() {
    system.shutdown()
    system.awaittermination()
  }
})

your app will wait until actor system will be shutt down and all poststop callbacks in your actors will be executed.

score:31

you can add shutdown hook:

// import scala.concurrent.duration._
//shutdown hook
scala.sys.addshutdownhook {
  logger.info("terminating...")
  actorsystem.terminate()
  await.result(actorsystem.whenterminated, 30 seconds)
  logger.info("terminated... bye")
}

Related Query

More Query from same tag