All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kaizen4j.common.httpclient.HttpClientProxyFactoryBean Maven / Gradle / Ivy

There is a newer version: 1.3.8.RELEASE
Show newest version
package org.kaizen4j.common.httpclient;

import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.kaizen4j.common.httpclient.interceptor.GZIPHttpResponseInterceptor;
import org.springframework.beans.factory.FactoryBean;

import com.google.common.base.Preconditions;

/**
 * HttpClientProxy 的工厂 Bean。使用示例:
 *
 * 
 * {@code
 *  
 * 		
 * 		
 * 		
 * 		
 * 	
 * }
 * 
*/ public final class HttpClientProxyFactoryBean implements FactoryBean { private int maxPerRoute; // 单位毫秒 private int timeoutMills; private int requestRetryCount; public int getMaxPerRoute() { return maxPerRoute; } public void setMaxPerRoute(int maxPerRoute) { this.maxPerRoute = maxPerRoute; } public int getTimeoutMills() { return timeoutMills; } public void setTimeoutMills(int timeoutMills) { this.timeoutMills = timeoutMills; } public int getRequestRetryCount() { return requestRetryCount; } public void setRequestRetryCount(int requestRetryCount) { this.requestRetryCount = requestRetryCount; } /* * @throws Exception * * @see org.springframework.beans.factory.FactoryBean#getObject() */ @Override public HttpClientProxy getObject() throws Exception { Preconditions.checkArgument(0 != maxPerRoute && 0 != timeoutMills); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setDefaultMaxPerRoute(maxPerRoute); RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeoutMills).setConnectTimeout(timeoutMills) .setSocketTimeout(timeoutMills).setContentCompressionEnabled(true).build(); boolean requestSentRetryEnabled = (0 != requestRetryCount) ? true : false; HttpRequestRetryHandler httpRequestRetryHandler = new StandardHttpRequestRetryHandler(requestRetryCount, requestSentRetryEnabled); CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig) .setConnectionManager(connectionManager).setRetryHandler(httpRequestRetryHandler) .addInterceptorFirst(new GZIPHttpResponseInterceptor()).build(); return new HttpClientProxy(httpClient); } /* * @see org.springframework.beans.factory.FactoryBean#getObjectType() */ @Override public Class getObjectType() { return HttpClientProxy.class; } /* * @see org.springframework.beans.factory.FactoryBean#isSingleton() */ @Override public boolean isSingleton() { return true; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy