问题
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(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
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().
I also tried to set following HTTP property:
httpConnection.addRequestProperty("Connection", "close");
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?
回答1:
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"/>
....
....
来源:https://stackoverflow.com/questions/35268924/java-waiting-connection-threads-created-by-http-connection-are-alive-for-very-l