How to set redirect strategy in apache async http client

痞子三分冷 提交于 2020-01-23 15:42:32

问题


How i can set redirect strategy in apache async http client? I have something like this (scala code). Commented code works as expected, but i am not able to perform more than 4 concurrent request to one host per second, second version can handle much more concurrent connections but doesn't handle redirects at all.

object HttpClientManager {
def createHttpClient(): CloseableHttpAsyncClient = { //cm: NHttpClientConnectionManager
    /*


    val httpClient = HttpAsyncClients
        .custom()
        .setDefaultRequestConfig(config)
        //.setConnectionManager(cm)
        .build()
*/
    // val config = RequestConfig.custom()
  //           .setSocketTimeout(3000)
  //           .setConnectTimeout(3000).build();

    val socketConfig = SocketConfig.custom()
            .setSoTimeout(15000)
            .build();
    val connectionConfig = ConnectionConfig.custom()
            .setBufferSize(8 * 1024)
            .setFragmentSizeHint(8 * 1024)
            .build();

        val ioreactor = new DefaultConnectingIOReactor();
        val mgr = new PoolingNHttpClientConnectionManager(ioreactor);
        mgr.setDefaultSocketConfig(socketConfig);
        mgr.setDefaultConnectionConfig(connectionConfig);
        mgr.setDefaultMaxPerRoute(100)
        mgr.setMaxTotal(200)
        val httpclient = HttpAsyncClients.createMinimal(mgr);

    httpclient.start()
    httpclient
}
 }

回答1:


CloseableHttpAsyncClient client = HttpAsyncClients.custom()
       .setRedirectStrategy(LaxRedirectStrategy.INSTANCE)
       .build();

Minimal client created by HttpAsyncClients#createMinimal uses absolutely the same connection management code as its 'full-blown' counterpart. It differs from it though in providing only a minimal protocol pipeline in order to provide better performance in those scenarios when people are prepared to sacrifice non-essential protocol aspects: proxy support, redirect, authentication and state management. So, minimal implementation simply does not handle redirects.



来源:https://stackoverflow.com/questions/19821742/how-to-set-redirect-strategy-in-apache-async-http-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!