Java:HTTP连接创建的等待连接线程持续很长时间 [英] Java: Waiting connection threads created by HTTP connection are alive for very long duration

查看:935
本文介绍了Java:HTTP连接创建的等待连接线程持续很长时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器端代码,用于检查SOAP服务是否已启动。代码如下:

I have a server side code which checks if SOAP service is up. Code looks like:

String response = "";

while (response.length() == 0) {
    try {
        final URL url = new URL("DummySoapServiceURL");
        final HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = null;
        try {
            httpConnection.setRequestMethod("GET");

            inputStream = httpConnection.getInputStream();
            final byte[] buffer = new byte[BUFFER_SIZE];
            while (inputStream.read(buffer, 0, BUFFER_SIZE) != -1) {
                response = new String(buffer);
            }
        } finally {
            IOUtils.closeQuietly(inputStream);
            httpConnection.disconnect();
        }
    } catch (MalformedURLException e) {
        // error handling
    } catch (IOException e) {
        // error handling
    }
}

现在的问题是,每次检查都会创建3-4个连接线程。即使SOAP服务检查完成,这些线程仍然存在。这些线程的线程转储快照如下所示:

Now the problem is that, for every check around 3-4 connection threads are created. And these threads are alive even if SOAP service check is completed. Snapshot of thread dump, for these threads looks like:

"http-host/ip:port-11" prio=10 tid=0x00000000064f0000 nid=0x32cc waiting on condition [0x00002b54bc604000]
   java.lang.Thread.State: WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000000d5093c78> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
    at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1068)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.net.NioEndpoint$DefaultThreadFactory$1$1.run(NioEndpoint.java:1249)
    at java.lang.Thread.run(Thread.java:744)
   Locked ownable synchronizers:
    - None

现在我不确定为什么这些连接线程在等待/停车以及如何关闭它们。在我的代码中,打开的流被关闭,连接断开连接使用disconnect()。

Now I am not sure why these connections threads are waiting/parking and how to close them. In my code opened streams are closed and connections is disconnected using disconnect().

我还尝试设置以下HTTP属性:

I also tried to set following HTTP property:

httpConnection.addRequestProperty("Connection", "close");

但它没有帮助。我怀疑在某个时候JAVA可能正在关闭这些线程。但我不知道,何时以及如何? JDK版本为jdk1.7.0_51_x64。请告诉我,如何阻止这些连接线程编号的建立?

But it didn't help. I doubt that at some time JAVA might be closing these threads. But I don't know, when and how? JDK version is jdk1.7.0_51_x64. Please let me know, how can I stop these connection thread numbers from building up?

推荐答案

迁移整个实现以使用apache HTTP客户端因为它有特殊的API来更好地控制。但它没有帮助。即使使用apache HTTP客户端,我也可以看到这些等待连接线程。

Migrated whole implementation to use apache HTTP client as it has special APIs for better control. But it didn't help. Even with apache HTTP client, I could see these waiting connection threads.

最后在用于JBOSS HTTP连接器配置的redhat网站。为HTTP连接器配置了线程池,它解决了这个问题:

Finally found hint on redhat website for JBOSS HTTP connector configuration. Configured thread pool for HTTP connector and it solved the issue:

<subsystem xmlns="urn:jboss:domain:threads:1.1"> 
    <thread-factory name="http-connector-factory" group-name="uq-thread-pool" thread-name-pattern="HTTP-%t" priority="9"/> 
    <unbounded-queue-thread-pool name="uq-thread-pool"> 
        <max-threads count="5"/> 
        <keepalive-time time="5" unit="seconds"/> 
        <thread-factory name="http-connector-factory"/> 
    </unbounded-queue-thread-pool> 
</subsystem> 

<subsystem xmlns="urn:jboss:domain:web:2.2" default-virtual-server="default-host" native="false">

    <connector name="http" protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="http" socket-binding="http" executor="uq-thread-pool"/> 
    ....
    ....

这篇关于Java:HTTP连接创建的等待连接线程持续很长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆