Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use `System.exit()` to stop a java application which has an embed Jetty server?

Tags:

java

jetty

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?

like image 977
Freewind Avatar asked Oct 22 '22 04:10

Freewind


2 Answers

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).

like image 137
JulianG Avatar answered Oct 24 '22 05:10

JulianG


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.

like image 20
PaulProgrammer Avatar answered Oct 24 '22 05:10

PaulProgrammer