操作系统:Linux (Debian)
Java:Java(TM) SE 运行时环境(内部版本 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);
}
}