Skip to content

Commit

Permalink
[openhabcloud] Create dedicated HttpClient from the factory to connec…
Browse files Browse the repository at this point in the history
…t to the local OpenHAB (Issue openhab#5770) (openhab#5773)

Signed-off-by: Pedro Garcia <pg@puzzle-star.com>
  • Loading branch information
puzzle-star authored and Pshatsillo committed Dec 8, 2019
1 parent e37cb12 commit 4c1657a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,6 @@ public class CloudClient {
* Logger for this class
*/
private Logger logger = LoggerFactory.getLogger(CloudClient.class);
/*
* This constant defines maximum number of HTTP connections per peer
* address for HTTP client which performs local connections to openHAB
*/
private static final int HTTP_CLIENT_MAX_CONNECTIONS_PER_DEST = 200;

/*
* This constant defines HTTP request timeout. It should be kept at about
* 30 seconds minimum to make it work for long polling requests
*/
private static final long HTTP_CLIENT_TIMEOUT = 30;

/*
* This variable holds base URL for the openHAB Cloud connections
Expand Down Expand Up @@ -330,7 +319,7 @@ private void handleRequestEvent(JSONObject data) {
if (data.has("protocol")) {
proto = data.getString("protocol");
}
request.timeout(HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS).header("X-Forwarded-Proto", proto);
request.header("X-Forwarded-Proto", proto);

if (requestMethod.equals("GET")) {
request.method(HttpMethod.GET);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public class CloudService implements ActionService, CloudClientListener, EventSu
private static final String CFG_MODE = "mode";
private static final String SECRET_FILE_NAME = "openhabcloud" + File.separator + "secret";
private static final String DEFAULT_URL = "https://myopenhab.org/";
private static final int DEFAULT_LOCAL_OPENHAB_MAX_CONCURRENT_REQUESTS = 200;
private static final int DEFAULT_LOCAL_OPENHAB_REQUEST_TIMEOUT = 30000;
private static final String HTTPCLIENT_NAME = "openhabcloud";

private Logger logger = LoggerFactory.getLogger(CloudService.class);

Expand Down Expand Up @@ -175,6 +178,11 @@ private void checkJavaVersion() {
protected void deactivate() {
logger.debug("openHAB Cloud connector deactivated");
cloudClient.shutdown();
try {
httpClient.stop();
} catch (Exception e) {
logger.debug("Could not stop Jetty http client", e);
}
}

@Modified
Expand Down Expand Up @@ -216,6 +224,17 @@ protected void modified(Map<String, ?> config) {
cloudClient.shutdown();
}

httpClient.setMaxConnectionsPerDestination(DEFAULT_LOCAL_OPENHAB_MAX_CONCURRENT_REQUESTS);
httpClient.setConnectTimeout(DEFAULT_LOCAL_OPENHAB_REQUEST_TIMEOUT);

if (!httpClient.isRunning()) {
try {
httpClient.start();
} catch (Exception e) {
logger.error("Could not start Jetty http client", e);
}
}

String localBaseUrl = "http://localhost:" + localPort;
cloudClient = new CloudClient(httpClient, InstanceUUID.get(), getSecret(), cloudBaseUrl, localBaseUrl,
remoteAccessEnabled, exposedItems);
Expand Down Expand Up @@ -332,7 +351,8 @@ public void sendCommand(String itemName, String commandString) {

@Reference
protected void setHttpClientFactory(HttpClientFactory httpClientFactory) {
this.httpClient = httpClientFactory.getCommonHttpClient();
this.httpClient = httpClientFactory.createHttpClient(HTTPCLIENT_NAME);
this.httpClient.setStopTimeout(0);
}

protected void unsetHttpClientFactory(HttpClientFactory httpClientFactory) {
Expand Down

0 comments on commit 4c1657a

Please sign in to comment.