Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
在spring项目中使用httpclient
- Loading branch information
duqi
committed
Feb 9, 2017
1 parent
092d60f
commit 5a89b5e
Showing
4 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/org/java/learn/httpclient/HttpClientFactoryBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.java.learn.httpclient; | ||
|
||
import org.apache.commons.codec.Charsets; | ||
import org.apache.http.NoHttpResponseException; | ||
import org.apache.http.client.ClientProtocolException; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.config.ConnectionConfig; | ||
import org.apache.http.conn.ConnectTimeoutException; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.springframework.beans.factory.FactoryBean; | ||
|
||
import java.net.SocketTimeoutException; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: duqi | ||
* Date: 2017/2/9 | ||
* Time: 13:54 | ||
*/ | ||
public class HttpClientFactoryBean implements FactoryBean<HttpClient> { | ||
|
||
private static final int DEFAULT_MAX_TOTAL = 512; | ||
private static final int DEFAULT_MAX_PER_ROUTE = 64; | ||
private static final int DEFAULT_CONNECTION_TIMEOUT = 5000; | ||
private static final int DEFAULT_SOCKET_TIMEOUT = 3000; | ||
|
||
@Override | ||
public HttpClient getObject() throws Exception { | ||
ConnectionConfig config = ConnectionConfig.custom() | ||
.setCharset(Charsets.UTF_8) | ||
.build(); | ||
|
||
RequestConfig defaultRequestConfig = RequestConfig.custom() | ||
.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT) | ||
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT) | ||
.build(); | ||
|
||
return HttpClients.custom() | ||
.setMaxConnPerRoute(DEFAULT_MAX_PER_ROUTE) | ||
.setMaxConnTotal(DEFAULT_MAX_TOTAL) | ||
.setRetryHandler((exception, executionCount, context) -> executionCount <= 3 && (exception instanceof NoHttpResponseException | ||
|| exception instanceof ClientProtocolException | ||
|| exception instanceof SocketTimeoutException | ||
|| exception instanceof ConnectTimeoutException)) | ||
.setDefaultConnectionConfig(config) | ||
.setDefaultRequestConfig(defaultRequestConfig) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Class<?> getObjectType() { | ||
return HttpClient.class; | ||
} | ||
|
||
@Override | ||
public boolean isSingleton() { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import org.apache.http.client.HttpClient; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import javax.annotation.Resource; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: duqi | ||
* Date: 2017/2/9 | ||
* Time: 14:18 | ||
*/ | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(locations = "classpath:applicationContext.xml") | ||
public class HttpClientFactoryBeanXmlTest { | ||
|
||
@Resource | ||
HttpClient httpClient; | ||
|
||
@Test | ||
public void httpClientAutoWired() throws Exception { | ||
Assert.assertNotNull(httpClient); | ||
} | ||
} |