mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-18 16:17:30 +00:00
Updating http connection management.... Broken
This commit is contained in:
@@ -4,7 +4,12 @@ import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.http.HttpClientConnection;
|
||||
import org.apache.http.HttpException;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.config.CookieSpecs;
|
||||
@@ -13,10 +18,17 @@ 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.client.methods.HttpUriRequest;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.conn.ConnectionPoolTimeoutException;
|
||||
import org.apache.http.conn.ConnectionRequest;
|
||||
import org.apache.http.conn.HttpClientConnectionManager;
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
|
||||
import org.apache.http.protocol.HttpRequestExecutor;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -25,13 +37,22 @@ import com.bwssystems.HABridge.api.NameValue;
|
||||
|
||||
public class HTTPHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(HTTPHandler.class);
|
||||
private CloseableHttpClient httpClient;
|
||||
private RequestConfig globalConfig;
|
||||
|
||||
// private CloseableHttpClient httpClient;
|
||||
// private RequestConfig globalConfig;
|
||||
private HttpClientContext context;
|
||||
private HttpClientConnectionManager connMgr;
|
||||
private HttpRoute route;
|
||||
private HttpClientConnection conn;
|
||||
private HttpHost theHost;
|
||||
|
||||
public HTTPHandler() {
|
||||
globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
|
||||
httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();
|
||||
context = HttpClientContext.create();
|
||||
connMgr = new BasicHttpClientConnectionManager();
|
||||
route = null;
|
||||
conn = null;
|
||||
theHost = null;
|
||||
// globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
|
||||
// httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +64,7 @@ public class HTTPHandler {
|
||||
String theContent = null;
|
||||
URI theURI = null;
|
||||
ContentType parsedContentType = null;
|
||||
ConnectionRequest connRequest = null;
|
||||
StringEntity requestBody = null;
|
||||
if (contentType != null && !contentType.trim().isEmpty()) {
|
||||
parsedContentType = ContentType.parse(contentType);
|
||||
@@ -55,6 +77,44 @@ public class HTTPHandler {
|
||||
log.warn("Error creating URI http request: " + url + " with message: " + e1.getMessage());
|
||||
return null;
|
||||
}
|
||||
if(route == null) {
|
||||
theHost = new HttpHost(theURI.getHost(), theURI.getPort());
|
||||
route = new HttpRoute(theHost);
|
||||
}
|
||||
if(conn == null) {
|
||||
// Request new connection. This can be a long process
|
||||
connRequest = connMgr.requestConnection(route, null);
|
||||
// Wait for connection up to 10 sec
|
||||
try {
|
||||
conn = connRequest.get(10, TimeUnit.SECONDS);
|
||||
} catch (ConnectionPoolTimeoutException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// If not open
|
||||
if (!conn.isOpen()) {
|
||||
// establish connection based on its route info
|
||||
try {
|
||||
connMgr.connect(conn, route, 1000, context);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
// and mark it as route complete
|
||||
try {
|
||||
connMgr.routeComplete(conn, route, context);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (httpVerb == null || httpVerb.trim().isEmpty() || HttpGet.METHOD_NAME.equalsIgnoreCase(httpVerb)) {
|
||||
request = new HttpGet(theURI);
|
||||
@@ -82,14 +142,19 @@ public class HTTPHandler {
|
||||
}
|
||||
}
|
||||
HttpResponse response = null;
|
||||
HttpRequestExecutor exeRequest = new HttpRequestExecutor();
|
||||
context.setTargetHost(theHost);
|
||||
for (int retryCount = 0; retryCount < 2; retryCount++) {
|
||||
try {
|
||||
response = httpClient.execute(request);
|
||||
response = exeRequest.execute(request, conn, context);
|
||||
} catch (ClientProtocolException e) {
|
||||
log.warn("Client Protocol Exception received, retyring....");
|
||||
} catch (IOException e) {
|
||||
log.warn("Error calling out to HA gateway: IOException in log", e);
|
||||
retryCount = 2;
|
||||
} catch (HttpException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.debug((httpVerb == null ? "GET" : httpVerb) + " execute (" + retryCount + ") on URL responded: "
|
||||
+ response.getStatusLine().getStatusCode());
|
||||
@@ -130,6 +195,7 @@ public class HTTPHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
connMgr.releaseConnection(conn, null, 1, TimeUnit.SECONDS);
|
||||
return theContent;
|
||||
}
|
||||
|
||||
@@ -138,17 +204,20 @@ public class HTTPHandler {
|
||||
// }
|
||||
|
||||
|
||||
public CloseableHttpClient getHttpClient() {
|
||||
return httpClient;
|
||||
}
|
||||
// public CloseableHttpClient getHttpClient() {
|
||||
// return httpClient;
|
||||
// }
|
||||
|
||||
|
||||
public void closeHandler() {
|
||||
try {
|
||||
httpClient.close();
|
||||
// httpClient.close();
|
||||
conn.close();
|
||||
connMgr.closeExpiredConnections();
|
||||
connMgr.shutdown();
|
||||
} catch (IOException e) {
|
||||
// noop
|
||||
}
|
||||
httpClient = null;
|
||||
// httpClient = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user