向 ApiFuture<WriteResult> 和 ApiFuture<DocumentSnapshot> 添加侦听器时要传递什么 Runnable?
What Runnable to Pass when adding a listener to ApiFuture<WriteResult> and ApiFuture<DocumentSnapshot>?
我正在使用 Google Cloud Functions 以 Java 语言将文档保存到 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()
添加侦听器。
我已经搜索过 Whosebug 和 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);
我正在使用 Google Cloud Functions 以 Java 语言将文档保存到 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()
添加侦听器。
我已经搜索过 Whosebug 和 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);