Apache Commons HTTPClient 3.x 保持连接打开

发布于 2024-07-09 18:47:07 字数 407 浏览 6 评论 0原文

我正在使用 HttpClient 对远程 servlet 执行 PostMethod,由于某种原因,我的很多连接都处于打开状态并占用了我服务器的所有连接。
以下是有关架构的更多信息
GWT 客户端调用 GWT 服务
GWT 服务实例化 HttpClient,创建 PostMethod 并让客户端执行该方法
然后它通过调用 method.getResponseBodyAsStream() 获取输入流并将其写入字节数组
然后它关闭输入流并刷新字节数组输出流,再执行几行代码,然后调用 method.releaseConnection()

一定有一些明显的我忽略的东西导致了这种情况。 如果我在浏览器中对同一服务执行 GET,连接会立即关闭,但 HTTPClient 的某些问题会导致它们保持打开状态。

I'm using HttpClient to execute a PostMethod against a remote servlet and for some reason a lot of my connections are hanging open and hogging up all of my server's connections.
Here's more info about the architecture
GWT client calls into a GWT Service
GWT service instantiates a HttpClient, creates a PostMethod and has the client execute the method
it then gets the input stream by calling method.getResponseBodyAsStream() and writes it out to a byte array
it then closes the input stream and flushes the byte array output stream, does a few more lines of code and then calls method.releaseConnection()

There has to be something obvious I'm overlooking that's causing this. If I perform a GET in a browser to the same service, the connections close immediately but something about HTTPClient is causing them to hang open.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2024-07-16 18:47:07

您需要调用 HttpMethodBase#releaseConnection()。 如果你返回一个InputStream供以后使用,一个简单的方法是用一个匿名的FilterInputStream覆盖close()来包装它:

final HttpMethodBase method = ...;
return new FilterInputStream(method.getResponseBodyAsStream())
{
  public void close() throws IOException
  {
    try {
      super.close();
    } finally {
      method.releaseConnection();
    }
  }
};

You need to call HttpMethodBase#releaseConnection(). If you return a InputStream to be used later, a simple way is to wrap it by a anonymous FilterInputStream overwriting close():

final HttpMethodBase method = ...;
return new FilterInputStream(method.getResponseBodyAsStream())
{
  public void close() throws IOException
  {
    try {
      super.close();
    } finally {
      method.releaseConnection();
    }
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文