Skip to content

Commit

Permalink
ZEPHYR-28948: http connection pool manager implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
pv-smartbear committed May 19, 2020
1 parent 260f4c4 commit 64ff415
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/com/thed/service/BaseService.java
Expand Up @@ -4,4 +4,7 @@
* Created by prashant on 25/6/19.
*/
public interface BaseService {

ZephyrRestService getZephyrRestService();

}
3 changes: 3 additions & 0 deletions src/main/java/com/thed/service/HttpClientService.java
Expand Up @@ -3,6 +3,7 @@
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -30,4 +31,6 @@ public interface HttpClientService {

void clear();

CloseableHttpClient getHttpClient();

}
2 changes: 2 additions & 0 deletions src/main/java/com/thed/service/ZephyrRestService.java
Expand Up @@ -262,6 +262,8 @@ public interface ZephyrRestService {

User getCurrentUser();

void closeHttpConnection() throws IOException;

/**
* Clears all data saved in this instance and related to this.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/thed/service/impl/BaseServiceImpl.java
Expand Up @@ -13,4 +13,8 @@ public class BaseServiceImpl implements BaseService {
public BaseServiceImpl() {
}

@Override
public ZephyrRestService getZephyrRestService() {
return zephyrRestService;
}
}
13 changes: 12 additions & 1 deletion src/main/java/com/thed/service/impl/HttpClientServiceImpl.java
@@ -1,6 +1,7 @@
package com.thed.service.impl;

import com.thed.service.HttpClientService;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
Expand All @@ -11,11 +12,14 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -36,7 +40,11 @@ public class HttpClientServiceImpl implements HttpClientService {
public HttpClientServiceImpl() {
cookieStore = new BasicCookieStore();
headers = new ArrayList<>();
httpClient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
HttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
httpClient = HttpClientBuilder.create()
.setConnectionManager(connectionManager)
.setDefaultCookieStore(cookieStore)
.build();
}

@Override
Expand Down Expand Up @@ -95,6 +103,7 @@ public String getRequest(String url) throws IOException {
}
} finally {
response.close();
httpGet.releaseConnection();
}

return result;
Expand Down Expand Up @@ -138,6 +147,7 @@ public String postRequest(String url, HttpEntity httpEntity) throws IOException
}
} finally {
response.close();
httpPost.releaseConnection();
}
return result;
}
Expand Down Expand Up @@ -172,6 +182,7 @@ public String putRequest(String url, String content) throws IOException {
}
} finally {
response.close();
httpPut.releaseConnection();
}
return result;
}
Expand Down
Expand Up @@ -574,6 +574,11 @@ public void setRestVersion(String restVersion) {
this.restVersion = restVersion;
}

@Override
public void closeHttpConnection() throws IOException {
httpClientService.getHttpClient().close();
}

@Override
public void clear() {
setCurrentUser(null);
Expand Down
Expand Up @@ -384,6 +384,8 @@ else if(zephyrConfigModel.getCycleDuration().equals("7 days")) {
logger.println(stackTraceElement.toString());
}
return false;
} finally {
userService.getZephyrRestService().closeHttpConnection();
}

logger.printf("%s Done uploading tests to Zephyr.%n", pInfo);
Expand Down

0 comments on commit 64ff415

Please sign in to comment.