@@ -199,6 +199,7 @@ public static class Builder {
199
199
private InetAddress inetAddress = null ;
200
200
private InetSocketAddress inetSocketAddress = null ;
201
201
private HttpServer httpServer = null ;
202
+ private ExecutorService executorService = null ;
202
203
private CollectorRegistry registry = CollectorRegistry .defaultRegistry ;
203
204
private boolean daemon = false ;
204
205
private Predicate <String > sampleNameFilter ;
@@ -248,13 +249,26 @@ public Builder withInetSocketAddress(InetSocketAddress address) {
248
249
/**
249
250
* Use this httpServer. The {@code httpServer} is expected to already be bound to an address.
250
251
* Must not be called together with {@link #withPort(int)}, or {@link #withHostname(String)},
251
- * or {@link #withInetAddress(InetAddress)}, or {@link #withInetSocketAddress(InetSocketAddress)}.
252
+ * or {@link #withInetAddress(InetAddress)}, or {@link #withInetSocketAddress(InetSocketAddress)},
253
+ * or {@link #withExecutorService(ExecutorService)}.
252
254
*/
253
255
public Builder withHttpServer (HttpServer httpServer ) {
254
256
this .httpServer = httpServer ;
255
257
return this ;
256
258
}
257
259
260
+ /**
261
+ * Optional: ExecutorService used by the {@code httpServer}.
262
+ * Must not be called together with the {@link #withHttpServer(HttpServer)}.
263
+ *
264
+ * @param executorService
265
+ * @return
266
+ */
267
+ public Builder withExecutorService (ExecutorService executorService ) {
268
+ this .executorService = executorService ;
269
+ return this ;
270
+ }
271
+
258
272
/**
259
273
* By default, the {@link HTTPServer} uses non-daemon threads. Set this to {@code true} to
260
274
* run the {@link HTTPServer} with daemon threads.
@@ -323,12 +337,13 @@ public HTTPServer build() throws IOException {
323
337
}
324
338
325
339
if (httpServer != null ) {
340
+ assertNull (executorService , "cannot configure 'httpServer' and `executorService'" );
326
341
assertZero (port , "cannot configure 'httpServer' and 'port' at the same time" );
327
342
assertNull (hostname , "cannot configure 'httpServer' and 'hostname' at the same time" );
328
343
assertNull (inetAddress , "cannot configure 'httpServer' and 'inetAddress' at the same time" );
329
344
assertNull (inetSocketAddress , "cannot configure 'httpServer' and 'inetSocketAddress' at the same time" );
330
345
assertNull (httpsConfigurator , "cannot configure 'httpServer' and 'httpsConfigurator' at the same time" );
331
- return new HTTPServer (httpServer , registry , daemon , sampleNameFilterSupplier , authenticator );
346
+ return new HTTPServer (executorService , httpServer , registry , daemon , sampleNameFilterSupplier , authenticator );
332
347
} else if (inetSocketAddress != null ) {
333
348
assertZero (port , "cannot configure 'inetSocketAddress' and 'port' at the same time" );
334
349
assertNull (hostname , "cannot configure 'inetSocketAddress' and 'hostname' at the same time" );
@@ -350,7 +365,7 @@ public HTTPServer build() throws IOException {
350
365
httpServer = HttpServer .create (inetSocketAddress , 3 );
351
366
}
352
367
353
- return new HTTPServer (httpServer , registry , daemon , sampleNameFilterSupplier , authenticator );
368
+ return new HTTPServer (executorService , httpServer , registry , daemon , sampleNameFilterSupplier , authenticator );
354
369
}
355
370
356
371
private void assertNull (Object o , String msg ) {
@@ -371,7 +386,7 @@ private void assertZero(int i, String msg) {
371
386
* The {@code httpServer} is expected to already be bound to an address
372
387
*/
373
388
public HTTPServer (HttpServer httpServer , CollectorRegistry registry , boolean daemon ) throws IOException {
374
- this (httpServer , registry , daemon , null , null );
389
+ this (null , httpServer , registry , daemon , null , null );
375
390
}
376
391
377
392
/**
@@ -416,7 +431,7 @@ public HTTPServer(String host, int port) throws IOException {
416
431
this (new InetSocketAddress (host , port ), CollectorRegistry .defaultRegistry , false );
417
432
}
418
433
419
- private HTTPServer (HttpServer httpServer , CollectorRegistry registry , boolean daemon , Supplier <Predicate <String >> sampleNameFilterSupplier , Authenticator authenticator ) {
434
+ private HTTPServer (ExecutorService executorService , HttpServer httpServer , CollectorRegistry registry , boolean daemon , Supplier <Predicate <String >> sampleNameFilterSupplier , Authenticator authenticator ) {
420
435
if (httpServer .getAddress () == null )
421
436
throw new IllegalArgumentException ("HttpServer hasn't been bound to an address" );
422
437
@@ -434,8 +449,12 @@ private HTTPServer(HttpServer httpServer, CollectorRegistry registry, boolean da
434
449
if (authenticator != null ) {
435
450
mContext .setAuthenticator (authenticator );
436
451
}
437
- executorService = Executors .newFixedThreadPool (5 , NamedDaemonThreadFactory .defaultThreadFactory (daemon ));
438
- server .setExecutor (executorService );
452
+ if (executorService != null ) {
453
+ this .executorService = executorService ;
454
+ } else {
455
+ this .executorService = Executors .newFixedThreadPool (5 , NamedDaemonThreadFactory .defaultThreadFactory (daemon ));
456
+ }
457
+ server .setExecutor (this .executorService );
439
458
start (daemon );
440
459
}
441
460
0 commit comments