gpt4 book ai didi

java - 使用 HTTPBuilder 并获取 "java.net.SocketException: No buffer space available (maximum connections reached?): connect"

转载 作者:行者123 更新时间:2023-12-04 04:41:12 24 4
gpt4 key购买 nike

我正在使用 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 实例,并为每次调用“查询”创建一个新的 HTTPBuilder 实例,在任何一种情况下我都会遇到相同的异常。

任何想法将不胜感激。

谢谢

最佳答案

您应该使用 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 的单个实例。对于多个请求。

关于java - 使用 HTTPBuilder 并获取 "java.net.SocketException: No buffer space available (maximum connections reached?): connect",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18873287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com