私は、Sunの軽量HttpServerを使用して、オンラインで見つけたチュートリアルに従って簡単なHttpServerを構築しました。
基本的に、メイン関数は次のようになります。
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
//Create the context for the server.
server.createContext("/", new BaseHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
そして、私はHttpHandlerインターフェースのメソッドを実装して、Http要求を処理し、応答を返しました。
static class BaseHandler implements HttpHandler {
//Handler method
public void handle(HttpExchange t) throws IOException {
//Implementation of http request processing
//Read the request, get the parameters and print them
//in the console, then build a response and send it back.
}
}
また、スレッドを介して複数のリクエストを送信するクライアントを作成しました。各スレッドは、次の要求をサーバーに送信します。
http:// localhost:8000 / [context]?int = "+ threadID
クライアントが実行されるたびに、リクエストはサーバーに異なる順序で到着するように見えますが、それらはシリアルに処理されます。
私が達成したいのは、可能であれば、リクエストを並行して処理することです。
たとえば、各ハンドラーを別々のスレッドで実行することは可能ですか。その場合、それは良いことです。
または、Sunの軽量サーバーを完全に使用して、最初から何かを構築することに集中する必要がありますか?
助けてくれてありがとう。