嘿,我在使用Tomcat 7和HttpClient 4.3.3。我试图用多个线程连接到Servlet,但我经常收到java.net.SocketException: Connection reset。当我在第一行挂起Servlet的线程时,我仍然在我的客户机上接收异常。我的问题是:造成这一问题的原因是否与数量太少的最大活动连接有关?如果没有人能帮我解决这个问题?
try{
// create new httpPost request with url of his class
HttpPost httpPost = new HttpPost( "http://192.168.1.229:8080/test/test" );
// create params and add it to httpPost
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
paramList.add( new BasicNameValuePair( "json_req", format ) );
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity( paramList );
httpPost.setEntity( formEntity );
// execute request and save response
CloseableHttpResponse response = httpclient.execute( httpPost, context );
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
resp = content.available() > 0;
content.close();
response.close();
// return the response
}
catch( Exception e ){
e.printStackTrace();
}例外:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:136)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:152)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:270)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:161)
at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.http.impl.conn.CPoolProxy.invoke(CPoolProxy.java:138)
at $Proxy0.receiveResponseHeader(Unknown Source)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:254)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at com.pribas.flightcacheservlet.servlet.HTTPThread.run(HTTPThread.java:69)
at java.lang.Thread.run(Thread.java:662)启动线程的代码:
public static void main( String[] args ) throws Exception{
FileUtil.init();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal( 1000 );
cm.setDefaultMaxPerRoute( 1000 );
cm.setDefaultSocketConfig( SocketConfig.custom().setSoKeepAlive( true ).setSoReuseAddress( true ).setSoTimeout( 3000 ).build() );
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager( cm ).build();
HTTPThread.THREAD_COUNT = 300;
HTTPThread.start = new CountDownLatch( HTTPThread.THREAD_COUNT );
Thread[] threads = new Thread[ HTTPThread.THREAD_COUNT ];
for( int i = 0; i < HTTPThread.THREAD_COUNT; i++ ){
threads[ i ] = new Thread( new HTTPThread( httpClient ) );
}
for( Thread thread : threads ){
thread.start();
}
for( Thread thread : threads ){
thread.join();
}
httpClient.close();
System.out.println( "Average response time: " + calAverage( HTTPThread.times ) + " milliseconds." );
}发布于 2014-06-10 09:01:46
下面是JavaDoc对提到的异常的看法:
引发,以指示基础协议中存在错误,例如TCP错误。
根据我个人的经验,我面对这样的情况似乎不是另一个连接端(服务器)关闭了连接,而是您的客户端。否则,您将收到另一条消息:Connection reset by peer
这可能是由于许多原因造成的,在您的例子中,我猜这与您没有在您的工作单元的末尾显示HttpClient实例有关;因此,请尝试在try语句的最后一个块中添加下面的一行:
try
{
...
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
httpClient.close(); //close the underlying client.
}更新:
您还可能需要启用SO_KEEPALIVE来保持套接字正常运行:
cm.setDefaultSocketConfig(
SocketConfig.custom().setSoKeepAlive(true)
.setSoReuseAddress(true)
.setSoTimeout(3000)
.build());https://stackoverflow.com/questions/24136084
复制相似问题