首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tomcat 7-连接复位

Tomcat 7-连接复位
EN

Stack Overflow用户
提问于 2014-06-10 08:18:00
回答 1查看 12.2K关注 0票数 0

嘿,我在使用Tomcat 7和HttpClient 4.3.3。我试图用多个线程连接到Servlet,但我经常收到java.net.SocketException: Connection reset。当我在第一行挂起Servlet的线程时,我仍然在我的客户机上接收异常。我的问题是:造成这一问题的原因是否与数量太少的最大活动连接有关?如果没有人能帮我解决这个问题?

代码语言:javascript
复制
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();
    }

例外:

代码语言:javascript
复制
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)

启动线程的代码:

代码语言:javascript
复制
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." );
}
EN

回答 1

Stack Overflow用户

发布于 2014-06-10 09:01:46

下面是JavaDoc对提到的异常的看法:

引发,以指示基础协议中存在错误,例如TCP错误。

根据我个人的经验,我面对这样的情况似乎不是另一个连接端(服务器)关闭了连接,而是您的客户端。否则,您将收到另一条消息:Connection reset by peer

这可能是由于许多原因造成的,在您的例子中,我猜这与您没有在您的工作单元的末尾显示HttpClient实例有关;因此,请尝试在try语句的最后一个块中添加下面的一行:

代码语言:javascript
复制
try
{
  ...
}
catch( Exception e )
{
  e.printStackTrace();
}
finally 
{
  httpClient.close(); //close the underlying client.
}

更新:

您还可能需要启用SO_KEEPALIVE来保持套接字正常运行:

代码语言:javascript
复制
    cm.setDefaultSocketConfig( 
        SocketConfig.custom().setSoKeepAlive(true)
          .setSoReuseAddress(true)
          .setSoTimeout(3000)
          .build());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24136084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档