CloseableHttpClient session not closed - java

I create a CloseableHttpClient with intent to connect to our Tomcat to execute some services.
After the request i call close on the client but I still see that there a active Session in Tomcat.
Should not close invalidate the session?
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(1000 * 60 * 2)
.setConnectTimeout(4000)
.build();
HttpClientBuilder cl = HttpClients.custom();
cl.setDefaultRequestConfig(requestConfig);
cl.setSSLSocketFactory(makeSSLConnectionSocketFactory());
HttpGet postMethod = new HttpGet(url);
try (CloseableHttpClient client = cl.build();
CloseableHttpResponse httpResponse = client.execute(postMethod);) {
} catch (IOException e) {
//e.printStackTrace();
}

Related

HTTPClient hung or slow in multi-threaded environment

I am using following version of httpclient api to call the rest webservice.
httpcore4.4.9
httpclient 4.5.9
I am using the ExecutorService to execute the task where it is calling my following methods to get the data from rest API. ExecutorService execute about 30+ rest call in one transaction and i observed that, httpclient HUNG or SLOW for few of the rest call and it hit the performance issue. Can you please check if below i am using httpclient correct way ?
String output = null;
CloseableHttpClient httpClient = getHttpClients();
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
HttpEntity entity = response.getEntity();
output = EntityUtils.toString(entity);
EntityUtils.consume(response.getEntity());
} catch (Exception e) {
log.error("error during call to REST API " + httpGet.getURI() + " ", e);
} finally {
httpClient.close();
httpGet.releaseConnection();
httpGet.abort();
}
return output;
}
private CloseableHttpClient getHttpClients() {
long startTime = System.currentTimeMillis();
long endTime = 0;
int timeout = 12000;
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setConnectTimeout(timeout);
requestBuilder.setConnectionRequestTimeout(timeout);
SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(timeout).build();
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setDefaultRequestConfig(requestBuilder.build());
builder.disableContentCompression();
builder.setDefaultSocketConfig(socketConfig);
CloseableHttpClient httpClient = builder
.build();
endTime = System.currentTimeMillis();
log.debug("Total time took to build client = " + (endTime - startTime));
return httpClient;
} ```
As per my experience, this problem is occurring because of EntityUtils.consume(response.getEntity()) .
You could find what it actually does here.
Try removing and running your code to match the speed and decide if you want it or not.
Below is the code used by me:
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpGet request = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(request);
String responseString = EntityUtils.toString(response.getEntity());
} catch (Exception ex) {
log.error(ex.toString());
}
Try to adjust MaxTotal and MaxPerRoute variable in PoolingHttpClientConnectionManager class.
connectionManager.setMaxTotal(maxTotalConnections);
connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute);

Java socket is closed - When posting SOAP request

first time posting a question on this site. I've been working on a project where I have to generate a signed(Header x509) SOAP request and submit it to an https URL where the webservice resides. Thus far I've been able to generate the Signed SOAP request, but I'm getting a socket is closed exception when I post the request. Can anyone help me figure out what is my code missing that could be causing this error. Thank you in advance!
Below is my code for how I'm posting the request, and the error:
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(new File("my keystore"), "mypassword".toCharArray(),
new TrustSelfSignedStrategy())
.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(300 * 1000)
.setConnectTimeout(300 * 1000)
.setConnectionRequestTimeout(300 * 1000)
.build();
HttpClient httpclient = HttpClients.custom() //CloseableHttpClient
.setDefaultRequestConfig(requestConfig)
.setSSLSocketFactory(sslsf)
.build();
HttpPost httpPost;
httpPost = new HttpPost("https://my webservice");
httpPost.setHeader("Content-type", "test/xml");
File input = new File(esbXmlFile);
InputStream inputStream = new FileInputStream(input);
InputStreamEntity inputStreamEntity = new InputStreamEntity(inputStream);
httpPost.setEntity(inputStreamEntity);
String content = null;
int statusCode = 1; //statusCode is response code form WS.
try
{
HttpResponse response = httpclient.execute(httpPost);
statusCode = response.getStatusLine().getStatusCode();
System.out.println("statusCode : " + statusCode);
HttpEntity entity = response.getEntity();
content = EntityUtils.toString(entity);
}
catch (SocketException e) {
e.getStackTrace();
}
catch (IOException e) {
e.getStackTrace();
}
I get this error:
DEBUG (main) [org.apache.http.conn.ssl.SSLConnectionSocketFactory] - Starting handshake
2016-11-28 16:24:04,019 DEBUG (main) [org.apache.http.impl.conn.DefaultManagedHttpClientConnection] - http-outgoing-1: Shutdown connection
2016-11-28 16:24:04,019 DEBUG (main) [org.apache.http.impl.execchain.MainClientExec] - Socket is closed
java.net.SocketException: Socket is closed
at java.net.Socket.setSoLinger(Socket.java:986)
at org.apache.http.impl.BHttpConnectionBase.shutdown(BHttpConnectionBase.java:305)
at org.apache.http.impl.conn.DefaultManagedHttpClientConnection.shutdown(DefaultManagedHttpClientConnection.java:97)
at org.apache.http.impl.conn.LoggingManagedHttpClientConnection.shutdown(LoggingManagedHttpClientConnection.java:89)
at org.apache.http.impl.conn.CPoolEntry.shutdownConnection(CPoolEntry.java:74)
at org.apache.http.impl.conn.CPoolProxy.shutdown(CPoolProxy.java:96)
at org.apache.http.impl.execchain.ConnectionHolder.abortConnection(ConnectionHolder.java:127)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:352)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
Maybe you have to set:
httpPost.setHeader("Content-type", "test/xml");
into
httpPost.setHeader("Content-type", "text/xml");
difference is test/xml into text/xml

HttpClient and Connection Timeout

I send http requests over Apache HttpClient and my code is here:
HttpHost proxy = new HttpHost("78.1.1.222", 80);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
httpClient = HttpClients.custom()
.setRoutePlanner(routePlanner)
.build();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Authorization","Basic " + encoding);
httpGet.addHeader("Cache-Control", "no-cache");
httpResponse = httpClient.execute(httpGet);
responseCode=httpResponse.getStatusLine().getStatusCode();
........
........(code continue..)
My question is that how can I add connection timeout time to this code?
Note that I must use proxy with that and I use HttpClient 4.4 .
http://www.baeldung.com/httpclient-timeout explains various ways to set the connection timeout.

HttpGet working in SE application but not in Container

My test application works when running it from a SE application but when using the same code in a web based application it refuses to work.
example code:
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
HttpHost target = new HttpHost("test.test.com", 443, "https");
HttpHost proxy = new HttpHost("41.41.41.41", 3128, "http");
final RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000).setProxy(proxy)
.build();
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.setDefaultRequestConfig(requestConfig)
.build();
HttpGet httpGet = new HttpGet(validateUrl);
httpGet.setHeader("Authorization", "Basic " + authenticationStringEncrypted);
CloseableHttpResponse response = httpclient.execute(target, httpGet);
In the web application it gets to this part:
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.setDefaultRequestConfig(requestConfig)
.build();
Then it stops, no exceptions are thrown and this block is surrounded with a try-catch, using multicatch to catch all exceptions.
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException | IOException ex) {
ex.printStackTrace();
}
The web based application is deployed to glassfish 4.1.

How to force Apache HttpClient / HttpConnection to absolutely close tcp connection?

I use lot of http connection with Apache HttpClient (org.apache.httpcomponents 4.3.6) to test servers and I cannot force connections to close, even when I schedule HttpConnectionManager to httpClient.getConnectionManager().shutdown(); after 10 seconds in another thread.
httpClient.close() also doesn't help.
Connections can last for minutes or even hours.
I have tried custom SocketConfig and this also not helps:
SocketConfig socketConfig = SocketConfig.custom()
.setSoKeepAlive(false)
.setSoLinger(5)
.setSoReuseAddress(true)
.setSoTimeout(5000)
.setTcpNoDelay(true).build();
The way I am fetching the content:
HttpUriRequest request = new HttpGet(url);
HttpResponse response = httpClient.execute(request);
try (InputStream in = response.getEntity().getContent()) {
String result = IOUtils.toString(in, StandardCharsets.UTF_8);
httpClient.close();
return result;
}
The way I am building HTTP Client:
SocketConfig socketConfig = SocketConfig.custom()
.setSoKeepAlive(false)
.setSoLinger(configuration.getInt("proxy.list.socket.so.linger"))
.setSoReuseAddress(true)
.setSoTimeout(configuration.getInt("proxy.list.socket.so.timeout"))
.setTcpNoDelay(true).build();
HttpClientBuilder builder = HttpClientBuilder.create();
builder.disableAutomaticRetries();
builder.disableContentCompression();
builder.disableCookieManagement();
builder.disableRedirectHandling();
builder.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
builder.setDefaultSocketConfig(socketConfig);
One of my prototypes of doing shutdown:
shutdownExecutor.schedule(() -> {
httpClient.getConnectionManager().closeExpiredConnections();
httpClient.getConnectionManager().closeIdleConnections(1, TimeUnit.SECONDS);
httpClient.getConnectionManager().shutdown();
httpClient.notifyAll();
try {
httpClient.close();
} catch (IOException ex) {
}
}, configuration.getInt("proxy.test.forced.timeout.seconds"), TimeUnit.SECONDS);
String content = HttpContentFetcher.getAndCloseClient(url, httpClient);
RequestConfig has helped. Now it looks like all the connections are discarded in specified limits.
RequestConfig config= RequestConfig.custom()
.setCircularRedirectsAllowed(false)
.setConnectionRequestTimeout(4000)
.setConnectTimeout(4000)
.setMaxRedirects(0)
.setRedirectsEnabled(false)
.setSocketTimeout(4000)
.setStaleConnectionCheckEnabled(true).build();
request.setConfig(config);

Categories