@@ -471,13 +471,17 @@ public ServiceRpc create(SpannerOptions options) {
471
471
private static final AtomicInteger DEFAULT_POOL_COUNT = new AtomicInteger ();
472
472
473
473
/** {@link ExecutorProvider} that is used for {@link AsyncResultSet}. */
474
- interface CloseableExecutorProvider extends ExecutorProvider , AutoCloseable {
474
+ public interface CloseableExecutorProvider extends ExecutorProvider , AutoCloseable {
475
475
/** Overridden to suppress the throws declaration of the super interface. */
476
476
@ Override
477
477
void close ();
478
478
}
479
479
480
- static class FixedCloseableExecutorProvider implements CloseableExecutorProvider {
480
+ /**
481
+ * Implementation of {@link CloseableExecutorProvider} that uses a fixed single {@link
482
+ * ScheduledExecutorService}.
483
+ */
484
+ public static class FixedCloseableExecutorProvider implements CloseableExecutorProvider {
481
485
private final ScheduledExecutorService executor ;
482
486
483
487
private FixedCloseableExecutorProvider (ScheduledExecutorService executor ) {
@@ -500,7 +504,7 @@ public boolean shouldAutoClose() {
500
504
}
501
505
502
506
/** Creates a FixedCloseableExecutorProvider. */
503
- static FixedCloseableExecutorProvider create (ScheduledExecutorService executor ) {
507
+ public static FixedCloseableExecutorProvider create (ScheduledExecutorService executor ) {
504
508
return new FixedCloseableExecutorProvider (executor );
505
509
}
506
510
}
@@ -516,8 +520,19 @@ static CloseableExecutorProvider createDefaultAsyncExecutorProvider() {
516
520
return createAsyncExecutorProvider (8 , 60L , TimeUnit .SECONDS );
517
521
}
518
522
519
- @ VisibleForTesting
520
- static CloseableExecutorProvider createAsyncExecutorProvider (
523
+ /**
524
+ * Creates a {@link CloseableExecutorProvider} that can be used as an {@link ExecutorProvider} for
525
+ * the async API. The {@link ExecutorProvider} will lazily create up to poolSize threads. The
526
+ * backing threads will automatically be shutdown if they have not been used during the keep-alive
527
+ * time. The backing threads are created as daemon threads.
528
+ *
529
+ * @param poolSize the maximum number of threads to create in the pool
530
+ * @param keepAliveTime the time that an unused thread in the pool should be kept alive
531
+ * @param unit the time unit used for the keepAliveTime
532
+ * @return a {@link CloseableExecutorProvider} that can be used for {@link
533
+ * SpannerOptions.Builder#setAsyncExecutorProvider(CloseableExecutorProvider)}
534
+ */
535
+ public static CloseableExecutorProvider createAsyncExecutorProvider (
521
536
int poolSize , long keepAliveTime , TimeUnit unit ) {
522
537
String format =
523
538
String .format ("spanner-async-pool-%d-thread-%%d" , DEFAULT_POOL_COUNT .incrementAndGet ());
@@ -1018,6 +1033,26 @@ public Builder setCompressorName(@Nullable String compressorName) {
1018
1033
return this ;
1019
1034
}
1020
1035
1036
+ /**
1037
+ * Sets the {@link ExecutorProvider} to use for high-level async calls that need an executor,
1038
+ * such as fetching results for an {@link AsyncResultSet}.
1039
+ *
1040
+ * <p>Async methods will use a sensible default if no custom {@link ExecutorProvider} has been
1041
+ * set. The default {@link ExecutorProvider} uses a cached thread pool containing a maximum of 8
1042
+ * threads. The pool is lazily initialized and will not create any threads if the user
1043
+ * application does not use any async methods. It will also scale down the thread usage if the
1044
+ * async load allows for that.
1045
+ *
1046
+ * <p>Call {@link SpannerOptions#createAsyncExecutorProvider(int, long, TimeUnit)} to create a
1047
+ * provider with a custom pool size or call {@link
1048
+ * FixedCloseableExecutorProvider#create(ScheduledExecutorService)} to create a {@link
1049
+ * CloseableExecutorProvider} from a standard Java {@link ScheduledExecutorService}.
1050
+ */
1051
+ public Builder setAsyncExecutorProvider (CloseableExecutorProvider provider ) {
1052
+ this .asyncExecutorProvider = provider ;
1053
+ return this ;
1054
+ }
1055
+
1021
1056
/**
1022
1057
* Specifying this will allow the client to prefetch up to {@code prefetchChunks} {@code
1023
1058
* PartialResultSet} chunks for each read and query. The data size of each chunk depends on the
@@ -1198,7 +1233,7 @@ public QueryOptions getDefaultQueryOptions(DatabaseId databaseId) {
1198
1233
return options ;
1199
1234
}
1200
1235
1201
- CloseableExecutorProvider getAsyncExecutorProvider () {
1236
+ public CloseableExecutorProvider getAsyncExecutorProvider () {
1202
1237
return asyncExecutorProvider ;
1203
1238
}
1204
1239
0 commit comments