Create HttpClient using PoolingHttpClientConnectionManager

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I have a java service class which will call a REST service and get response back.
It is working fine in developer environment where less requests are made, but I still wanted to get a review.
My service method call is like:
private String getResponse(Object request, String cUri)
String responseStr = StringUtils.EMPTY;
try
Gson requestJson = new GsonBuilder().disableHtmlEscaping().create();
String inputJson = requestJson.toJson(request);
logger.debug("Input JSON : " + inputJson);
responseStr = connect.getResponse(cUri, inputJson,config.getHttpClient());
logger.debug("Outut JSON for provided request" + responseStr);
catch (Exception e)
logger.error("Exception while getting response from service : " + e,e);
return responseStr;
Here config.getHttpClient()); is as follows:
public class Test
private static CloseableHttpClient httpClient;
private static PoolingHttpClientConnectionManager connectionManager;
public CloseableHttpClient getHttpClient()
if(httpClient != null)
return httpClient;
try
SSLContext sslContext = getSSLContext();
SSLConnectionSocketFactory trustSelfSignedSocketFactory = new SSLConnectionSocketFactory(
sslContext == null
? SSLContext.getDefault() : sslContext
,new String "TLSv1", "TLSv1.2", "TLSv1.1"
,null
,NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> socketFactoryRegistry= RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", trustSelfSignedSocketFactory)
.register("http", PlainConnectionSocketFactory.getSocketFactory()).build();
connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connectionManager.setMaxTotal(Integer.parseInt(props.getProperty(Constants._CONNECTION_COUNT)));
connectionManager.setDefaultMaxPerRoute(Integer.valueOf(props.getProperty(Constants._MAX_PER_ROUTE)));
httpClient = HttpClientBuilder
.create()
.setConnectionManager(connectionManager)
.setConnectionManagerShared(true)
.build();
catch (Exception e)
logger.error("Error in getting rest server connection" + e, e);
return httpClient;
private SSLContext getSSLContext()
//return sslContext;
And this Test class is initialized as Singleton bean.
And public String getResponse(String uri, String inputJson, CloseableHttpClient httpClient) is as follows:
public class ConnectionHelper
public String getResponse(String uri, String inputJson, CloseableHttpClient httpClient) throws Exception
String responseStr = StringUtils.EMPTY;
HttpPost request = new HttpPost(uri);
StringEntity params = new StringEntity(inputJson);
request.addHeader("Content-type", "application/json");
request.setEntity(params);
try(final CloseableHttpResponse response= httpClient.execute(request))
responseStr = EntityUtils.toString(response.getEntity(),"UTF-8");
catch(Exception e)
logger.error("Error while exeucting on request " + e,e);
finally
httpClient.close();
return responseStr;
java rest spring
add a comment |Â
up vote
2
down vote
favorite
I have a java service class which will call a REST service and get response back.
It is working fine in developer environment where less requests are made, but I still wanted to get a review.
My service method call is like:
private String getResponse(Object request, String cUri)
String responseStr = StringUtils.EMPTY;
try
Gson requestJson = new GsonBuilder().disableHtmlEscaping().create();
String inputJson = requestJson.toJson(request);
logger.debug("Input JSON : " + inputJson);
responseStr = connect.getResponse(cUri, inputJson,config.getHttpClient());
logger.debug("Outut JSON for provided request" + responseStr);
catch (Exception e)
logger.error("Exception while getting response from service : " + e,e);
return responseStr;
Here config.getHttpClient()); is as follows:
public class Test
private static CloseableHttpClient httpClient;
private static PoolingHttpClientConnectionManager connectionManager;
public CloseableHttpClient getHttpClient()
if(httpClient != null)
return httpClient;
try
SSLContext sslContext = getSSLContext();
SSLConnectionSocketFactory trustSelfSignedSocketFactory = new SSLConnectionSocketFactory(
sslContext == null
? SSLContext.getDefault() : sslContext
,new String "TLSv1", "TLSv1.2", "TLSv1.1"
,null
,NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> socketFactoryRegistry= RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", trustSelfSignedSocketFactory)
.register("http", PlainConnectionSocketFactory.getSocketFactory()).build();
connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connectionManager.setMaxTotal(Integer.parseInt(props.getProperty(Constants._CONNECTION_COUNT)));
connectionManager.setDefaultMaxPerRoute(Integer.valueOf(props.getProperty(Constants._MAX_PER_ROUTE)));
httpClient = HttpClientBuilder
.create()
.setConnectionManager(connectionManager)
.setConnectionManagerShared(true)
.build();
catch (Exception e)
logger.error("Error in getting rest server connection" + e, e);
return httpClient;
private SSLContext getSSLContext()
//return sslContext;
And this Test class is initialized as Singleton bean.
And public String getResponse(String uri, String inputJson, CloseableHttpClient httpClient) is as follows:
public class ConnectionHelper
public String getResponse(String uri, String inputJson, CloseableHttpClient httpClient) throws Exception
String responseStr = StringUtils.EMPTY;
HttpPost request = new HttpPost(uri);
StringEntity params = new StringEntity(inputJson);
request.addHeader("Content-type", "application/json");
request.setEntity(params);
try(final CloseableHttpResponse response= httpClient.execute(request))
responseStr = EntityUtils.toString(response.getEntity(),"UTF-8");
catch(Exception e)
logger.error("Error while exeucting on request " + e,e);
finally
httpClient.close();
return responseStr;
java rest spring
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a java service class which will call a REST service and get response back.
It is working fine in developer environment where less requests are made, but I still wanted to get a review.
My service method call is like:
private String getResponse(Object request, String cUri)
String responseStr = StringUtils.EMPTY;
try
Gson requestJson = new GsonBuilder().disableHtmlEscaping().create();
String inputJson = requestJson.toJson(request);
logger.debug("Input JSON : " + inputJson);
responseStr = connect.getResponse(cUri, inputJson,config.getHttpClient());
logger.debug("Outut JSON for provided request" + responseStr);
catch (Exception e)
logger.error("Exception while getting response from service : " + e,e);
return responseStr;
Here config.getHttpClient()); is as follows:
public class Test
private static CloseableHttpClient httpClient;
private static PoolingHttpClientConnectionManager connectionManager;
public CloseableHttpClient getHttpClient()
if(httpClient != null)
return httpClient;
try
SSLContext sslContext = getSSLContext();
SSLConnectionSocketFactory trustSelfSignedSocketFactory = new SSLConnectionSocketFactory(
sslContext == null
? SSLContext.getDefault() : sslContext
,new String "TLSv1", "TLSv1.2", "TLSv1.1"
,null
,NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> socketFactoryRegistry= RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", trustSelfSignedSocketFactory)
.register("http", PlainConnectionSocketFactory.getSocketFactory()).build();
connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connectionManager.setMaxTotal(Integer.parseInt(props.getProperty(Constants._CONNECTION_COUNT)));
connectionManager.setDefaultMaxPerRoute(Integer.valueOf(props.getProperty(Constants._MAX_PER_ROUTE)));
httpClient = HttpClientBuilder
.create()
.setConnectionManager(connectionManager)
.setConnectionManagerShared(true)
.build();
catch (Exception e)
logger.error("Error in getting rest server connection" + e, e);
return httpClient;
private SSLContext getSSLContext()
//return sslContext;
And this Test class is initialized as Singleton bean.
And public String getResponse(String uri, String inputJson, CloseableHttpClient httpClient) is as follows:
public class ConnectionHelper
public String getResponse(String uri, String inputJson, CloseableHttpClient httpClient) throws Exception
String responseStr = StringUtils.EMPTY;
HttpPost request = new HttpPost(uri);
StringEntity params = new StringEntity(inputJson);
request.addHeader("Content-type", "application/json");
request.setEntity(params);
try(final CloseableHttpResponse response= httpClient.execute(request))
responseStr = EntityUtils.toString(response.getEntity(),"UTF-8");
catch(Exception e)
logger.error("Error while exeucting on request " + e,e);
finally
httpClient.close();
return responseStr;
java rest spring
I have a java service class which will call a REST service and get response back.
It is working fine in developer environment where less requests are made, but I still wanted to get a review.
My service method call is like:
private String getResponse(Object request, String cUri)
String responseStr = StringUtils.EMPTY;
try
Gson requestJson = new GsonBuilder().disableHtmlEscaping().create();
String inputJson = requestJson.toJson(request);
logger.debug("Input JSON : " + inputJson);
responseStr = connect.getResponse(cUri, inputJson,config.getHttpClient());
logger.debug("Outut JSON for provided request" + responseStr);
catch (Exception e)
logger.error("Exception while getting response from service : " + e,e);
return responseStr;
Here config.getHttpClient()); is as follows:
public class Test
private static CloseableHttpClient httpClient;
private static PoolingHttpClientConnectionManager connectionManager;
public CloseableHttpClient getHttpClient()
if(httpClient != null)
return httpClient;
try
SSLContext sslContext = getSSLContext();
SSLConnectionSocketFactory trustSelfSignedSocketFactory = new SSLConnectionSocketFactory(
sslContext == null
? SSLContext.getDefault() : sslContext
,new String "TLSv1", "TLSv1.2", "TLSv1.1"
,null
,NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> socketFactoryRegistry= RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", trustSelfSignedSocketFactory)
.register("http", PlainConnectionSocketFactory.getSocketFactory()).build();
connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connectionManager.setMaxTotal(Integer.parseInt(props.getProperty(Constants._CONNECTION_COUNT)));
connectionManager.setDefaultMaxPerRoute(Integer.valueOf(props.getProperty(Constants._MAX_PER_ROUTE)));
httpClient = HttpClientBuilder
.create()
.setConnectionManager(connectionManager)
.setConnectionManagerShared(true)
.build();
catch (Exception e)
logger.error("Error in getting rest server connection" + e, e);
return httpClient;
private SSLContext getSSLContext()
//return sslContext;
And this Test class is initialized as Singleton bean.
And public String getResponse(String uri, String inputJson, CloseableHttpClient httpClient) is as follows:
public class ConnectionHelper
public String getResponse(String uri, String inputJson, CloseableHttpClient httpClient) throws Exception
String responseStr = StringUtils.EMPTY;
HttpPost request = new HttpPost(uri);
StringEntity params = new StringEntity(inputJson);
request.addHeader("Content-type", "application/json");
request.setEntity(params);
try(final CloseableHttpResponse response= httpClient.execute(request))
responseStr = EntityUtils.toString(response.getEntity(),"UTF-8");
catch(Exception e)
logger.error("Error while exeucting on request " + e,e);
finally
httpClient.close();
return responseStr;
java rest spring
edited Jun 27 at 10:06
Vogel612â¦
20.9k345124
20.9k345124
asked Jun 27 at 9:13
user2018303
132
132
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197336%2fcreate-httpclient-using-poolinghttpclientconnectionmanager%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password