Complete Akka HTTP Application

I have a valid Akka HTTP application and I want to close it.

Pressing Ctrl+ Cin SBT does not work for me (my shell is currently Git Bash for Windows).

What is the recommended way to gracefully close an Akka application?

+6
source share
1 answer

Inspiring this thread , I added a route to my application that disconnects the application:

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

where systemis the ActorSystem application .

Given this route, I can close the application with

curl http://localhost:5000/shutdown

Edit:

. Henrik , , Enter SBT:

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 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 {}:{}!", host, 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())
+6

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


All Articles