diff options
Diffstat (limited to 'vespa-http-client/src/main/java/com/yahoo/vespa/http/client/FeedClientFactory.java')
-rw-r--r-- | vespa-http-client/src/main/java/com/yahoo/vespa/http/client/FeedClientFactory.java | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/FeedClientFactory.java b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/FeedClientFactory.java index 6095134b7a2..4d50905da7b 100644 --- a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/FeedClientFactory.java +++ b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/FeedClientFactory.java @@ -5,7 +5,9 @@ package com.yahoo.vespa.http.client; import com.yahoo.vespa.http.client.config.SessionParams; import com.yahoo.vespa.http.client.core.api.FeedClientImpl; -import static com.yahoo.vespa.http.client.SessionFactory.createTimeoutExecutor; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadFactory; /** * Factory for creating FeedClient. @@ -15,7 +17,7 @@ import static com.yahoo.vespa.http.client.SessionFactory.createTimeoutExecutor; public class FeedClientFactory { /** - * Creates a FeedClient. + * Creates a FeedClient. Call this sparingly: Feed clients are expensive and should be as long-lived as possible. * * @param sessionParams parameters for connection, hosts, cluster configurations and more. * @param resultCallback on each result, this callback is called. @@ -25,4 +27,31 @@ public class FeedClientFactory { return new FeedClientImpl(sessionParams, resultCallback, createTimeoutExecutor()); } + static ScheduledThreadPoolExecutor createTimeoutExecutor() { + ScheduledThreadPoolExecutor timeoutExecutor; + timeoutExecutor = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory("timeout-")); + timeoutExecutor.setRemoveOnCancelPolicy(true); + timeoutExecutor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); + timeoutExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); + return timeoutExecutor; + } + + private static class DaemonThreadFactory implements ThreadFactory { + + private final ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory(); + private final String prefix; + + private DaemonThreadFactory(String prefix) { + this.prefix = prefix; + } + + @Override + public Thread newThread(Runnable runnable) { + Thread t = defaultThreadFactory.newThread(runnable); + t.setDaemon(true); + t.setName(prefix + t.getName()); + return t; + } + } + } |