Skip to content

Commit

Permalink
Provide the means to configure any service threads created as daemon …
Browse files Browse the repository at this point in the history
…threads.
  • Loading branch information
jcferretti committed May 26, 2023
1 parent 0790996 commit d19ca52
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
28 changes: 27 additions & 1 deletion jetcd-core/src/main/java/io/etcd/jetcd/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import com.google.common.collect.Streams;

/**
* ClientBuilder knows how to create an Client instance.
* ClientBuilder knows how to create a Client instance.
*/
public final class ClientBuilder implements Cloneable {

Expand All @@ -74,6 +74,9 @@ public final class ClientBuilder implements Cloneable {
private Duration connectTimeout;
private boolean waitForReady = true;

// default matches vertx default and does not change behvior compared to earlier versions of jetcd.
private boolean useDaemonThread = false;

ClientBuilder() {
}

Expand Down Expand Up @@ -692,6 +695,29 @@ public ClientBuilder waitForReady(boolean waitForReady) {
return this;
}

/**
* Returns if any service threads created by this client will be setup as daemon threads.
*
* @return true if this client will create any necessary service threads as daemon threads.
*/
public boolean useDaemonThread() {
return useDaemonThread;
}

/**
* Configure whether any service threads created by this client will be setup as daemon threads.
* Defaults to false. Note if this is not explicitly set to true, a JVM that has created service threads
* through this client may not exit when/if its main thread exits.
*
* @param useDaemonThread true if any service threads created by this client should be setup as daemon threads.
* False by default to preserve backwards compatibility.
* @return this builder
*/
public ClientBuilder useDaemonThread(boolean useDaemonThread) {
this.useDaemonThread = useDaemonThread;
return this;
}

/**
* build a new Client.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.function.Function;
import java.util.function.Predicate;

import io.vertx.core.VertxOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -174,7 +175,7 @@ ManagedChannelBuilder<?> defaultChannelBuilder(String target) {
throw new IllegalArgumentException("At least one endpoint should be provided");
}
if (vertx == null) {
vertx = Vertx.vertx();
vertx = Vertx.vertx(new VertxOptions().setUseDaemonThread(builder.useDaemonThread()));
}
final VertxChannelBuilder channelBuilder = VertxChannelBuilder.forTarget(vertx, target);

Expand Down

0 comments on commit d19ca52

Please sign in to comment.