Я хочу, чтобы он мог работать на Java SE 8 без необходимости использования Java EE. И я хочу, чтобы он поддерживал «мягкую остановку», то есть, когда я вызываю этот метод «мягкой остановки», HTTP-сервер перестает принимать новые HTTP-запросы, но продолжает обрабатывать существующие HTTP-запросы и ждет неопределенное время, пока все существующие HTTP-запросы не будут завершены. были обработаны. Для этого ожидания не должно быть максимального времени, поскольку мне может потребоваться подождать, пока все существующие HTTP-запросы будут обработаны. Если я не хочу ждать, я могу просто принудительно остановить процесс программы.
Эта библиотека не нуждается в поддержке HTTPS, поскольку я планирую развернуть этот сервер в доверенной внутренней сети. .
Я хочу использовать эту библиотеку следующим образом (в этом примере воображаемый пакет «somehttpserver» предоставляется библиотекой HTTP-сервера):
Код: Выделить всё
import somehttpserver.HttpServer;
import somehttpserver.HttpMethod;
import java.util.Scanner;
public class MyHttpServerProgram {
public static void main(String[] args) {
HttpServer theHttpServer;
boolean isSuccess;
Scanner consoleScanner;
String consoleInput;
theHttpServer = new HttpServer();
theHttpServer.setListeningPort(9000);
//add HTTP "endpoints", which are combinations of a HTTP method and a URL
//the handlers are objects implementing the interface somehttpserver.HttpEndpointHandler
theHttpServer.addEndpoint(HttpMethod.POST, "/postSomething", new MyHandler1());
theHttpServer.addEndpoint(HttpMethod.GET, "/getSomething", new MyHandler2());
//add more HTTP endpoints...
//start the HTTP server
isSuccess = true;
try {
theHttpServer.start();
} catch (Exception e) {
isSuccess = false;
}
if (isSuccess == false) {
System.out.print("The HTTP server has failed to start.");
} else {
//Now the HTTP server has started successfully.
//The HTTP server runs concurrently, that is, runs in its own thread, not in this thread.
System.out.print("The HTTP server has started successfully.");
System.out.print("\n\n");
System.out.print("When you want to stop this HTTP server, enter STOP to stop it.\n");
consoleScanner = new Scanner(System.in);
consoleInput = consoleScanner.nextLine();
while ((consoleInput.equals("STOP")) == false) {
consoleInput = consoleScanner.nextLine();
}
//Now, we stop the HTTP server
//Stop the HTTP server in a graceful way, that is:
//stop accepting new HTTP requests, but continue processing existing HTTP requests, and wait indefinitely until all existing HTTP requests have been processed
theHttpServer.stop();
System.out.print("\n");
System.out.print("The HTTP server has stopped successfully.");
}
System.out.print("\n\n");
System.out.print("The HTTP server program will exit. Enter OK to exit.\n");
consoleInput = consoleScanner.nextLine();
while ((consoleInput.equals("OK")) == false) {
consoleInput = consoleScanner.nextLine();
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/788 ... ttp-server