blob: 4d50905da7b4407548ea537ab0b9c1ff18c3d80c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client;
import com.yahoo.vespa.http.client.config.SessionParams;
import com.yahoo.vespa.http.client.core.api.FeedClientImpl;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
/**
* Factory for creating FeedClient.
*
* @author dybis
*/
public class FeedClientFactory {
/**
* 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.
* @return newly created FeedClient API object.
*/
public static FeedClient create(SessionParams sessionParams, FeedClient.ResultCallback resultCallback) {
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;
}
}
}
|