gpt4 book ai didi

Java HttpGet,服务中打开文件过多错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:57:49 24 4
gpt4 key购买 nike

我正在使用 RESTFul HttpGet 服务

导入org.apache.http.client.methods.HttpGet;

我创建了一个函数来查询另一台计算机上的服务。该调用工作正常,但是我在服务上遇到“打开文件过多”的情况。我这边只是返回一个 500 错误,我发现了这个错误。

我与供应商进行了交谈,他们非常坚定地认为应该以持久的方式进行 RESTful 调用,并且我只是没有释放某些东西,并继续认为问题出在我这边。

我编写了一个压力函数(见下文)来帮助隔离问题。据我所知,我正在释放一切。我对 Java 还很陌生,所以我只是没有看到一些不清楚的东西。

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public void doStressTest()
{
String strUri = "http://<ip address>:<port>/task?vara=dataa&varb=datab";
HttpGet oRestyGet = new HttpGet();
oRestyGet.addHeader("Accept", "application/xml");

for (int iLoop = 0; iLoop < 1000; iLoop++)
{
CloseableHttpClient httpclient = HttpClients.createDefault();
try
{
oRestyGet.setURI(new URI(strUri));
try
{
CloseableHttpResponse response2 = httpclient.execute(oRestyGet);
try
{
String strResponseHeader = response2.getStatusLine().toString();
if (false == "HTTP/1.1 200 OK".equalsIgnoreCase(strResponseHeader))
return;

continue;
}
finally
{
response2.close();
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
httpclient.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

try
{
oRestyGet.releaseConnection();
}
catch (Exception e)
{
e.printStackTrace();
}
}

更新:我想我应该添加供应商所说的文字,以防万一它可能有所帮助。我应该补充一点,在下面的压力测试中,我打开了一个 HTTP GET 请求对象,该对象对于我的“ session ”来说是持久的并用于整个请求。

This means that the system is running out of FDs for the user running the process. This happens when there are too many open sockets or FDs. In general with HTTP it is highly recommended the HTTP requests are sent in a persistent way, that is you have one HTTP connection for the entire session and not opening multiple HTTP connections then closing them for each request. More often then not you end up with lingering connections.

我正在关闭“可关闭的”CloseableHttpClient,而且我也不认为我需要只有一个。我应该补充一点,“我”并没有耗尽任何东西,而是服务。供应商似乎说该服务和操作系统都是完美的。还可以采取哪些措施来隔离问题?

更新2:(我将日志文件从服务发送给供应商,他们说了以下内容。陷入僵局?)

It looks like the problem is that there are too many connections left open by your interface. I included the log file from a session by Firefox. You can see how I can make multiple HTTP requests over the same connection using a browser.

There is only one line where the initial connection is made

HTTP connection from [127.0.0.1]...ALLOWED

the subsequent GET requests are from the same connection.

...your logfile shows: HTTP connection from [192.168.20.123]...ALLOWED

for each GET request you make, and those connections are not closed.

更新3:我可以访问该服务生成的日志,但无法访问源。 Java 将连接和 GET 请求作为一个包发出以响应以下行:

CloseableHttpResponse response2 = httpclient.execute(oRestyGet);

我正在发出 httpclient.close(),它不会生成任何日志条目,因此我怀疑该服务根本不对此做出响应。

由于我不熟悉另一端的机制,也许该服务只是响应事件,问题是 CENTOS 没有正确处理 close() 调用。不管怎样,用这个方法,问题就不是我的了。证明这一点是另一个故事。另一种选择是某种可以正确释放资源的其他解决方案。

最佳答案

如果您收到 HTTP 500 内部服务器错误,则这与您的客户端代码或管理 HTTP 连接的方式无关。问题完全出在供应商方面。

关于Java HttpGet,服务中打开文件过多错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22492089/

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