我正在使用 com.sun.net.httpserver.HttpServer 创建一个用于测试服务器代码位的小容器,并且无法让它使用多个线程来处理请求。
我调用 java.util.concurrent.Executors.newFixedThreadPool(20) 来创建一个具有 20 个线程的 java.util.concurrent.ThreadPoolExecutor。然后,我在 HttpServer 上设置了这个 Executor。使用 Jmeter,我触发了 20 个客户端线程来发送一个请求,该请求被路由到服务器中唯一的 HttpHandler 实现。该处理程序执行 System.out.println(this) 并且我看到了以下输出:
Started TestServer at port 8800
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
http.TestHandler@30eb9dfa
我想我会看到这里使用了 20 个(或近 20 个)不同的线程。这是代码。
package http;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class TestServer implements Runnable {
private final static int PORT = Integer.getInteger("test.port", 8800);
private static TestServer serverInstance;
private HttpServer httpServer;
private ExecutorService executor;
@Override
public void run() {
try {
executor = Executors.newFixedThreadPool(20);
httpServer = HttpServer.create(new InetSocketAddress(PORT), 0);
httpServer.createContext("/test", new TestHandler());
httpServer.setExecutor(executor);
httpServer.start();
System.out.println("Started TestServer at port " + PORT);
// Wait here until notified of shutdown.
synchronized (this) {
try {
this.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
static void shutdown() {
try {
System.out.println("Shutting down TestServer.");
serverInstance.httpServer.stop(0);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (serverInstance) {
serverInstance.notifyAll();
}
}
public static void main(String[] args) throws Exception {
serverInstance = new TestServer();
Thread serverThread = new Thread(serverInstance);
serverThread.start();
Runtime.getRuntime().addShutdownHook(new OnShutdown());
try {
serverThread.join();
} catch (Exception e) { }
}
}
/* Responds to the /test URI. */
class TestHandler implements HttpHandler {
boolean debug = Boolean.getBoolean("test.debug");
public void handle(HttpExchange exchange) throws IOException {
System.out.println(this); // ALWAYS SAME THREAD!
String response = "RESPONSE AT " + System.currentTimeMillis();
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.flush();
os.close();
}
}
/* Responds to a JVM shutdown by stopping the server. */
class OnShutdown extends Thread {
public void run() {
TestServer.shutdown();
}
}
我希望 HttpServer 并行创建多个 TestHandler 来为多个同时请求提供服务。我在这里想念什么?
(顺便说一句,这与Can I make a Java HttpServer threaded/process requests in parallel?非常相似,尽管答案是使用我已经在做的 Executor。谢谢。)