2

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
}
 }
webczarnecki
  • 71
  • 2
  • 6

2 Answers2

2
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.

ok2c
  • 26,450
  • 5
  • 63
  • 71
  • Thank you for response, but this doesn't sovle my problem. When i try to use your code, the performance is terrible and i can't find how to make it better. What should i do? I have been trying to solve this for a few days before i decided to ask this question. Or should i try to use non-async version of apache http client? – webczarnecki Nov 07 '13 at 18:37
  • I found solution when i checked your previous answers :) Thank you a lot :) – webczarnecki Nov 07 '13 at 19:03
  • @webczarnecki: Performance issues have nothing to do with redirect handling. If performance in terms of raw data throughput is an overriding concern for you, you will be better suited by a decent blocking HTTP client. – ok2c Nov 08 '13 at 09:07
  • the LaxRedirectStrategy as per documentation only disables it for POST requests. See answer below for disabling it on any request. – Daniel Sep 17 '20 at 04:43
1

You can set that on the Apache Request object directly, no need to instantiate a separate client object.

apacheHttpRequestObject.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build())
Daniel
  • 2,087
  • 3
  • 23
  • 37