user613114 February 2016

Java: Waiting connection threads created by HTTP connection are alive for very long duration

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
    }
}

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(ThreadPoolExecut        

Answers


user613114 February 2016

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.

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"/> 
    ....
    ....

Post Status

Asked in February 2016
Viewed 2,149 times
Voted 4
Answered 1 times

Search




Leave an answer