|
27 | 27 |
|
28 | 28 | import java.io.Closeable;
|
29 | 29 | import java.io.IOException;
|
| 30 | +import java.security.AccessController; |
| 31 | +import java.security.PrivilegedAction; |
30 | 32 | import java.util.List;
|
31 | 33 | import java.util.concurrent.Executors;
|
32 | 34 | import java.util.concurrent.ScheduledExecutorService;
|
33 | 35 | import java.util.concurrent.ScheduledFuture;
|
| 36 | +import java.util.concurrent.ThreadFactory; |
34 | 37 | import java.util.concurrent.TimeUnit;
|
35 | 38 | import java.util.concurrent.atomic.AtomicBoolean;
|
| 39 | +import java.util.concurrent.atomic.AtomicInteger; |
36 | 40 |
|
37 | 41 | /**
|
38 | 42 | * Class responsible for sniffing nodes from some source (default is elasticsearch itself) and setting them to a provided instance of
|
|
45 | 49 | public class Sniffer implements Closeable {
|
46 | 50 |
|
47 | 51 | private static final Log logger = LogFactory.getLog(Sniffer.class);
|
| 52 | + private static final String SNIFFER_THREAD_NAME = "es_rest_client_sniffer"; |
48 | 53 |
|
49 | 54 | private final Task task;
|
50 | 55 |
|
@@ -79,7 +84,8 @@ private Task(HostsSniffer hostsSniffer, RestClient restClient, long sniffInterva
|
79 | 84 | this.restClient = restClient;
|
80 | 85 | this.sniffIntervalMillis = sniffIntervalMillis;
|
81 | 86 | this.sniffAfterFailureDelayMillis = sniffAfterFailureDelayMillis;
|
82 |
| - this.scheduledExecutorService = Executors.newScheduledThreadPool(1); |
| 87 | + SnifferThreadFactory threadFactory = new SnifferThreadFactory(SNIFFER_THREAD_NAME); |
| 88 | + this.scheduledExecutorService = Executors.newScheduledThreadPool(1, threadFactory); |
83 | 89 | scheduleNextRun(0);
|
84 | 90 | }
|
85 | 91 |
|
@@ -151,4 +157,34 @@ synchronized void shutdown() {
|
151 | 157 | public static SnifferBuilder builder(RestClient restClient) {
|
152 | 158 | return new SnifferBuilder(restClient);
|
153 | 159 | }
|
| 160 | + |
| 161 | + private static class SnifferThreadFactory implements ThreadFactory { |
| 162 | + |
| 163 | + private final AtomicInteger threadNumber = new AtomicInteger(1); |
| 164 | + private final String namePrefix; |
| 165 | + private final ThreadFactory originalThreadFactory; |
| 166 | + |
| 167 | + private SnifferThreadFactory(String namePrefix) { |
| 168 | + this.namePrefix = namePrefix; |
| 169 | + this.originalThreadFactory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { |
| 170 | + @Override |
| 171 | + public ThreadFactory run() { |
| 172 | + return Executors.defaultThreadFactory(); |
| 173 | + } |
| 174 | + }); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public Thread newThread(final Runnable r) { |
| 179 | + return AccessController.doPrivileged(new PrivilegedAction<Thread>() { |
| 180 | + @Override |
| 181 | + public Thread run() { |
| 182 | + Thread t = originalThreadFactory.newThread(r); |
| 183 | + t.setName(namePrefix + "[T#" + threadNumber.getAndIncrement() + "]"); |
| 184 | + t.setDaemon(true); |
| 185 | + return t; |
| 186 | + } |
| 187 | + }); |
| 188 | + } |
| 189 | + } |
154 | 190 | }
|
0 commit comments