我正在使用 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天。請幫忙。
uj5u.com熱心網友回復:
根據檔案,您可以使用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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/357280.html
標籤:爪哇 行家 谷歌云firestore 谷歌云功能
下一篇:在階段定義插件目標
