加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 关闭Akka HTTP应用程序

发布时间:2020-12-16 08:54:59 所属栏目:安全 来源:网络整理
导读:我有一个正在运行的Akka HTTP应用程序,我想关闭它. 在我的SBT does not work中按Ctrl C(我的shell目前是Windows的Git Bash). 什么是优雅关闭Akka应用程序的推荐方法? 解决方法 从 this thread获取灵感,我添加了一个关闭应用程序的应用程序路径: def shutdo
我有一个正在运行的Akka HTTP应用程序,我想关闭它.

在我的SBT does not work中按Ctrl C(我的shell目前是Windows的Git Bash).

什么是优雅关闭Akka应用程序的推荐方法?

解决方法

从 this thread获取灵感,我添加了一个关闭应用程序的应用程序路径:

def shutdownRoute: Route = path("shutdown") {
  Http().shutdownAllConnectionPools() andThen { case _ => system.terminate() }
  complete("Shutting down app")
}

系统是应用程序的ActorSystem.

鉴于这条路线,我现在可以关闭我的应用程序了

curl http://localhost:5000/shutdown

编辑:

能够远程关闭服务器对于生产代码来说不是一个好主意.在评论中,Henrik指出了一种通过在SBT控制台中按Enter键来关闭服务器的不同方法:

StdIn.readLine()
// Unbind from the port and shut down when done
bindingFuture
  .flatMap(_.unbind())
  .onComplete(_ => system.terminate())

对于上下文,我将上面的代码放在服务器初始化的末尾:

// Gets the host and a port from the configuration
val host = system.settings.config.getString("http.host")
val port = system.settings.config.getInt("http.port")

implicit val materializer = ActorMaterializer()

// bindAndHandle requires an implicit ExecutionContext
implicit val ec = system.dispatcher

import akka.http.scaladsl.server.Directives._
val route = path("hi") {
  complete("How's it going?")
}

// Starts the HTTP server
val bindingFuture: Future[ServerBinding] = Http().bindAndHandle(route,host,port)

val log = Logging(system.eventStream,"my-application")

bindingFuture.onComplete {
  case Success(serverBinding) =>
    log.info(s"Server bound to ${serverBinding.localAddress}")

  case Failure(ex) =>
    log.error(ex,"Failed to bind to {}:{}!",port)
    system.terminate()
}

log.info("Press enter key to stop...")
// Let the application run until we press the enter key
StdIn.readLine()
// Unbind from the port and shut down when done
bindingFuture
  .flatMap(_.unbind())
  .onComplete(_ => system.terminate())

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读