-
From within my program, how can I shut down the server that is running? There must be a thread that is still alive when my program is otherwise finished and I need to terminate it so that my program can end. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
TL;DR: if you use cask with In general, you need to access the Undertow server that underpins cask's HTTP handling and call the As per the Undertow docs
Now, how you access the server is dependent on how you use cask. In case you started cask by extending What you could do instead is reimplement what the main method does in a more fine grained way, specific to your app, and call that instead. For example, here is an extract of how I commonly use cask when I need more sophisticated setup and teardown code: class HttpServer(
override val host: String,
override val port: Int,
override val debugMode: Boolean,
val routes: Iterable[cask.Routes]
) extends cask.MainRoutes:
override def allRoutes = routes.toSeq
val server =
io.undertow.Undertow.builder
.addHttpListener(port, host)
.setHandler(defaultHandler)
.build()
def start() = server.start()
def stop() = server.stop()
end HttpServer
def main() =
val server = HttpServer(....)
println("starting http server")
server.start()
// wait until exit condition
// ...
println("stopping http server")
server.stop() |
Beta Was this translation helpful? Give feedback.
-
Thank you! Instead of calling main from my extension to MainRoutes, I now just make my own server and ask it to start. Then I stop it when it's time to close. It's easy enough to implement once one knows that to do. |
Beta Was this translation helpful? Give feedback.
TL;DR: if you use cask with
object App extend cask.MainRoutes {...}
you can't do itIn general, you need to access the Undertow server that underpins cask's HTTP handling and call the
stop()
method.As per the Undertow docs
Now, how you access the server is dependent on how you use cask.
In case you started cask by extending
cask.MainRoutes
and callingmain()
(as is done in the most simple cases e.g.object App extends cask.MainRoutes
), then there is no way to stop the server programmatically. This is be…