Skip to content
Snippets Groups Projects
Commit b985b5ac authored by Lange, Dr. Herbert's avatar Lange, Dr. Herbert
Browse files

properly handle shutdown of the server

parent a225de88
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,6 @@ import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
......@@ -58,23 +57,31 @@ public class Main {
/**
* Main method.
* @param args No command line arguments expected
* @throws IOException Exception on server problems
*/
public static void main(String[] args) throws IOException {
public static void main(String[] args) {
Main main = new Main();
main.logger.info("Starting server");
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with endpoints available at "
+ "%s%nHit Ctrl-C to stop it...", BASE_URI));
System.in.read();
System.out.printf("Jersey app started with endpoints available at "
+ "%s%nHit Ctrl-C to stop it...%n", BASE_URI);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
main.logger.info("Shutting down server");
server.shutdown();
try {
Main.cleanupThreads();
} catch (InterruptedException e) {
e.printStackTrace();
}
}));
main.mainWait();
}
synchronized void mainWait() {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment