Skip to content

Commit 5a89b5e

Browse files
author
duqi
committedFeb 9, 2017
在spring项目中使用httpclient
1 parent 092d60f commit 5a89b5e

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed
 

‎pom.xml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
</plugins>
2121
</build>
2222

23+
<properties>
24+
<spring.version>4.3.2.RELEASE</spring.version>
25+
</properties>
26+
2327
<dependencies>
2428
<dependency>
2529
<groupId>org.apache.httpcomponents</groupId>
@@ -47,17 +51,27 @@
4751
<dependency>
4852
<groupId>org.springframework</groupId>
4953
<artifactId>spring-core</artifactId>
50-
<version>4.2.4.RELEASE</version>
54+
<version>${spring.version}</version>
5155
</dependency>
5256
<dependency>
5357
<groupId>org.springframework</groupId>
5458
<artifactId>spring-beans</artifactId>
55-
<version>4.2.4.RELEASE</version>
59+
<version>${spring.version}</version>
5660
</dependency>
5761
<dependency>
5862
<groupId>org.springframework</groupId>
5963
<artifactId>spring-context</artifactId>
60-
<version>4.2.4.RELEASE</version>
64+
<version>${spring.version}</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.springframework</groupId>
68+
<artifactId>spring-test</artifactId>
69+
<version>${spring.version}</version>
70+
</dependency>
71+
<dependency>
72+
<groupId>junit</groupId>
73+
<artifactId>junit</artifactId>
74+
<version>4.12</version>
6175
</dependency>
6276

6377
<dependency>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.java.learn.httpclient;
2+
3+
import org.apache.commons.codec.Charsets;
4+
import org.apache.http.NoHttpResponseException;
5+
import org.apache.http.client.ClientProtocolException;
6+
import org.apache.http.client.HttpClient;
7+
import org.apache.http.client.config.RequestConfig;
8+
import org.apache.http.config.ConnectionConfig;
9+
import org.apache.http.conn.ConnectTimeoutException;
10+
import org.apache.http.impl.client.HttpClients;
11+
import org.springframework.beans.factory.FactoryBean;
12+
13+
import java.net.SocketTimeoutException;
14+
15+
/**
16+
* Created by IntelliJ IDEA.
17+
* User: duqi
18+
* Date: 2017/2/9
19+
* Time: 13:54
20+
*/
21+
public class HttpClientFactoryBean implements FactoryBean<HttpClient> {
22+
23+
private static final int DEFAULT_MAX_TOTAL = 512;
24+
private static final int DEFAULT_MAX_PER_ROUTE = 64;
25+
private static final int DEFAULT_CONNECTION_TIMEOUT = 5000;
26+
private static final int DEFAULT_SOCKET_TIMEOUT = 3000;
27+
28+
@Override
29+
public HttpClient getObject() throws Exception {
30+
ConnectionConfig config = ConnectionConfig.custom()
31+
.setCharset(Charsets.UTF_8)
32+
.build();
33+
34+
RequestConfig defaultRequestConfig = RequestConfig.custom()
35+
.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT)
36+
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
37+
.build();
38+
39+
return HttpClients.custom()
40+
.setMaxConnPerRoute(DEFAULT_MAX_PER_ROUTE)
41+
.setMaxConnTotal(DEFAULT_MAX_TOTAL)
42+
.setRetryHandler((exception, executionCount, context) -> executionCount <= 3 && (exception instanceof NoHttpResponseException
43+
|| exception instanceof ClientProtocolException
44+
|| exception instanceof SocketTimeoutException
45+
|| exception instanceof ConnectTimeoutException))
46+
.setDefaultConnectionConfig(config)
47+
.setDefaultRequestConfig(defaultRequestConfig)
48+
.build();
49+
}
50+
51+
@Override
52+
public Class<?> getObjectType() {
53+
return HttpClient.class;
54+
}
55+
56+
@Override
57+
public boolean isSingleton() {
58+
return true;
59+
}
60+
}

‎src/main/resources/applicationContext.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
66

77
<context:component-scan base-package="org.java.learn"/>
8+
9+
<bean id="httpClientFactoryBean" class="org.java.learn.httpclient.HttpClientFactoryBean"/>
10+
<!--<bean id="httpClient" factory-bean="httpClientFactoryBean"/>-->
811
</beans>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import org.apache.http.client.HttpClient;
2+
import org.junit.Assert;
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.test.context.ContextConfiguration;
6+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7+
8+
import javax.annotation.Resource;
9+
10+
/**
11+
* Created by IntelliJ IDEA.
12+
* User: duqi
13+
* Date: 2017/2/9
14+
* Time: 14:18
15+
*/
16+
@RunWith(SpringJUnit4ClassRunner.class)
17+
@ContextConfiguration(locations = "classpath:applicationContext.xml")
18+
public class HttpClientFactoryBeanXmlTest {
19+
20+
@Resource
21+
HttpClient httpClient;
22+
23+
@Test
24+
public void httpClientAutoWired() throws Exception {
25+
Assert.assertNotNull(httpClient);
26+
}
27+
}

0 commit comments

Comments
 (0)
Please sign in to comment.