I have a java application, which embeds a jetty server to provide http services.
If I visit it via a url:
http://localhost:8080/doStop
it will stop the jetty server and terminates itself.
I have two options:
1. calls `myserver.stop()`, which `myserver` is an instance of `org.mortbay.jetty.Server`, and had set `.setStopAtShutdown(true)`
2. just invoke `System.exit()`
I wonder is it safe to just invoke System.exit()
to stop the whole application?
Sure, you can use System.exit(0)
.
But using the stop method from server would be much more cleaner and prevent you of unexpected errors (e.g. still open request).
System.exit()
is safe unless you have resources to release, half-baked data to persist, or want to ensure the server finishes servicing any open requests before shutting down.
Generally, System.exit()
is considered harmful, because you may forget that you (or not know that your coworker) had something effective tied to the shutdown event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With