Apache HttpComponents HttpClient timeout
Jak ustawić limit czasu połączenia w httpcomponents httpclient? Dokumentację znalazłem pod adresem: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html ale nie jest jasne, jak te parametry są rzeczywiście ustawione.
Pomocne byłoby również wyjaśnienie różnicy między SO_TIMEOUT
i CONNECTION_TIMEOUT
.
5 answers
W wersji 4.3 klienta Http Apache konfiguracja została zrefakturowana (ponownie). Nowy sposób wygląda tak:
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder = requestBuilder.setConnectTimeout(timeout);
requestBuilder = requestBuilder.setConnectionRequestTimeout(timeout);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setDefaultRequestConfig(requestBuilder.build());
HttpClient client = builder.build();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-07 13:12:59
Odpowiedź od @jontro jest poprawna, ale zawsze miło jest mieć fragment kodu, jak to zrobić. Można to zrobić na dwa sposoby:
Wersja 1: Ustaw 10 sekund timeout dla każdego z tych parametrów:
HttpClient httpclient = new DefaultHttpClient();
// this one causes a timeout if a connection is established but there is
// no response within 10 seconds
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10 * 1000);
// this one causes a timeout if no connection is established within 10 seconds
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10 * 1000);
// now do the execute:
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);
Wersja 2: Ustaw także 10 sekund timeout dla każdego z tych parametrów:
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
HttpConnectionParams.setSoTimeout(params, 10 * 1000);
HttpClient httpclient = new DefaultHttpClient(params);
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-29 20:55:02
W wersji HttpClient 4.3 możesz użyć poniższego przykładu.. powiedzmy przez 5 sekund
int timeout = 5;
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout * 1000)
.setConnectionRequestTimeout(timeout * 1000)
.setSocketTimeout(timeout * 1000).build();
CloseableHttpClient client =
HttpClientBuilder.create().setDefaultRequestConfig(config).build();
HttpGet request = new HttpGet("http://localhost:8080/service"); // GET Request
response = client.execute(request);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-19 09:37:32
W sekcja 2.5 znajduje się przykład jak ustawić parametr CONNECTION_TIMEOUT.
CONNECTION_TIMEOUT to czas oczekiwania na początkowe połączenie, a SO_TIMEOUT to czas oczekiwania na odczyt pakietu po nawiązaniu połączenia.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-16 23:09:57
Ustawiłem hard timeout dla całego żądania, aby obejść problem java.net.SocketInputStream.socketRead0
.
private static final ScheduledExecutorService SCHEDULED_EXECUTOR = Executors.newSingleThreadScheduledExecutor()
HttpGet request = new HttpGet("http://www.example.com")
final Runnable delayedTask = new Runnable() {
@Override
public void run() {
request.abort()
}
}
SCHEDULED_EXECUTOR.schedule(delayedTask, 100000, TimeUnit.MILLISECONDS)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-05 08:59:20