I am saving a document to Google Cloud Firestore using Google Cloud Functions in Java language.
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);
I want to add a listener to future
here.
future.addListener(/* which Runnable listener to pass here to
process the WriteResult object
when the future completes? */, MoreExecutors.directExecutor());
I am unable to find a Runnable which can serve the purpose here. I know I can call future.get()
method to hold the execution and get the WriteResult object but I don't want to do that.
Also, unable to understand how to add a listener to userDoc.get()
.
I have searched StackOverflow and Github already. Could not find anything helpful.
All the posts talk about ApiFuture.addListener()
which is deprecated and is not available now.
Please find below my 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>
Already wasted 2 days. Please help.
CodePudding user response:
According to the documentation, you can use 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/
Example:
ApiFuture<WriteResult> result1 =
ApiFutures.addCallback(result1, new ApiFutureCallback<WriteResult>() {
@Override
public void onFailure(Throwable throwable) {
}
@Override
public void onSuccess(WriteResult writeResult) {
}
}, Runnable::run);