gpt4 book ai didi

java - 通过 http 集成两个 Java webapps 的 IO 错误

转载 作者:行者123 更新时间:2023-11-28 22:55:32 28 4
gpt4 key购买 nike

我有两个 java webapps,每个都部署在一个单独的 tomcat 服务器(和一个单独的机器)上,其中一个向另一个(+1000)发出大量连续的(但顺序的,不是并行的)http 请求.它大部分时间都有效,但是当“客户端服务器”(生成请求的服务器)上的负载很高时,我会遇到无法追踪的随机 IO 错误。

这是客户端的简化版本

public class BPXProxy {
private static HttpClient client;
public static void init(){
client = new HttpClient();
}
public static InvoiceCreationInfo registerInvoice(InvoiceData invoice) throws SQLException, HttpException, IOException {
JSONObject invoiceJSON = new JSONObject();
invoiceJSON.put("invoicer", invoice.getInvoicerIdentification().getDocument());
invoiceJSON.put("buyer", invoice.getBuyerIdentification().getDocument());
String serviceURL = ConfigurationManager.getBPXServicesPath() + "Invoice?operation=registerExternalInvoice&branchCode="+ branchCode;
PostMethod updatePage = new PostMethod(serviceURL);
updatePage.addParameter("invoice", invoiceJSON.toJSONString());
updatePage.addParameter("invoiceSubject", invoice.getInvoiceSubject());
client.executeMethod(updatePage); // java.io.IOException: Stream closed, java.net.SocketException: Socket closed
String response = updatePage.getResponseBodyAsString(); // java.io.IOException: chunked stream ended unexpectedly, java.io.IOException: CRLF expected at end of chunk: -1/-1
updatePage.releaseConnection();
JSONObject jsonResponse = JSONSimpleHelper.parseJSON(response);
return jsonResponse;
}
}

这些异常(exception)的例子:

java.io.IOException: chunked stream ended unexpectedly
at org.apache.commons.httpclient.ChunkedInputStream.getChunkSizeFromInputStream(ChunkedInputStream.java:251)
at org.apache.commons.httpclient.ChunkedInputStream.nextChunk(ChunkedInputStream.java:220)
at org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:175)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:107)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:126)
at org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:684)
at org.apache.commons.httpclient.HttpMethodBase.getResponseBodyAsString(HttpMethodBase.java:735)
at net.slashware.mobilia.slashbillingClient.proxy.BPXProxy.registerInvoice(BPXProxy.java:208)

java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:206)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:77)
at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:105)
at org.apache.commons.httpclient.HttpParser.parseHeaders(HttpParser.java:165)
at org.apache.commons.httpclient.HttpMethodBase.readResponseHeaders(HttpMethodBase.java:1790)
at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1592)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:397)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
at net.slashware.mobilia.slashbillingClient.proxy.BPXProxy.registerInvoice(BPXProxy.java:207)

java.net.SocketException: Socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:146)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:77)
at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:105)
at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1115)
at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1832)
at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1590)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:397)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
at net.slashware.mobilia.slashbillingClient.proxy.BPXProxy.getAccountStatusInvoices(BPXProxy.java:2905)

java.io.IOException: CRLF expected at end of chunk: -1/-1
at org.apache.commons.httpclient.ChunkedInputStream.readCRLF(ChunkedInputStream.java:206)
at org.apache.commons.httpclient.ChunkedInputStream.nextChunk(ChunkedInputStream.java:218)
at org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:175)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:107)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:126)
at org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:684)
at org.apache.commons.httpclient.HttpMethodBase.getResponseBodyAsString(HttpMethodBase.java:735)
at net.slashware.mobilia.slashbillingClient.proxy.BPXProxy.getPersonOpenReceivables(BPXProxy.java:3132)

org.apache.commons.httpclient.ProtocolException: The server ec2-54-196-218-148.compute-1.amazonaws.com failed to respond with a valid HTTP response
at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1846)
at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1590)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:397)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
at net.slashware.mobilia.slashbillingClient.proxy.BPXProxy.registerInvoice(BPXProxy.java:207)

知道为什么会发生这些吗?看起来修复应该针对“客户端服务器”还是其他服务器?

我在 aws 上执行此操作,每个服务器都在一个单独的 ec2 节点上,每个请求大约需要 4 秒才能完成。我正在使用 apache commons httpclient 进行调用;我已经监控了服务器,内存消耗没有问题,而且我没有看到每台服务器上的网络输入和网络输出峰值。

最佳答案

我推荐两件事。

首先,使用实例字段和方法代替静态字段和方法。您可能遇到竞争条件,因为响应没有按您预期的那样进入,并且您在 registerInvoice 的一次调用中关闭流,然后尝试在另一次调用中读取它们。所以,你的类(class)看起来像

public class BPXProxy {
private HttpClient client;
public void init(){
client = new HttpClient();
}
public InvoiceCreationInfo registerInvoice(InvoiceData invoice) throws SQLException, HttpException, IOException {
JSONObject invoiceJSON = new JSONObject();
invoiceJSON.put("invoicer", invoice.getInvoicerIdentification().getDocument());
invoiceJSON.put("buyer", invoice.getBuyerIdentification().getDocument());
String serviceURL = ConfigurationManager.getBPXServicesPath() + "Invoice?operation=registerExternalInvoice&branchCode="+ branchCode;
PostMethod updatePage = new PostMethod(serviceURL);
updatePage.addParameter("invoice", invoiceJSON.toJSONString());
updatePage.addParameter("invoiceSubject", invoice.getInvoiceSubject());
client.executeMethod(updatePage); // java.io.IOException: Stream closed, java.net.SocketException: Socket closed
String response = updatePage.getResponseBodyAsString(); // java.io.IOException: chunked stream ended unexpectedly, java.io.IOException: CRLF expected at end of chunk: -1/-1
updatePage.releaseConnection();
JSONObject jsonResponse = JSONSimpleHelper.parseJSON(response);
return jsonResponse;
}
}

你会像这样调用它

BPXProxy instance = new BPXProxy();
instance.init();
InvoiceCreationInfo info = instance.registerInvoice(invoice);

其次,看起来您使用的 Apache HttpClient 3.x 已经严重过时了。升级到 4.3 版(或更高版本,如果存在)。您必须更改代码,但基于 quick start ,它不应该太激烈:

public class BPXProxy {

private CloseableHttpClient client = HttpClients.createDefault();

public InvoiceCreationInfo registerInvoice(InvoiceData invoice) throws SQLException, HttpException, IOException {
JSONObject invoiceJSON = new JSONObject();
invoiceJSON.put("invoicer", invoice.getInvoicerIdentification().getDocument());
invoiceJSON.put("buyer", invoice.getBuyerIdentification().getDocument());
String serviceURL = ConfigurationManager.getBPXServicesPath() + "Invoice?operation=registerExternalInvoice&branchCode="+ branchCode;
HttpPost httpPost = new HttpPost(serviceURL);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add("invoice", invoiceJSON.toJSONString());
nvps.add("invoiceSubject", invoice.getInvoiceSubject());
client.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse rsp = client.execute(httpPost);
try {
HttpEntity entity = rsp.getEntity();
String rspJson = EntityUtils.toString(entity);
EntityUtils.consume(entity);
return JSONSimpleHelper.parseJSON(rspJson);
} finally {
rsp.close();
}
}
}

(警告:我没有尝试编译或测试这段代码。)

关于java - 通过 http 集成两个 Java webapps 的 IO 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28151518/

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