我們正在使用帶有 Spring-Ws 的 Http Client 4.5.x 并使用該webServiceTemplate.marshalSendAndReceive(requestObject)方法發出請求。我們想要一個可靠的連接超時值,但目前遇到了第 8 節(DNS 循環)中描述的問題,其中嘗試了多個 IP 地址,因此超時是不可預測的。是否有簡單的方法可以僅使用 Spring-ws 和 Http Client 庫在一段時間后設置硬超時,或者是否需要設置某種自定義超時?案例:連接超時設置為 1 秒(該方法的實際超時為 4 秒——是否可以使用 Spring/Http 客戶端庫將方法超時設置為 1 秒?)應用程序日志(Http 客戶端日志設置為DEBUG):16:45:02 (org.apache.http.impl.execchain.MainClientExec) Opening connection {}->http://salesforce.com:448 16:45:02 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connecting to salesforce.com/96.43.149.26:448 16:45:03 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connect to salesforce.com/96.43.149.26:448 timed out. Connection will be retried using another IP address 16:45:03 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connecting to salesforce.com/96.43.145.26:448 16:45:04 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connect to salesforce.com/96.43.145.26:448 timed out. Connection will be retried using another IP address 16:45:04 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connecting to salesforce.com/96.43.144.26:448 16:45:05 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connect to salesforce.com/96.43.144.26:448 timed out. Connection will be retried using another IP address 16:45:05 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connecting to salesforce.com/96.43.148.26:44816:45:06 (org.apache.http.impl.conn.DefaultManagedHttpClientConnection) http-outgoing-0: Shutdown connection Http客戶端bean:<bean id="httpClientBean" class="org.apache.http.client.HttpClient" factory-bean="httpClientFactory" factory-method="getHttpClient" />服務代碼(我們希望這個方法調用需要 X 秒,而不是 2x 或 3x 秒):// we want this method call to take ~1 second, not ~4 seconds (i.e. similar to the connection timeout value, not a multiplier)Object obj = webServiceTemplate.marshalSendAndReceive(requestDocument);
1 回答

蠱毒傳說
TA貢獻1895條經驗 獲得超3個贊
有兩種選擇
建立自定義 ClientConnectionOperator
構建自定義DnsResolver. 這個選項要簡單得多。
CloseableHttpClient client = HttpClients.custom()
.setDnsResolver(host -> new InetAddress[] { InetAddress.getByName(host) })
.build();
添加回答
舉報
0/150
提交
取消