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

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

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

import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.kaizen4j.common.httpclient.interceptor.GZIPHttpResponseInterceptor;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;

import java.util.Objects;
import java.util.concurrent.ThreadFactory;

/**
 * AsyncHttpClientProxy 的工厂 Bean。使用示例:
 *
 * 
 * {@code
 *  
 * 		
 * 		
 * 		
 * 	    
 * 	
 * }
 * 
*/ public class AsyncHttpClientProxyFactoryBean implements FactoryBean, DisposableBean { private CloseableHttpAsyncClient httpAsyncClient; private int maxPerRoute; // 单位毫秒 private int timeoutMills; /* 最大并发请求数量 */ private int maxConcurrencyCount; 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 getMaxConcurrencyCount() { return maxConcurrencyCount; } public void setMaxConcurrencyCount(int maxConcurrencyCount) { this.maxConcurrencyCount = maxConcurrencyCount; } @Override public AsyncHttpClientProxy getObject() throws Exception { Preconditions.checkArgument(0 != maxPerRoute && 0 != timeoutMills && 0 != maxConcurrencyCount); IOReactorConfig ioReactorConfig = IOReactorConfig.custom() .setIoThreadCount(Runtime.getRuntime().availableProcessors()).setConnectTimeout(timeoutMills) .setSoTimeout(timeoutMills).setSoKeepAlive(true).build(); DefaultConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig); PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(ioReactor); connectionManager.setDefaultMaxPerRoute(maxPerRoute); RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeoutMills).setConnectTimeout(timeoutMills) .setSocketTimeout(timeoutMills).setContentCompressionEnabled(true).build(); ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("AsyncHttpClientProxy-%d").build(); httpAsyncClient = HttpAsyncClients.custom().setThreadFactory(threadFactory) .setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build(); AsyncHttpClientProxy asyncHttpClientProxy = new AsyncHttpClientProxy(httpAsyncClient, maxConcurrencyCount); asyncHttpClientProxy.addResponseInterceptor(new GZIPHttpResponseInterceptor()); return asyncHttpClientProxy; } @Override public Class getObjectType() { return AsyncHttpClientProxy.class; } @Override public boolean isSingleton() { return true; } @Override public void destroy() throws Exception { if (Objects.nonNull(httpAsyncClient)) { httpAsyncClient.close(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy