How to stop the graceful acting system for the akka-http server that needs to be deployed.

I just created my first recreation server with akka-http. The problem is that I donโ€™t know how to deploy the server so that I can gracefully disconnect the actors system.

For example, I found something here: https://stackoverflow.com/a/212940/2/ where you could use the Akka microkernel, but it is deprecated. I tried to use sbt-native-package, but I don't know how to gracefully close the actors system.

Thanks!

+4
source share
3 answers

You can add hookdown:

// import scala.concurrent.duration._
//shutdown Hook
scala.sys.addShutdownHook {
  logger.info("Terminating...")
  actorSystem.terminate()
  Await.result(actorSystem.whenTerminated, 30 seconds)
  logger.info("Terminated... Bye")
}
+23
source

Runtime.getRuntime.addShutdownHook(new Thread() {
  override def run() {
    system.shutdown()
    system.awaitTermination()
  }
})

, , postStop .

+2

ActorSystem, :

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

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 }
+1

Source: https://habr.com/ru/post/1614165/


All Articles