gpt4 book ai didi

java - 多线程 Java HTTP 服务器

转载 作者:行者123 更新时间:2023-12-02 01:09:03 25 4
gpt4 key购买 nike

我使用 sun.net.httpServer 在我的应用程序中运行 HTTP 服务器(不要问我为什么)。

问题是它只在一个线程中处理我的请求,所以我的吞吐量是一场灾难。

我想如果我 setExecutor 到我的 httpServer 这个问题就会解决,但我开始在服务器端和客户端(SOAP-UI )。我测试了各种执行器,包括 Executors.newCachedThreadPool 、 Executors.newFixedThreadPool 、 Executors.newWorkStealingPool 、 Executors.newScheduledThreadPool没有任何运气。

正如我所说,当我 setExecutornull 时,我的代码工作正常,但在此配置中,我的代码按顺序处理请求。

我不知道该怎么办。这是我的代码:

public class HTTPListener {
private HttpServer httpServer;
private int port = 1253;

public void stop() {
if (httpServer != null) {
httpServer.stop(0);
httpServer = null;
}
}
public void startHTTPServer() {
try {
httpServer = HttpServer.create(new InetSocketAddress(1252), 100);
httpServer.setExecutor(null);
httpServer.createContext("/", new RequestHandler());
httpServer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

当我设置 Executors 时出现此错误

java.io.IOException: stream closed
at sun.net.httpserver.FixedLengthOutputStream.write(FixedLengthOutputStream.java:68)
at sun.net.httpserver.PlaceholderOutputStream.write(ExchangeImpl.java:444)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at java.io.FilterOutputStream.close(FilterOutputStream.java:158)
at com.RequestHandler.sendResponse(RequestHandler.java:61)
at com..RequestHandler.handle(RequestHandler.java:18)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79)
at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:82)
at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:675)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79)
at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:645)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

这是我的 RequestHandler 类:

public class RequestHandler implements HttpHandler {
private HttpExchange sender;

@Override
public void handle(HttpExchange httpExchange) throws IOException {
this.sender = httpExchange;
try {
String request = readRequestBody();
sendResponse(200, "Hello World !");
} catch (Exception e) {
e.printStackTrace();
} finally {
this.sender.close();
}
}

private String readRequestBody() throws Exception {
try (InputStream isr = sender.getRequestBody()) {
byte[] buffer = new byte[isr.available()];
isr.read(buffer);
return Arrays.toString(buffer);
} catch (IOException ex) {
throw new Exception(ex.getMessage());
}
}

private void sendResponse(int httpResponseCode, String response) throws IOException {
OutputStream outputStream = null;
try {
sender.getResponseHeaders().set("Content-Type", "text/html; charset=UTF-8");
sender.sendResponseHeaders(httpResponseCode, response.length());
outputStream = sender.getResponseBody();
outputStream.write(response.getBytes());
} finally {
if (outputStream != null)
outputStream.close();
}

}
}

我也在客户端收到此错误代码

org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 13; received: 0
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:180)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:137)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.Reader.read(Reader.java:140)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:247)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:291)
at Business.HTTPHandler.Client.send(Client.java:68)
at main.Main.lambda$main$1(Main.java:86)
at java.lang.Thread.run(Thread.java:745)

org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at Business.HTTPHandler.Client.send(Client.java:67)
at main.Main.lambda$main$1(Main.java:86)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.http.ProtocolException: Invalid header: *
at org.apache.http.impl.io.AbstractMessageParser.parseHeaders(AbstractMessageParser.java:232)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:268)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:165)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:272)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
... 5 more

Caused by: org.apache.http.ProtocolException: Invalid header: 13
at org.apache.http.impl.io.AbstractMessageParser.parseHeaders(AbstractMessageParser.java:232)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:268)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:165)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:272)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
... 5 more

最佳答案

您的 RequestHandler 类不是线程安全的,这就是为什么它在您运行单线程时有效,而在使用执行程序时失败。

请勿不要HttpExchange 对象存储在 sender 变量中。当所有线程都覆盖同一个变量时,它会导致竞争条件,并且可能看到处于错误状态的错误对象(例如,流已经关闭的对象)。

您可以将其作为参数传递给您的方法。您确实不需要额外的变量。

关于java - 多线程 Java HTTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59629716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com