30
30
import java .util .List ;
31
31
import java .util .Map ;
32
32
import java .util .concurrent .Executor ;
33
+ import java .util .concurrent .ScheduledExecutorService ;
33
34
import javax .annotation .Nullable ;
34
35
import javax .annotation .concurrent .ThreadSafe ;
35
36
@@ -412,6 +413,7 @@ public static final class Args {
412
413
private final ProxyDetector proxyDetector ;
413
414
private final SynchronizationContext syncContext ;
414
415
private final ServiceConfigParser serviceConfigParser ;
416
+ @ Nullable private final ScheduledExecutorService scheduledExecutorService ;
415
417
@ Nullable private final ChannelLogger channelLogger ;
416
418
@ Nullable private final Executor executor ;
417
419
@@ -420,12 +422,14 @@ private Args(
420
422
ProxyDetector proxyDetector ,
421
423
SynchronizationContext syncContext ,
422
424
ServiceConfigParser serviceConfigParser ,
425
+ @ Nullable ScheduledExecutorService scheduledExecutorService ,
423
426
@ Nullable ChannelLogger channelLogger ,
424
427
@ Nullable Executor executor ) {
425
428
this .defaultPort = checkNotNull (defaultPort , "defaultPort not set" );
426
429
this .proxyDetector = checkNotNull (proxyDetector , "proxyDetector not set" );
427
430
this .syncContext = checkNotNull (syncContext , "syncContext not set" );
428
431
this .serviceConfigParser = checkNotNull (serviceConfigParser , "serviceConfigParser not set" );
432
+ this .scheduledExecutorService = scheduledExecutorService ;
429
433
this .channelLogger = channelLogger ;
430
434
this .executor = executor ;
431
435
}
@@ -460,6 +464,25 @@ public SynchronizationContext getSynchronizationContext() {
460
464
return syncContext ;
461
465
}
462
466
467
+ /**
468
+ * Returns a {@link ScheduledExecutorService} for scheduling delayed tasks.
469
+ *
470
+ * <p>This service is a shared resource and is only meant for quick tasks. DO NOT block or run
471
+ * time-consuming tasks.
472
+ *
473
+ * <p>The returned service doesn't support {@link ScheduledExecutorService#shutdown shutdown()}
474
+ * and {@link ScheduledExecutorService#shutdownNow shutdownNow()}. They will throw if called.
475
+ *
476
+ * @since 1.26.0
477
+ */
478
+ @ ExperimentalApi ("https://github.com/grpc/grpc-java/issues/6454" )
479
+ public ScheduledExecutorService getScheduledExecutorService () {
480
+ if (scheduledExecutorService == null ) {
481
+ throw new IllegalStateException ("ScheduledExecutorService not set in Builder" );
482
+ }
483
+ return scheduledExecutorService ;
484
+ }
485
+
463
486
/**
464
487
* Returns the {@link ServiceConfigParser}.
465
488
*
@@ -501,6 +524,7 @@ public String toString() {
501
524
.add ("proxyDetector" , proxyDetector )
502
525
.add ("syncContext" , syncContext )
503
526
.add ("serviceConfigParser" , serviceConfigParser )
527
+ .add ("scheduledExecutorService" , scheduledExecutorService )
504
528
.add ("channelLogger" , channelLogger )
505
529
.add ("executor" , executor )
506
530
.toString ();
@@ -517,6 +541,7 @@ public Builder toBuilder() {
517
541
builder .setProxyDetector (proxyDetector );
518
542
builder .setSynchronizationContext (syncContext );
519
543
builder .setServiceConfigParser (serviceConfigParser );
544
+ builder .setScheduledExecutorService (scheduledExecutorService );
520
545
builder .setChannelLogger (channelLogger );
521
546
builder .setOffloadExecutor (executor );
522
547
return builder ;
@@ -541,6 +566,7 @@ public static final class Builder {
541
566
private ProxyDetector proxyDetector ;
542
567
private SynchronizationContext syncContext ;
543
568
private ServiceConfigParser serviceConfigParser ;
569
+ private ScheduledExecutorService scheduledExecutorService ;
544
570
private ChannelLogger channelLogger ;
545
571
private Executor executor ;
546
572
@@ -577,6 +603,16 @@ public Builder setSynchronizationContext(SynchronizationContext syncContext) {
577
603
return this ;
578
604
}
579
605
606
+ /**
607
+ * See {@link Args#getScheduledExecutorService}.
608
+ */
609
+ @ ExperimentalApi ("https://github.com/grpc/grpc-java/issues/6454" )
610
+ public Builder setScheduledExecutorService (
611
+ ScheduledExecutorService scheduledExecutorService ) {
612
+ this .scheduledExecutorService = checkNotNull (scheduledExecutorService );
613
+ return this ;
614
+ }
615
+
580
616
/**
581
617
* See {@link Args#getServiceConfigParser}. This is a required field.
582
618
*
@@ -618,7 +654,7 @@ public Args build() {
618
654
return
619
655
new Args (
620
656
defaultPort , proxyDetector , syncContext , serviceConfigParser ,
621
- channelLogger , executor );
657
+ scheduledExecutorService , channelLogger , executor );
622
658
}
623
659
}
624
660
}
0 commit comments