Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
used the shared threadpool for the shared client (#6588)
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Kreuzer <kai@openhab.org>
  • Loading branch information
kaikreuzer authored and htreu committed Nov 28, 2018
1 parent f703b10 commit 5f21515
Showing 1 changed file with 24 additions and 16 deletions.
Expand Up @@ -129,14 +129,14 @@ public HttpClient createHttpClient(String consumerName, String endpoint) {
Objects.requireNonNull(endpoint, "endpoint must not be null");
logger.debug("http client for endpoint {} requested", endpoint);
checkConsumerName(consumerName);
return createHttpClientInternal(consumerName, endpoint, false);
return createHttpClientInternal(consumerName, endpoint, false, null);
}

@Override
public HttpClient createHttpClient(String consumerName) {
logger.debug("http client for consumer {} requested", consumerName);
checkConsumerName(consumerName);
return createHttpClientInternal(consumerName, null, false);
return createHttpClientInternal(consumerName, null, false, null);
}

@Override
Expand All @@ -145,14 +145,14 @@ public WebSocketClient createWebSocketClient(String consumerName, String endpoin
Objects.requireNonNull(endpoint, "endpoint must not be null");
logger.debug("web socket client for endpoint {} requested", endpoint);
checkConsumerName(consumerName);
return createWebSocketClientInternal(consumerName, endpoint, false);
return createWebSocketClientInternal(consumerName, endpoint, false, null);
}

@Override
public WebSocketClient createWebSocketClient(String consumerName) {
logger.debug("web socket client for consumer {} requested", consumerName);
checkConsumerName(consumerName);
return createWebSocketClientInternal(consumerName, null, false);
return createWebSocketClientInternal(consumerName, null, false, null);
}

@Override
Expand Down Expand Up @@ -215,12 +215,12 @@ private synchronized void initialize() {
}

if (commonHttpClient == null) {
commonHttpClient = createHttpClientInternal("common", null, true);
commonHttpClient = createHttpClientInternal("common", null, true, threadPool);
logger.debug("Jetty shared http client created");
}

if (commonWebSocketClient == null) {
commonWebSocketClient = createWebSocketClientInternal("common", null, true);
commonWebSocketClient = createWebSocketClientInternal("common", null, true, threadPool);
logger.debug("Jetty shared web socket client created");
}

Expand All @@ -239,19 +239,24 @@ private synchronized void initialize() {
}
}

private HttpClient createHttpClientInternal(String consumerName, @Nullable String endpoint, boolean startClient) {
private HttpClient createHttpClientInternal(String consumerName, @Nullable String endpoint, boolean startClient,
@Nullable QueuedThreadPool threadPool) {
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<HttpClient>() {
@Override
public HttpClient run() {
logger.debug("creating http client for consumer {}, endpoint {}", consumerName, endpoint);

HttpClient httpClient = new HttpClient(createSslContextFactory(endpoint));
final QueuedThreadPool queuedThreadPool = createThreadPool(consumerName, minThreadsCustom,
maxThreadsCustom, keepAliveTimeoutCustom);

httpClient.setMaxConnectionsPerDestination(2);
httpClient.setExecutor(queuedThreadPool);

if (threadPool != null) {
httpClient.setExecutor(threadPool);
} else {
final QueuedThreadPool queuedThreadPool = createThreadPool(consumerName, minThreadsCustom,
maxThreadsCustom, keepAliveTimeoutCustom);
httpClient.setExecutor(queuedThreadPool);
}

if (startClient) {
try {
Expand All @@ -277,18 +282,21 @@ public HttpClient run() {
}

private WebSocketClient createWebSocketClientInternal(String consumerName, @Nullable String endpoint,
boolean startClient) {
boolean startClient, @Nullable QueuedThreadPool threadPool) {
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<WebSocketClient>() {
@Override
public WebSocketClient run() {
logger.debug("creating web socket client for consumer {}, endpoint {}", consumerName, endpoint);

WebSocketClient webSocketClient = new WebSocketClient(createSslContextFactory(endpoint));
final QueuedThreadPool queuedThreadPool = createThreadPool(consumerName, minThreadsCustom,
maxThreadsCustom, keepAliveTimeoutCustom);

webSocketClient.setExecutor(queuedThreadPool);
if (threadPool != null) {
webSocketClient.setExecutor(threadPool);
} else {
final QueuedThreadPool queuedThreadPool = createThreadPool(consumerName, minThreadsCustom,
maxThreadsCustom, keepAliveTimeoutCustom);
webSocketClient.setExecutor(queuedThreadPool);
}

if (startClient) {
try {
Expand Down

0 comments on commit 5f21515

Please sign in to comment.