Browse Source

[Improvement][common] HttpClient singleton mode #3332 (#3350)

* Optimize the httpclient

* Optimize the httpclient singleton

* Merge branch 'github-local/dev' into feature/httpClientsingleton

# Conflicts:
#	dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HttpUtils.java
#	dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HttpUtilsTest.java

* Merge branch 'github-local/dev' into feature/httpClientsingleton

# Conflicts:
#	dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HttpUtils.java
#	dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HttpUtilsTest.java

* Merge branch 'github-local/dev' into feature/httpClientsingleton

# Conflicts:
#	dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HttpUtils.java
#	dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HttpUtilsTest.java
pull/3/MERGE
felix.wang 4 years ago committed by GitHub
parent
commit
b555d0a086
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HttpUtils.java
  2. 4
      dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HttpUtilsTest.java

20
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HttpUtils.java

@ -53,13 +53,19 @@ public class HttpUtils {
public static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); public static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
private HttpUtils() {
private static CloseableHttpClient httpClient; }
private HttpUtils() { public static CloseableHttpClient getInstance(){
return HttpClientInstance.httpClient;
}
private static class HttpClientInstance{
private static final CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(requestConfig).build();
} }
private static PoolingHttpClientConnectionManager cm; private static PoolingHttpClientConnectionManager cm;
private static SSLContext ctx = null; private static SSLContext ctx = null;
@ -112,20 +118,14 @@ public class HttpUtils {
} }
public static synchronized CloseableHttpClient getHttpClient() {
if (null == httpClient) {
httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(requestConfig).build();
;
}
return httpClient;
}
/** /**
* get http request content * get http request content
* @param url url * @param url url
* @return http get request response content * @return http get request response content
*/ */
public static String get(String url){ public static String get(String url){
CloseableHttpClient httpclient = HttpUtils.getHttpClient(); CloseableHttpClient httpclient = HttpUtils.getInstance();
HttpGet httpget = new HttpGet(url); HttpGet httpget = new HttpGet(url);
String responseContent = null; String responseContent = null;

4
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HttpUtilsTest.java

@ -46,8 +46,8 @@ public class HttpUtilsTest {
@Test @Test
public void testGetHttpClient() { public void testGetHttpClient() {
CloseableHttpClient httpClient1 = HttpUtils.getHttpClient(); CloseableHttpClient httpClient1 = HttpUtils.getInstance();
CloseableHttpClient httpClient2 = HttpUtils.getHttpClient(); CloseableHttpClient httpClient2 = HttpUtils.getInstance();
Assert.assertEquals(httpClient1, httpClient2); Assert.assertEquals(httpClient1, httpClient2);
} }
} }

Loading…
Cancel
Save