Spring에서 HttpClient + Pool 설정 하기 (HttpClient Connection Pool)

draft 1.0……………………………………..

http.xml (스프링 빈설정 파일)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:oxm="http://www.springframework.org/schema/oxm"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">


	<bean id="schemeRegistry" class="org.apache.http.conn.scheme.SchemeRegistry" />

	<bean id="plainSocketFactory" class="org.apache.http.conn.scheme.PlainSocketFactory"
		factory-method="getSocketFactory" />

	<bean id="sSLSocketFactory" class="org.apache.http.conn.ssl.SSLSocketFactory"
		factory-method="getSocketFactory" />

	<bean id="schemeHttp" class="org.apache.http.conn.scheme.Scheme">
		<constructor-arg index="0" value="http" />
		<constructor-arg index="1" ref="plainSocketFactory" />
		<constructor-arg index="2" type="int" value="80" />
	</bean>

	<bean id="schemeHttps" class="org.apache.http.conn.scheme.Scheme">
		<constructor-arg index="0" value="http" />
		<constructor-arg index="1" ref="sSLSocketFactory" />
		<constructor-arg index="2" type="int" value="443" />
	</bean>

	<bean id="configuratorSchemeRegistry"
		class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
		p:targetObject-ref="schemeRegistry" p:targetMethod="setItems">
		<property name="arguments">
			<map>
				<entry key="http" value-ref="schemeHttp" />
				<entry key="https" value-ref="schemeHttps" />
			</map>
		</property>
	</bean>

	<bean id="server1Host" class="org.apache.http.HttpHost">
		<constructor-arg index="0" value="beyondj2ee.cafe24.com" />
		<constructor-arg index="1" type="int" value="8080" />
	</bean>

	<bean id="server2Host" class="org.apache.http.HttpHost">
		<constructor-arg value="beyondj2ee.cafe24.com" />
		<constructor-arg type="int" value="9090" />
	</bean>

	<bean id="poolingClientConnectionManager"
		class="org.apache.http.impl.conn.PoolingClientConnectionManager"
		p:maxTotal="200" p:defaultMaxPerRoute="20" destroy-method="shutdown">
		<constructor-arg index="0" ref="schemeRegistry" />
	</bean>

	<bean class="pe.beyondj2ee.streetest.HostConnectionManagerBuilder"
		init-method="init" p:connPoolControl-ref="poolingClientConnectionManager">
		<property name="hostMap">
			<map>
				<entry key-ref="server1Host" value="20" />
				<entry key-ref="server2Host" value="5" />
			</map>
		</property>
	</bean>

	<!-- http parameter -->
	<bean id="httpParams" class="org.apache.http.params.BasicHttpParams" />

	<bean class="org.apache.http.params.HttpConnectionParamBean"
		p:connectionTimeout="180000" p:tcpNoDelay="false"
		p:staleCheckingEnabled="false" p:soTimeout="120000">
		<constructor-arg ref="httpParams" />
	</bean>

	<bean class="org.apache.http.params.HttpProtocolParamBean"
		p:contentCharset="UTF-8" p:useExpectContinue="true"
		p:userAgent="Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.1.1) Gecko/20090715 Firefox/17.0.1">
		<property name="version">
			<util:constant static-field="org.apache.http.HttpVersion.HTTP_1_1" />
		</property>
		<constructor-arg index="0" ref="httpParams"/>
	</bean>

	<!-- http client -->
	<bean id="httpPoolClient" class="org.apache.http.impl.client.DefaultHttpClient">
		<constructor-arg index="0" ref="poolingClientConnectionManager" />
		<constructor-arg index="1" ref="httpParams" />
	</bean>

	<bean id="httpNoPoolClient" class="org.apache.http.impl.client.DefaultHttpClient">
		<constructor-arg index="0" ref="httpParams" />
	</bean>
	

</beans>

HostConnectionManagerBuilder.java

package pe.beyondj2ee.streetest;

import java.util.Iterator;
import java.util.Map;

import org.apache.http.HttpHost;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.pool.ConnPoolControl;

/**
 * The Class HostConnectionManagerBuilder.
 */
public class HostConnectionManagerBuilder {

	/** The route. */
	ConnPoolControl connPoolControl;

	/** The route map. */
	Map<HttpHost, Integer> hostMap;

	/**
	 * registry HttpRoutes into ConnPoolControl .
	 * 
	 * @throws Exception the exception
	 */
	public void init() throws Exception {
		Iterator iterator = hostMap.entrySet().iterator();
		HttpHost host;
		Integer value = 0;
		
		while (iterator.hasNext()) {
			Map.Entry mapEntry = (Map.Entry) iterator.next();
			host = (HttpHost) mapEntry.getKey();
			value = (Integer)mapEntry.getValue();
			connPoolControl.setMaxPerRoute(new HttpRoute(host), value);
		}
	}

	/**
	 * Gets the conn pool control.
	 *
	 * @return the conn pool control
	 */
	public ConnPoolControl getConnPoolControl() {
		return connPoolControl;
	}

	/**
	 * Sets the conn pool control.
	 *
	 * @param connPoolControl the new conn pool control
	 */
	public void setConnPoolControl(ConnPoolControl connPoolControl) {
		this.connPoolControl = connPoolControl;
	}

	/**
	 * Gets the host map.
	 *
	 * @return the host map
	 */
	public Map<HttpHost, Integer> getHostMap() {
		return hostMap;
	}

	/**
	 * Sets the host map.
	 *
	 * @param hostMap the host map
	 */
	public void setHostMap(Map<HttpHost, Integer> hostMap) {
		this.hostMap = hostMap;
	}
}

Published by: beyondj2ee

Past SI AA, now I am pikicast developer in yellow mobile and daddy, Java, Spring, OpenSource, Application Architect :) Java Application Architect. mail : beyondj2ee@gmail.com twitter : twitter.com/beyondj2ee facebook : https:www.facebook.com/beyondj2ee blog: http:beyondj2ee.wordpress.com

Categories Architecture댓글 남기기

답글 남기기

아래 항목을 채우거나 오른쪽 아이콘 중 하나를 클릭하여 로그 인 하세요:

WordPress.com 로고

WordPress.com의 계정을 사용하여 댓글을 남깁니다. 로그아웃 / 변경 )

Twitter 사진

Twitter의 계정을 사용하여 댓글을 남깁니다. 로그아웃 / 변경 )

Facebook 사진

Facebook의 계정을 사용하여 댓글을 남깁니다. 로그아웃 / 변경 )

Google+ photo

Google+의 계정을 사용하여 댓글을 남깁니다. 로그아웃 / 변경 )

%s에 연결하는 중