Skip to content

Commit

Permalink
Use daemon threads for NodeChecker and IdleConnectionReaper.
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbode committed Dec 20, 2015
1 parent c7c3bcf commit 95a5fe5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.searchbox.client.config.discovery;

import com.google.common.util.concurrent.AbstractScheduledService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.searchbox.client.JestClient;
Expand All @@ -11,6 +13,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.LinkedHashSet;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -91,6 +96,28 @@ protected Scheduler scheduler() {
return scheduler;
}

@Override
protected ScheduledExecutorService executor() {
final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat(serviceName())
.build());
// Add a listener to shutdown the executor after the service is stopped. This ensures that the
// JVM shutdown will not be prevented from exiting after this service has stopped or failed.
// Technically this listener is added after start() was called so it is a little gross, but it
// is called within doStart() so we know that the service cannot terminate or fail concurrently
// with adding this listener so it is impossible to miss an event that we are interested in.
addListener(new Listener() {
@Override public void terminated(State from) {
executor.shutdown();
}
@Override public void failed(State from, Throwable failure) {
executor.shutdown();
}}, MoreExecutors.sameThreadExecutor());
return executor;
}

/**
* Converts the Elasticsearch reported publish address in the format "inet[<hostname>:<port>]" or
* "inet[<hostname>/<hostaddress>:<port>]" to a normalized http address in the form "http://host:port".
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package io.searchbox.client.config.idle;

import com.google.common.util.concurrent.AbstractScheduledService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.searchbox.client.config.ClientConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;

/**
* Used to reap idle connections from the connection manager.
*/
Expand Down Expand Up @@ -33,4 +39,26 @@ protected Scheduler scheduler() {
clientConfig.getMaxConnectionIdleTime(),
clientConfig.getMaxConnectionIdleTimeDurationTimeUnit());
}

@Override
protected ScheduledExecutorService executor() {
final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat(serviceName())
.build());
// Add a listener to shutdown the executor after the service is stopped. This ensures that the
// JVM shutdown will not be prevented from exiting after this service has stopped or failed.
// Technically this listener is added after start() was called so it is a little gross, but it
// is called within doStart() so we know that the service cannot terminate or fail concurrently
// with adding this listener so it is impossible to miss an event that we are interested in.
addListener(new Listener() {
@Override public void terminated(State from) {
executor.shutdown();
}
@Override public void failed(State from, Throwable failure) {
executor.shutdown();
}}, MoreExecutors.sameThreadExecutor());
return executor;
}
}

0 comments on commit 95a5fe5

Please sign in to comment.