Java &引用;“连接重置”;Mac上的Apache HttpClient 4.5异常
我需要最多1000个线程来同时发送HTTP请求。但在Mac上,我似乎达到了某种极限,这导致Apache HTTPClient返回异常:Java &引用;“连接重置”;Mac上的Apache HttpClient 4.5异常,java,macos,apache,httpclient,apache-httpclient-4.x,Java,Macos,Apache,Httpclient,Apache Httpclient 4.x,我需要最多1000个线程来同时发送HTTP请求。但在Mac上,我似乎达到了某种极限,这导致Apache HTTPClient返回异常: INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://my.url:80: Connection reset 该限制似乎介于200到300个并发连接之间 我创建了一个简单的应用程序,即使使用该应用程序,也能够重现问题: imp
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://my.url:80: Connection reset
该限制似乎介于200到300个并发连接之间
我创建了一个简单的应用程序,即使使用该应用程序,也能够重现问题:
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class App implements Runnable
{
private static final int NUMBER_THREADS = 1000;
public static void main( String[] args )
{
Thread[] threads = new Thread[NUMBER_THREADS];
for(int i = 0; i < NUMBER_THREADS; i++)
{
threads[i] = new Thread(new App());
}
for(int i = 0; i < NUMBER_THREADS; i++)
{
threads[i].start();
}
}
public void run()
{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://my.url/test.html");
CloseableHttpResponse response = null;
try
{
System.out.println( System.nanoTime() + " " + Thread.currentThread().getName() + " started" );
response = httpclient.execute(httpget);
System.out.println( System.nanoTime() + " " + Thread.currentThread().getName() + " finished" );
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(response != null)
{
response.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
所以我想知道这个极限是从哪里来的?OsX/Apache HttpClient本身?有可能控制它吗
提前感谢。问题确实出在maxfile上,但我没有正确更改它们。这篇文章帮助我解决了这个问题,它展示了在MacOSXYosemite/ElCapitan上更新文件限制的正确过程
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxfiles</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxfiles</string>
<string>65536</string>
<string>65536</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxproc</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxproc</string>
<string>2048</string>
<string>2048</string>
</array>
<key>RunAtLoad</key>
<true />
<key>ServiceIPC</key>
<false />
</dict>
升级到最新的MacOS 10.11/El Capitan没有帮助
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxproc</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxproc</string>
<string>2048</string>
<string>2048</string>
</array>
<key>RunAtLoad</key>
<true />
<key>ServiceIPC</key>
<false />
</dict>
ulimit -n 65536
ulimit -u 2048