簡體   English   中英

Mac上的Apache HttpClient 4.5出現“連接重置”異常

[英]“Connection reset” exception with Apache HttpClient 4.5 on Mac

我需要最多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個並發連接之間。

我創建了一個簡單的應用程序,即使使用該應用程序也能夠重現該問題:

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();
            }
        }
    }
}

我更新了一些Mac設置,但這無濟於事:

> sysctl -a | grep files
kern.maxfiles = 12288
kern.maxfilesperproc = 10240
kern.maxfiles: 12288
kern.maxfilesperproc: 10240
kern.num_files: 3202
> ulimit -n 10240
> ulimit -n
10240
> sysctl -a | grep somax
kern.ipc.somaxconn: 128
> sudo sysctl -w kern.ipc.somaxconn=2048
Password:
kern.ipc.somaxconn: 128 -> 2048
> sysctl -a | grep somax
kern.ipc.somaxconn: 2048

所以我想了解這個限制來自何處? OsX / Apache HttpClient本身? 並且可以控制它嗎?

提前致謝。

問題確實存在於maxfiles中,但是我沒有正確更改它們。 文章幫助我通過顯示更新在Mac OS X優勝美地/埃爾卡皮坦文件限制的正確程序來解決它。

  1. 創建和更新/Library/LaunchDaemons/limit.maxfiles.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.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> 
  2. 創建/Library/LaunchDaemons/limit.maxproc.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> 

  3. 添加.bashprofile並包含以下幾行:

     ulimit -n 65536 ulimit -u 2048 
  4. 重新啟動系統

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM