0

What is the purpose of asyncConfiguration SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR? in which use cases should we use it?

from java doc on FUTURE_COMPLETION_EXECUTOR, I see: Configure the executor that should be used to complete the CompletableFuture that is returned by the service clients. I thought that subsequent calls on CompletableFuture of async result will be executed on executor passed in FUTURE_COMPLETION_EXECUTOR, but it’s not.

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

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

Example of async request:

snsAsyncClient.publish(publishRequest).thenApplyAsync(..);
Vasyl Sarzhynskyi
  • 3,689
  • 2
  • 22
  • 55

1 Answers1

1

AWS SDK async requests return CompletableFuture. Invocation of thenApply(..) and whenComplete(..) on returned CompletableFuture instance by default will be executed on threads like sdk-async-response-0-X. The idea of async configuration SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR is to provide thread pool executor, which will be used for subsequent calls on CompletableFuture like thenApply(..) and whenComplete(..). This executor will not be applied for methods like thenApplyAsync and whenCompleteAsync due to CompletableFuture implementation (if executor not passed into thenApplyAsync(..), it will be used by default ForkJoinPool.commonPool(), or we could pass custom executor as a second method argument).

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

code inside thenApply and whenComplete will be processed on configured executor from SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR.

Vasyl Sarzhynskyi
  • 3,689
  • 2
  • 22
  • 55