java - 向 ApiFuture 添加侦听器时要传递的可运行内容和 ApiFuture?
问题描述
我正在使用 Java 语言的 Google Cloud Functions 将文档保存到 Google Cloud Firestore。
DocumentReference userDoc = firestore.document("Users/user1");
final Map<String,Object> UserData = new HashMap<String,Object>(){{
put("Name", "First Last");
}};
ApiFuture<WriteResult> future = userDoc.set(UserData);
我想在future
这里添加一个监听器。
future.addListener(/* which Runnable listener to pass here to
process the WriteResult object
when the future completes? */, MoreExecutors.directExecutor());
我找不到可以在这里达到目的的 Runnable。我知道我可以调用future.get()
方法来保持执行并获取 WriteResult 对象,但我不想这样做。
此外,无法理解如何将侦听器添加到userDoc.get()
.
我已经搜索过 StackOverflow 和 Github。找不到任何有用的东西。
所有帖子都讨论了ApiFuture.addListener()
哪些已弃用且现在不可用。
请在我的 pom.xml 下面找到
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>some.group.Id</groupId>
<artifactId>cloud-functions</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.cloud.functions/functions-framework-api -->
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.0.4</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-auth -->
<!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-auth -->
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>8.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-pubsub -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>1.114.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.google.cloud.functions</groupId>
<artifactId>function-maven-plugin</artifactId>
<version>0.10.0</version>
<configuration>
<functionTarget>com.infonotics.stocklyticsusa.FirstFunction</functionTarget>
</configuration>
</plugin>
</plugins>
</build>
</project>
已经浪费了2天。请帮忙。
解决方案
根据文档,您可以使用ApiFutures.addCallBack
:
static <V> void addCallback(ApiFuture<V> future, ApiFutureCallback<? super V> callback, Executor executor)
https://googleapis.github.io/api-common-java/1.9.1/apidocs/
例子:
ApiFuture<WriteResult> result1 =
ApiFutures.addCallback(result1, new ApiFutureCallback<WriteResult>() {
@Override
public void onFailure(Throwable throwable) {
}
@Override
public void onSuccess(WriteResult writeResult) {
}
}, Runnable::run);
推荐阅读
- image - 使用 Imagemagick 查找黑色边框尺寸
- c# - 如何以与即时窗口相同的方式将集合输出到控制台
- r - R问题 - 为数据框中的变量生成数字序列
- python - 使用python删除文本文件中不需要的部分
- python - Python 记录器不遵循格式
- phpspreadsheet - 保存文件 phpspreadset 后计算不起作用
- python - pandas:将一系列字母数字文本拆分并转换为列和行
- selenium - 选择第二行表的 XPath 不适用于 Selenium
- material-design - 带轮播项目的材料盒
- bash - 如何验证功能分支不包含任何合并提交并且是最新的?