AWS SDK - 异步配置的目的 SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR

AWS SDK - purpose of async configuration SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR

异步配置的目的是什么SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR? 我们应该在哪些用例中使用它?

FUTURE_COMPLETION_EXECUTOR 上的 java 文档,我看到: Configure the executor that should be used to complete the CompletableFuture that is returned by the service clients. 我认为后续对异步结果 CompletableFuture 的调用将在传入的执行程序上执行 FUTURE_COMPLETION_EXECUTOR,但事实并非如此。

ClientAsyncConfiguration.Builder asyncConfig = ClientAsyncConfiguration.builder()
        .advancedOption(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR, customExecutor);

SnsAsyncClient.builder()
        .asyncConfiguration(asyncConfig.build())
        .build();

异步请求示例:

snsAsyncClient.publish(publishRequest).thenApplyAsync(..);

AWS SDK 异步请求 return CompletableFuture。默认情况下,在 returned CompletableFuture 实例上调用 thenApply(..)whenComplete(..) 将在 sdk-async-response-0-X 等线程上执行。 异步配置SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR的思路是提供线程池执行器,用于CompletableFuture后续调用,如thenApply(..)whenComplete(..)。由于 CompletableFuture 实现,此执行器将不会应用于 thenApplyAsyncwhenCompleteAsync 等方法(如果执行器未传递到 thenApplyAsync(..),将默认使用 ForkJoinPool.commonPool(),或者我们可以将自定义执行程序作为第二个方法参数传递)。

snsAsyncClient.publish(publishRequest)
        .thenApply(..)
        .whenComplete(..);

thenApplywhenComplete 中的代码将在来自 SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR 的已配置执行程序上处理。