当我需要使用 I/O(查询 DB、调用第三个 API,...)时,我可以使用 RichAsyncFunction。但我需要通过 GG Sheet API 与 Google Sheet 进行交互: https: //developers.google.com/sheets/api/quickstart/java。这个API是同步的。我写了下面的代码片段:
public class SendGGSheetFunction extends RichAsyncFunction<Obj, String> {
@Override
public void asyncInvoke(Obj message, final ResultFuture<String> resultFuture) {
CompletableFuture.supplyAsync(() -> {
syncSendToGGSheet(message);
return "";
}).thenAccept((String result) -> {
resultFuture.complete(Collections.singleton(result));
});
}
}
Run Code Online (Sandbox Code Playgroud)
但我发现消息发送到GGSheet非常慢,看起来是同步发送的。