gpt4 book ai didi

java - Apache http 客户端 -> 本地地址绑定(bind) -> 一段时间后出现 BindException

转载 作者:行者123 更新时间:2023-11-29 05:49:38 25 4
gpt4 key购买 nike

操作系统:Linux (Debian)
Java: Java(TM) SE Runtime Environment (build 1.7.0_05-b06)
Java:Java HotSpot(TM) 64 位服务器 VM(构建 23.1-b03,混合模式)
Apache http 客户端:v4.2.3(最新)

我想创建一个包含多个客户端的 PoolingConnectionManager,每个客户端都有其唯一的本地地址 (ConnRoutePNames.LOCAL_ADDRESS)。
这些客户端将由多个工作人员使用(每个工作人员选择随机客户端并执行请求)。

问题是:当我为客户端设置本地地址时,一段时间后(例如 1 分钟)我总是收到 java.net.BindException 消息“地址已在使用”。

问题:是bug吗?

java.net.BindException: Address already in use
at java.net.PlainSocketImpl.socketBind(Native Method) ~[na:1.7.0_05]
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376) ~[na:1.7.0_05]
at java.net.Socket.bind(Socket.java:627) ~[na:1.7.0_05]
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:120) ~[httpclient-4.2.3.jar:4.2.3]
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180) ~[httpclient-4.2.3.jar:4.2.3]
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294) ~[httpclient-4.2.3.jar:4.2.3]
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:645) ~[httpclient-4.2.3.jar:4.2.3]
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480) ~[httpclient-4.2.3.jar:4.2.3]
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) ~[httpclient-4.2.3.jar:4.2.3]
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805) ~[httpclient-4.2.3.jar:4.2.3]
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784) ~[httpclient-4.2.3.jar:4.2.3]
at controllers.Test$Worker.run(Test.java:67) ~[test_2.9.1-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
at java.lang.Thread.run(Thread.java:722) [na:1.7.0_05]

代码(简化):

public static void main(String[] args) throws UnknownHostException
{
ClientConnectionManager connManager = buildConnectionManager(30);

List<HttpClient> clients = new ArrayList<>();
for(int i=0; i<8; ++i)
{
HttpClient client = buildClient(connManager, InetAddress.getByName("111.111.111." + (50 + i));
clients.add(client);
}

for(int i=0; i<30; ++i)
{
new Thread(new Worker(clients)).start();
}
}

public static ClientConnectionManager buildConnectionManager(Integer parallelism)
{
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
connectionManager.setMaxTotal(parallelism);
connectionManager.setDefaultMaxPerRoute(parallelism);

return connectionManager;
}

public static HttpClient buildClient(ClientConnectionManager connectionManager, InetAddress localAddress)
{
DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
HttpParams params = httpClient.getParams();
params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress);

return httpClient;
}

private static class Worker implements Runnable
{
private List<HttpClient> clients = null;

public Worker(List<HttpClient> clients)
{
this.clients = clients;
}

public void run()
{

do
{
HttpGet httpGet = new HttpGet("http://google.com/robots.txt");
HttpClient client = this.clients.get(new Random().nextInt(this.clients.size()));

try
{
HttpResponse httpResponse = client.execute(httpGet);
EntityUtils.consume(httpResponse.getEntity());

logger.debug("Success request");
}
catch(IOException e)
{
httpGet.abort();
logger.info("IO error", e);
}
}
while(true);
}
}

最佳答案

此问题可能与您的 TCP 堆栈有关。我不认为这是 HttpClient 中的错误。您可以在这里找到解决方案: http://planet.jboss.org/post/concurrent_high_throughput_performance_testing_with_jmeter

关于java - Apache http 客户端 -> 本地地址绑定(bind) -> 一段时间后出现 BindException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14419555/

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