httpClient多线程问题

问题: 今天线上出现一个httpclient的问题:

java.lang.IllegalStateException: Connection is not open 
at org.apache.commons.httpclient.HttpConnection.assertOpen(HttpConnection.java:1 
276) 

        at org.apache.commons.httpclient.HttpConnection.getResponseInputStream(H 
ttpConnection.java:857) 
        at org.apache.commons.httpclient.HttpMethodBase.readResponseHeaders(Http 
MethodBase.java:1790) 
        at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodB 
ase.java:1592) 
        at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.j 
ava:995) 
        at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(Htt 
pMethodDirector.java:397) 
        at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMe 
thodDirector.java:170) 
        at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.jav 
a:396) 
        at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.jav 
a:324) 
        at com.kingpoint.BiDownTestCookie.getByPostMethod(BiDownTestCookie.java: 
172) 
        at com.kingpoint.ClickIE.run(ClickIE.java:27) 
        at java.util.TimerThread.mainLoop(Timer.java:512) 
        at java.util.TimerThread.run(Timer.java:462) 
场景:我通过多线程调用外部http服务,但是我的httpClient是一个单例,当某个请求返回以后,还有的请求用的是同样的单例,而上一个请求的连接我已经关闭了,所以导致connection is not open。

解决方案:

1. 请求加锁

2. 每次new HttpClient(); 保证独立。

参考:http://www.iteye.com/problems/63688

你可能感兴趣的:(问题汇总)