|
13 | 13 | import java.util.Set; |
14 | 14 | import java.util.HashSet; |
15 | 15 | import java.util.concurrent.ExecutorService; |
| 16 | +import java.util.concurrent.ExecutionException; |
16 | 17 | import java.util.concurrent.Executors; |
| 18 | +import java.util.concurrent.FutureTask; |
| 19 | +import java.util.concurrent.ThreadFactory; |
17 | 20 | import java.util.zip.GZIPOutputStream; |
18 | 21 |
|
19 | 22 | import com.sun.net.httpserver.HttpHandler; |
@@ -108,36 +111,106 @@ protected static Set<String> parseQuery(String query) throws IOException { |
108 | 111 | return names; |
109 | 112 | } |
110 | 113 |
|
111 | | - protected HttpServer server; |
| 114 | + |
| 115 | + static class DaemonThreadFactory implements ThreadFactory { |
| 116 | + private ThreadFactory delegate; |
| 117 | + private final boolean daemon; |
| 118 | + |
| 119 | + DaemonThreadFactory(ThreadFactory delegate, boolean daemon) { |
| 120 | + this.delegate = delegate; |
| 121 | + this.daemon = daemon; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Thread newThread(Runnable r) { |
| 126 | + Thread t = delegate.newThread(r); |
| 127 | + t.setDaemon(daemon); |
| 128 | + return t; |
| 129 | + } |
| 130 | + |
| 131 | + static ThreadFactory defaultThreadFactory(boolean daemon) { |
| 132 | + return new DaemonThreadFactory(Executors.defaultThreadFactory(), daemon); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + protected final HttpServer server; |
112 | 137 | protected final ExecutorService executorService; |
113 | 138 |
|
114 | 139 |
|
115 | 140 | /** |
116 | 141 | * Start a HTTP server serving Prometheus metrics from the given registry. |
117 | 142 | */ |
118 | | - public HTTPServer(InetSocketAddress addr, CollectorRegistry registry) throws IOException { |
| 143 | + public HTTPServer(InetSocketAddress addr, CollectorRegistry registry, boolean daemon) throws IOException { |
119 | 144 | server = HttpServer.create(); |
120 | 145 | server.bind(addr, 3); |
121 | 146 | HttpHandler mHandler = new HTTPMetricHandler(registry); |
122 | 147 | server.createContext("/", mHandler); |
123 | 148 | server.createContext("/metrics", mHandler); |
124 | | - executorService = Executors.newFixedThreadPool(5); |
| 149 | + executorService = Executors.newFixedThreadPool(5, DaemonThreadFactory.defaultThreadFactory(daemon)); |
125 | 150 | server.setExecutor(executorService); |
126 | | - server.start(); |
| 151 | + start(daemon); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Start a HTTP server serving Prometheus metrics from the given registry using non-daemon threads. |
| 156 | + */ |
| 157 | + public HTTPServer(InetSocketAddress addr, CollectorRegistry registry) throws IOException { |
| 158 | + this(addr, registry, false); |
127 | 159 | } |
128 | 160 |
|
129 | 161 | /** |
130 | 162 | * Start a HTTP server serving the default Prometheus registry. |
131 | 163 | */ |
| 164 | + public HTTPServer(int port, boolean daemon) throws IOException { |
| 165 | + this(new InetSocketAddress(port), CollectorRegistry.defaultRegistry, daemon); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Start a HTTP server serving the default Prometheus registry using non-daemon threads. |
| 170 | + */ |
132 | 171 | public HTTPServer(int port) throws IOException { |
133 | | - this(new InetSocketAddress(port), CollectorRegistry.defaultRegistry); |
| 172 | + this(port, false); |
134 | 173 | } |
135 | 174 |
|
136 | 175 | /** |
137 | 176 | * Start a HTTP server serving the default Prometheus registry. |
138 | 177 | */ |
| 178 | + public HTTPServer(String host, int port, boolean daemon) throws IOException { |
| 179 | + this(new InetSocketAddress(host, port), CollectorRegistry.defaultRegistry, daemon); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Start a HTTP server serving the default Prometheus registry using non-daemon threads. |
| 184 | + */ |
139 | 185 | public HTTPServer(String host, int port) throws IOException { |
140 | | - this(new InetSocketAddress(host, port), CollectorRegistry.defaultRegistry); |
| 186 | + this(new InetSocketAddress(host, port), CollectorRegistry.defaultRegistry, false); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Start a HTTP server by making sure that its background thread inherit proper daemon flag. |
| 191 | + */ |
| 192 | + private void start(boolean daemon) { |
| 193 | + if (daemon == Thread.currentThread().isDaemon()) { |
| 194 | + server.start(); |
| 195 | + } else { |
| 196 | + FutureTask<Void> startTask = new FutureTask<Void>(new Runnable() { |
| 197 | + @Override |
| 198 | + public void run() { |
| 199 | + server.start(); |
| 200 | + } |
| 201 | + }, null); |
| 202 | + DaemonThreadFactory.defaultThreadFactory(daemon).newThread(startTask).start(); |
| 203 | + try { |
| 204 | + startTask.get(); |
| 205 | + } catch (ExecutionException e) { |
| 206 | + throw new RuntimeException("Unexpected exception on starting HTTPSever", e); |
| 207 | + } catch (InterruptedException e) { |
| 208 | + // This is possible only if the current tread has been interrupted, |
| 209 | + // but in real use cases this should not happen. |
| 210 | + // In any case, there is nothing to do, except to propagate interrupted flag. |
| 211 | + Thread.currentThread().interrupt(); |
| 212 | + } |
| 213 | + } |
141 | 214 | } |
142 | 215 |
|
143 | 216 | /** |
|
0 commit comments