使用HTTPBuilder并获取“java.net.SocketException:没有可用的缓冲区空间(达到最大连接数?):connect”

时间:2013-09-18 13:15:00

标签: java groovy socketexception httpbuilder

我正在使用HTTPBuilder为NoSQL数据库的REST API构建客户端。除了处理大型数据集外,它工作正常。在大量调用之后,客户端抛出此异常:

java.net.SocketException: No buffer space available (maximum connections reached?): connect
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:127)
at  org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:446)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:417)
at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:366)
at groovyx.net.http.HTTPBuilder$request.call(Unknown Source)
at scripts.groovy.prototypes.NeoClient.getRelationship(NeoClient.groovy:70)
at com.gsgenetics.genie.trait.Node$Edge.getTargetNode(Node.java:108)
at com.gsgenetics.genie.trait.Node$Edge.compareTo(Node.java:145)
at com.gsgenetics.genie.trait.Node$Edge.compareTo(Node.java:67)
at com.gsgenetics.genie.trait.Node.getEdges(Node.java:32)
at com.gsgenetics.genie.trait.Node.iterator(Node.java:384)
at com.gsgenetics.genie.GenomeStructuredGraph.add(GenomeStructuredGraph.java:131)
at com.gsgenetics.genie.GenomeStructuredGraph.add(GenomeStructuredGraph.java:197)
at com.gsgenetics.genie.GenomeStructuredGraph$add.call(Unknown Source)
at scripts.groovy.prototypes.read-ccds$_run_closure3.doCall(read-ccds.groovy:53)
at scripts.groovy.prototypes.read-ccds.run(read-ccds.groovy:50)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

看起来某些资源尚未发布,但我可以'弄清楚究竟是什么以及如何让它们发布。我在Windows 7上运行它,但在Linux上运行时遇到类似的错误。

这是我实例化和使用HTTPBuilder的代码:

import groovyx.net.http.*
import net.sf.json.JSONArray
import net.sf.json.JSONObject

import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

class NeoClient {

    static final URL = 'http://some IP address'
//    static def http = new HTTPBuilder( URL )
    static net.sf.json.JSONObject query(statement, params=null, success=null, error=null) {
        def http = new HTTPBuilder( URL )
        net.sf.json.JSONObject returnJson = []
        http.request( POST, JSON ) {
            uri.path = '/db/data/cypher/'
            headers.'X-Stream' = 'true'
            requestContentType = JSON
            body =  [ query : statement , params : params ?: [:] ]


            response.success = { resp, json ->
                if (success) success(json)
                else {
                    println "Status ${resp.statusLine} Columns ${json.columns}\nData: ${json.data}"
                }
                returnJson = json
            }

            response.failure = { resp, message ->
                def result=[status:resp.statusLine.statusCode,statusText:resp.statusLine.reasonPhrase]
                result.headers = resp.headers.collect { h -> [ (h.name) : h.value ] }
                result.message = message
                if (error) {
                    error(result)
                } else {
                    println "Status: ${result.status} : ${result.statusText} "
                    println "Headers: ${result.headers}"
                    println "Message: ${result.message}"
                }
                throw new Exception("Neo Client Exception");
            }
        }
    }
}

请注意,我尝试在我的静态类NeoClient中使用单个HTTPBuilder实例,并为每次调用“query”创建一个新的HTTPBuilder实例,在任何一种情况下我都会得到相同的异常。

非常感谢任何想法。

由于

1 个答案:

答案 0 :(得分:3)

您应该使用HTTPBuilder.shutdown方法释放此实例持有的任何系统资源。

此外,为每个请求创建新的HTTPBuilder并不是性能和资源管理方面的最佳方法。更好的方法是使用HttpBuilder的单个实例来处理多个请求。为此,您需要覆盖HttpBuilder的默认行为以使用线程安全连接管理器:

private static class CustomHttpBuilder extends HttpBuilder {
    protected AbstractHttpClient createClient( HttpParams params ) {
       def connManager = new MultiThreadedHttpConnectionManager()
       def connManagerParams = new HttpConnectionManagerParams()
       connManagerParams.maxTotalConnections = 50 // default is 20
       connManagerParams.defaultMaxConnectionsPerHost = 50 // default is 2
       connManager.params = connManagerParams
       new HttpClient(connManager)
   }
} 

我从Grails users forum获得了此代码示例。这将允许您对多个请求使用CustomHttpBuilder的单个实例。