Reputation: 591
Hey I'm using Tomcat 7 and HttpClient 4.3.3. I am trying to connect to my Servlet with multiple threads but I often receive java.net.SocketException: Connection reset
. When I suspend the Servlet's threads at the first line I'm still receiving the exception on my client.
My question: is the cause of this problem maybe be linked with a too small number of maximum active connections? If not could please anyone help me with the problem?
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();
}
Exception:
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)
Code which starts the threads:
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." );
}
Upvotes: 0
Views: 12603
Reputation: 16354
Here is what the JavaDoc says about the mentioned exception:
Thrown to indicate that there is an error in the underlying protocol such as a TCP error
Through my personal experience, I faced such cases seems that it is not the other connection end (server) who closed the connection but your client. Otherwise you would receive another message: Connection reset by peer
This can happen due to many causes and in your case I guess it is related to the fact that you have not colsed the HttpClient
instance at the end of your work unit; so try adding below line in the finally block of your try
statement:
try
{
...
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
httpClient.close(); //close the underlying client.
}
You may also need to enable the SO_KEEPALIVE
to keep sockets alive:
cm.setDefaultSocketConfig(
SocketConfig.custom().setSoKeepAlive(true)
.setSoReuseAddress(true)
.setSoTimeout(3000)
.build());
Upvotes: 1