Skip to content

What Runnable to Pass when adding a listener to ApiFuture and ApiFuture?

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="" xmlns:xsi="" xsi:schemaLocation="">
        <!-- -->
    <!-- -->
    <!-- -->
    <!-- -->

    <!-- -->

Already wasted 2 days. Please help.


According to the documentation, you can use ApiFutures.addCallBack:

static <V> void     addCallback(ApiFuture<V> future, ApiFutureCallback<? super V> callback, Executor executor)


    ApiFuture<WriteResult> result1 = 

    ApiFutures.addCallback(result1, new ApiFutureCallback<WriteResult>() {
        public void onFailure(Throwable throwable) {


        public void onSuccess(WriteResult writeResult) {

    }, Runnable::run);