IT_Programming/Dev Libs & Framework

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

JJun ™ 2013. 3. 15. 17:53


출처: 

http://beyondj2ee.wordpress.com/2012/12/12/spring%EC%97%90%EC%84%9C-httpclient-pool-%EC%84%A4%EC%A0%95-%ED%95%98%EA%B8%B0-httpclient-connection-pool/





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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?xml version="1.0" encoding="UTF-8"?>
 
 
    <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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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;
    }
}