2013年9月23日星期一

httpclient program baffling stop running , help

Using apache 's httpclient4.1.1 version , the main idea is to use a page httpclient monitoring whether to publish relevant content, but the actual operation is found , the program sometimes baffling stop off , the process still further , but I'm dead loop print no longer appear and feel the program is a deadlock in a certain place .
heap space is enough , jvm did not crash, do a dump, have not found any problems , seeking heroes help analyze possible deadlock in which place it ?

public class HttpClientTest {
HttpClient httpclient = new DefaultHttpClient();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
public static void main(String[] args) {
HttpClientTest tester = new HttpClientTest();
HashMap<String, String> headMap = new HashMap<String, String>();
String strResult;
int i=0;
while (true) {
try {
if(i>1000000){//每100W重置一次计数
i=0;
}
if (i % 10000 == 0) {
System.out.println(Calendar.getInstance().getTime().toString()+ "i have run "+ i + " times!");
}
i++;
Thread.sleep(1000);// 暂停1秒

// 1---进行GET请求,看是否有最新内容
headMap.clear();
// headMap.put("Accept-Language", "zh-CN");
headMap.put("Accept", "text/html, application/xhtml+xml, */*");
// headMap.put("Accept-Encoding", "gzip, deflate");
strResult = tester.httpGetResponse("http://127.0.0.1", headMap);
int index1 = strResult.indexOf("最新");//是否新内容
if (index1 < 0) {
continue;
}
System.out.println(Calendar.getInstance().getTime().toString()+" have news!");
}catch (Exception e) {
e.printStackTrace();
}
}
}
/**
 * 进行get请求的方法
 * 
 * @param url
 *            请求的URL
 * @param headMap
 *            请求的http头信息
 * @return
 */
private String httpGetResponse(String url, HashMap<String, String> headMap)throws Exception  {
String responseBody = "";
if (url == null || url.trim().length() < 1) {
System.out.println("error:url is invalid! url=" + url);
return responseBody;
}


HttpGet httpget = new HttpGet(url);

try {
// Process the header info
if (headMap != null) {
Iterator it = headMap.keySet().iterator();
String key, value;
while (it.hasNext()) {
key = it.next().toString();
value = headMap.get(key);
if (key != null && key.trim().length() > 0 && value != null
&& value.trim().length() > 0) {
// System.out.println("header: " + key+"="+value);
httpget.addHeader(key, value);
}
}
}

responseBody =httpclient.execute(httpget,
responseHandler);
// System.out.println("----------------------------------------");
// System.out.println(responseBody);
// System.out.println("----------------------------------------");

} catch (Exception e) {
//e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
//httpclient.getConnectionManager().shutdown();
}
return responseBody;
}
}

------ Solution ------------------------------------- -------


Returns the timeout until a connection is etablished. A value of zero means the timeout is not used. The default value is zero .
------ For reference only ------------ ---------------------------
add , tested , and sometimes you can run 10 hours , sometimes less than a run hours stuck, no longer performed , seeking analysis ah
------ For reference only ------------------------ ---------------
next jammed when using JDK comes jstack.exe, see Thread the situation is more clear.
------ For reference only -------------------------------------- -
grabbed a thread dump, which has a lock, but the lock in the thread when the normal is there, it should be read returns the contents of the lock, I do not know because of this reason , seeking analysis ah

2012-06-13 16:36:16
Full thread dump Java HotSpot (TM) Client VM (11.0-b15 mixed mode):

"JMX server connection timeout 12" daemon prio = 6 tid = 0x66107800 nid = 0x17ac in Object.wait () [0x66c4f000 .. 0x66c4fbe8]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait (Native Method)
- waiting on <0x08a89738> (a [I)
at com.sun.jmx.remote.internal.ServerCommunicatorAdmin $ Timeout.run (Unknown Source)
- locked <0x08a89738> (a [I)
at java.lang.Thread.run (Unknown Source)

Locked ownable synchronizers:
- None

"RMI Scheduler (0)" daemon prio = 6 tid = 0x66103c00 nid = 0x16c4 waiting on condition [0x66bff000 .. 0x66bffc68]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park (Native Method)
- parking to wait for <0x08a897c0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer $ ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos (Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer $ ConditionObject.awaitNanos (Unknown Source)
at java.util.concurrent.DelayQueue.take (Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor $ DelayedWorkQueue.take (Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor $ DelayedWorkQueue.take (Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.getTask (Unknown Source)
at java.util.concurrent.ThreadPoolExecutor $ Worker.run (Unknown Source)
at java.lang.Thread.run (Unknown Source)

Locked ownable synchronizers:
- None

"RMI TCP Connection (1) -192.168.1.103" daemon prio = 6 tid = 0x66103800 nid = 0x161c runnable [0x66baf000 .. 0x66bafce8]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0 (Native Method)
at java.net.SocketInputStream.read (Unknown Source)
at java.io.BufferedInputStream.fill (Unknown Source)
at java.io.BufferedInputStream.read (Unknown Source)
- locked <0x08a8b978> (a java.io.BufferedInputStream)
at java.io.FilterInputStream.read (Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages (Unknown Source)
at sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.run0 (Unknown Source)
at sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.run (Unknown Source)
at java.util.concurrent.ThreadPoolExecutor $ Worker.runTask (Unknown Source)
at java.util.concurrent.ThreadPoolExecutor $ Worker.run (Unknown Source)
at java.lang.Thread.run (Unknown Source)

Locked ownable synchronizers:
- <0x08a93040> (a java.util.concurrent.locks.ReentrantLock $ NonfairSync)

"RMI TCP Accept-0" daemon prio = 6 tid = 0x66193800 nid = 0x1600 runnable [0x6689f000 .. 0x6689fd68]
java.lang.Thread.State: RUNNABLE
at java.net.PlainSocketImpl.socketAccept (Native Method)
at java.net.PlainSocketImpl.accept (Unknown Source)
- locked <0x08a8bc30> (a java.net.SocksSocketImpl)
at java.net.ServerSocket.implAccept (Unknown Source)
at java.net.ServerSocket.accept (Unknown Source)
at sun.management.jmxremote.LocalRMIServerSocketFactory $ 1.accept (Unknown Source)
at sun.rmi.transport.tcp.TCPTransport $ AcceptLoop.executeAcceptLoop (Unknown Source)
at sun.rmi.transport.tcp.TCPTransport $ AcceptLoop.run (Unknown Source)
at java.lang.Thread.run (Unknown Source)

Locked ownable synchronizers:
- None

"Low Memory Detector" daemon prio = 6 tid = 0x01a77800 nid = 0xfb8 runnable [0x00000000 .. 0x00000000]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"CompilerThread0" daemon prio = 10 tid = 0x01a34000 nid = 0xfb4 waiting on condition [0x00000000 .. 0x65e9f790]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"Attach Listener" daemon prio = 10 tid = 0x01a33400 nid = 0xfb0 waiting on condition [0x00000000 .. 0x65e4fb14]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"Signal Dispatcher" daemon prio = 10 tid = 0x01a32c00 nid = 0xfac runnable [0x00000000 .. 0x00000000]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"Finalizer" daemon prio = 8 tid = 0x01a20400 nid = 0xf98 in Object.wait () [0x65daf000 .. 0x65dafc68]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait (Native Method)
- waiting on <0x089d02b0> (a java.lang.ref.ReferenceQueue $ Lock)
at java.lang.ref.ReferenceQueue.remove (Unknown Source)
- locked <0x089d02b0> (a java.lang.ref.ReferenceQueue $ Lock)
at java.lang.ref.ReferenceQueue.remove (Unknown Source)
at java.lang.ref.Finalizer $ FinalizerThread.run (Unknown Source)

Locked ownable synchronizers:
- None

"Reference Handler" daemon prio = 10 tid = 0x01a1b800 nid = 0xf94 in Object.wait () [0x65d5f000 .. 0x65d5fce8]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait (Native Method)
- waiting on <0x089d0338> (a java.lang.ref.Reference $ Lock)
at java.lang.Object.wait (Object.java: 485)
at java.lang.ref.Reference $ ReferenceHandler.run (Unknown Source)
- locked <0x089d0338> (a java.lang.ref.Reference $ Lock)

Locked ownable synchronizers:
- None

"main" prio = 6 tid = 0x00c89400 nid = 0xf6c runnable [0x003cf000 .. 0x003cfe28]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0 (Native Method)
at java.net.SocketInputStream.read (Unknown Source)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer (AbstractSessionInputBuffer.java: 149)
at org.apache.http.impl.io.SocketInputBuffer.fillBuffer (SocketInputBuffer.java: 110)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine (AbstractSessionInputBuffer.java: 260)
at org.apache.http.impl.conn.DefaultResponseParser.parseHead (DefaultResponseParser.java: 98)
at org.apache.http.impl.io.AbstractMessageParser.parse (AbstractMessageParser.java: 252)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader (AbstractHttpClientConnection.java: 281)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader (DefaultClientConnection.java: 247)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader (AbstractClientConnAdapter.java: 219)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse (HttpRequestExecutor.java: 298)
at org.apache.http.protocol.HttpRequestExecutor.execute (HttpRequestExecutor.java: 125)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute (DefaultRequestDirector.java: 633)
at org.apache.http.impl.client.DefaultRequestDirector.execute (DefaultRequestDirector.java: 454)
at org.apache.http.impl.client.AbstractHttpClient.execute (AbstractHttpClient.java: 820)
at org.apache.http.impl.client.AbstractHttpClient.execute (AbstractHttpClient.java: 941)
at org.apache.http.impl.client.AbstractHttpClient.execute (AbstractHttpClient.java: 919)
at org.apache.http.impl.client.AbstractHttpClient.execute (AbstractHttpClient.java: 910)
at HttpClientTest.httpGetResponse (HttpClientTest.java: 34)
at HttpClientTest.main (HttpClientTest.java: 4)

Locked ownable synchronizers:
- None

"VM Thread" prio = 10 tid = 0x01a16c00 nid = 0xf90 runnable

"VM Periodic Task Thread" prio = 10 tid = 0x01a78800 nid = 0xfbc waiting on condition

JNI global references: 719


------ For reference only ---------------------------------- -----
with the lock should be nothing to stop here , it should be the other party does not respond , so the wait timeout , the timeout will be very very long .


"main" prio = 6 tid = 0x00c89400 nid = 0xf6c runnable [0x003cf000 .. 0x003cfe28]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0 (Native Method)
at java.net.SocketInputStream.read (Unknown Source)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer (AbstractSessionInputBuffer.java: 149)
at org.apache.http.impl.io.SocketInputBuffer.fillBuffer (SocketInputBuffer.java: 110)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine (AbstractSessionInputBuffer.java: 260)
at org.apache.http.impl.conn.DefaultResponseParser.parseHead (DefaultResponseParser.java: 98)
at org.apache.http.impl.io.AbstractMessageParser.parse (AbstractMessageParser.java: 252)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader (AbstractHttpClientConnection.java: 281)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader (DefaultClientConnection.java: 247)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader (AbstractClientConnAdapter.java: 219)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse (HttpRequestExecutor.java: 298)
at org.apache.http.protocol.HttpRequestExecutor.execute (HttpRequestExecutor.java: 125)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute (DefaultRequestDirector.java: 633)
at org.apache.http.impl.client.DefaultRequestDirector.execute (DefaultRequestDirector.java: 454)
at org.apache.http.impl.client.AbstractHttpClient.execute (AbstractHttpClient.java: 820)
at org.apache.http.impl.client.AbstractHttpClient.execute (AbstractHttpClient.java: 941)
at org.apache.http.impl.client.AbstractHttpClient.execute (AbstractHttpClient.java: 919)
at org.apache.http.impl.client.AbstractHttpClient.execute (AbstractHttpClient.java: 910)
at HttpClientTest.httpGetResponse (HttpClientTest.java: 34)
at HttpClientTest.main (HttpClientTest.java: 4)


set your own timeout it:
HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager (). getParams ();
managerParams.setConnectionTimeout (5000); / / set the connection timeout ( in milliseconds ) , for example, 5 seconds
managerParams.setSoTimeout (60000); / / Set the read data timeout ( in milliseconds ) , for example, 60 seconds
------ For reference only ---- -----------------------------------
I set the timeout too , does not seem very useful, looking back I try again , thank you
------ For reference only -------------------------------- -------


By the way, if not set the timeout , httpclient should have a default timeout , not always die in that place it
------ For reference only --------- ------------------------------
ask the landlord do to solve this problem . I use the 3.1 version is also encountered this problem, do not regularly occur threads no response . After setting the timeout is still invalid, wrote their own timeout control function to solve the problem , but the business is still a large number of time-out failures. Requested service side said I did not receive the response sent by the client , I do not know why there is a request could not be sent out the problem , and this problem seems set comes with httpclient timeout parameter is not effective .

没有评论:

发表评论