[英]How to set redirect strategy in apache async http client
我如何在 apache 異步 http 客戶端中設置重定向策略? 我有這樣的東西(scala 代碼)。 注釋代碼按預期工作,但我每秒無法對一台主機執行超過 4 個並發請求,第二個版本可以處理更多並發連接,但根本不處理重定向。
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
}
}
CloseableHttpAsyncClient client = HttpAsyncClients.custom()
.setRedirectStrategy(LaxRedirectStrategy.INSTANCE)
.build();
由HttpAsyncClients#createMinimal
創建的最小客戶端使用與其“成熟”對應物完全相同的連接管理代碼。 它與它的不同之處在於僅提供最小的協議管道,以便在人們准備犧牲非必要協議方面的情況下提供更好的性能:代理支持、重定向、身份驗證和狀態管理。 因此,最小實現根本不處理重定向。
您可以直接在 Apache Request 對象上設置它,無需實例化單獨的客戶端對象。
apacheHttpRequestObject.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.