Search in sources :

Example 1 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project coprhd-controller by CoprHD.

the class BaseHttpClientFactory method createRawHTTPClient.

/**
 * Create a HTTPClient using the factories configuration without Credentials
 *
 * @param useConnectionTimeout - allows override of the the default connectionTimeout
 * @param useConnectionReadTimeout - allows override of the default connectionReadTimeout
 * @throws AuthenticationException, GeneralSecurityException, RuntimeException
 */
protected AbstractHttpClient createRawHTTPClient(int useConnectionTimeout, int useConnectionReadTimeout) throws AuthenticationException, GeneralSecurityException, RuntimeException {
    // select the appropriate connection manager and set options as appropriate
    ClientConnectionManager cm = null;
    if (threadSafeClients) {
        ThreadSafeClientConnManager tscm = new ThreadSafeClientConnManager();
        tscm.setMaxTotal(maxConnections);
        tscm.setDefaultMaxPerRoute(maxConnectionsPerHost);
        cm = tscm;
    } else {
        cm = new SingleClientConnManager();
    }
    // construct a client instance with the connection manager embedded
    AbstractHttpClient httpClient = new DefaultHttpClient(cm);
    if (relaxSSL) {
        // !!!WARNING: This effectively turns off the authentication component of SSL, leaving only encryption
        // can throw GeneralSecurityException
        SSLHelper.configurePermissiveSSL(httpClient);
    } else if (isSecureSSL()) {
        SSLHelper.configureSSLWithTrustManger(httpClient, coordinator);
    }
    // see org.apache.http.client.params.AllClientPNames for a collected
    // list of the available client parameters
    HttpParams clientParams = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(clientParams, useConnectionTimeout);
    HttpConnectionParams.setSoTimeout(clientParams, useConnectionReadTimeout);
    // consider turning off the use of the Expect: 100-Continue response if
    // your posts/puts tend to be relatively small
    HttpProtocolParams.setUseExpectContinue(clientParams, false);
    // TODO: reconsider this setting
    // by default the client auto-retries on failures - turn that off so we can handle it manually
    httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
    return httpClient;
}
Also used : AbstractHttpClient(org.apache.http.impl.client.AbstractHttpClient) HttpParams(org.apache.http.params.HttpParams) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SingleClientConnManager(org.apache.http.impl.conn.SingleClientConnManager) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 2 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project gateway-dubbox by zhuzhong.

the class OpenApiHttpClientServiceImpl method initHttpClient.

private void initHttpClient() {
    // 注册访问协议相关的socket工厂
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", SSLConnectionSocketFactory.getSystemSocketFactory()).build();
    // httpclient 工厂
    HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory(DefaultHttpRequestWriterFactory.INSTANCE, DefaultHttpResponseParserFactory.INSTANCE);
    // dns解析器
    DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE;
    // 创建池化连接管理器
    manager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, connFactory, dnsResolver);
    // 默认socket配置
    SocketConfig defaultSocketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
    manager.setDefaultSocketConfig(defaultSocketConfig);
    // 设置整个连接池的最大连接数
    manager.setMaxTotal(this.maxTotal);
    // 每个路由最大连接数
    manager.setDefaultMaxPerRoute(this.defaultMaxPerRoute);
    manager.setValidateAfterInactivity(this.validateAfterInactivity);
    RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(this.connectionTimeout).setSocketTimeout(this.socketTimeout).setConnectionRequestTimeout(this.connectionRequestTimeout).build();
    httpClient = HttpClients.custom().setConnectionManager(manager).setConnectionManagerShared(false).evictIdleConnections(60, TimeUnit.SECONDS).evictExpiredConnections().setConnectionTimeToLive(60, TimeUnit.SECONDS).setDefaultRequestConfig(defaultRequestConfig).setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE).setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE).setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)).build();
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : DnsResolver(org.apache.http.conn.DnsResolver) SystemDefaultDnsResolver(org.apache.http.impl.conn.SystemDefaultDnsResolver) RequestConfig(org.apache.http.client.config.RequestConfig) SocketConfig(org.apache.http.config.SocketConfig) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpRoute(org.apache.http.conn.routing.HttpRoute) ManagedHttpClientConnection(org.apache.http.conn.ManagedHttpClientConnection) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) ManagedHttpClientConnectionFactory(org.apache.http.impl.conn.ManagedHttpClientConnectionFactory)

Example 3 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project dropwizard by dropwizard.

the class JerseyClientBuilderTest method usesACustomHttpRequestRetryHandler.

@Test
void usesACustomHttpRequestRetryHandler() {
    final DefaultHttpRequestRetryHandler customRetryHandler = new DefaultHttpRequestRetryHandler(2, true);
    builder.using(customRetryHandler);
    verify(apacheHttpClientBuilder).using(customRetryHandler);
}
Also used : DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project beam by apache.

the class HttpHealthcareApiClient method initClient.

private void initClient() throws IOException {
    credentials = GoogleCredentials.getApplicationDefault();
    // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests.
    HttpRequestInitializer requestInitializer = new AuthenticatedRetryInitializer(credentials.createScoped(CloudHealthcareScopes.CLOUD_PLATFORM, StorageScopes.CLOUD_PLATFORM_READ_ONLY));
    client = new CloudHealthcare.Builder(new NetHttpTransport(), new JacksonFactory(), requestInitializer).setApplicationName("apache-beam-hl7v2-io").build();
    httpClient = HttpClients.custom().setRetryHandler(new DefaultHttpRequestRetryHandler(10, false)).build();
}
Also used : NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) CloudHealthcare(com.google.api.services.healthcare.v1.CloudHealthcare) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) RetryHttpRequestInitializer(org.apache.beam.sdk.extensions.gcp.util.RetryHttpRequestInitializer) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory)

Example 5 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project voldemort by voldemort.

the class HttpClientBench method createClient.

private static HttpClient createClient() {
    ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(SchemeRegistryFactory.createDefault(), DEFAULT_CONNECTION_MANAGER_TIMEOUT, TimeUnit.MILLISECONDS);
    DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
    HttpParams clientParams = httpClient.getParams();
    HttpConnectionParams.setSocketBufferSize(clientParams, 60000);
    HttpConnectionParams.setTcpNoDelay(clientParams, false);
    HttpProtocolParams.setUserAgent(clientParams, VOLDEMORT_USER_AGENT);
    HttpProtocolParams.setVersion(clientParams, HttpVersion.HTTP_1_1);
    // HostConfiguration hostConfig = new HostConfiguration();
    // hostConfig.setHost("localhost");
    HttpConnectionParams.setConnectionTimeout(clientParams, DEFAULT_CONNECTION_MANAGER_TIMEOUT);
    HttpConnectionParams.setSoTimeout(clientParams, 500);
    httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
    HttpClientParams.setCookiePolicy(clientParams, CookiePolicy.IGNORE_COOKIES);
    connectionManager.setMaxTotal(DEFAULT_MAX_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_HOST_CONNECTIONS);
    HttpConnectionParams.setStaleCheckingEnabled(clientParams, false);
    return httpClient;
}
Also used : HttpParams(org.apache.http.params.HttpParams) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Aggregations

DefaultHttpRequestRetryHandler (org.apache.http.impl.client.DefaultHttpRequestRetryHandler)19 IOException (java.io.IOException)7 HttpContext (org.apache.http.protocol.HttpContext)5 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)4 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)4 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)4 HttpParams (org.apache.http.params.HttpParams)4 HttpClient (org.apache.http.client.HttpClient)3 RequestConfig (org.apache.http.client.config.RequestConfig)3 KeyManagementException (java.security.KeyManagementException)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ConnectionReuseStrategy (org.apache.http.ConnectionReuseStrategy)2 HttpHost (org.apache.http.HttpHost)2 HttpResponse (org.apache.http.HttpResponse)2 AuthScope (org.apache.http.auth.AuthScope)2 NTCredentials (org.apache.http.auth.NTCredentials)2 SocketConfig (org.apache.http.config.SocketConfig)2 NoConnectionReuseStrategy (org.apache.http.impl.NoConnectionReuseStrategy)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2