如何从弃用的任务转移到 ApiFuture for firebase admin SDK 5.4 及更高版本

How to move from deprecated Task to ApiFuture for firebase admin SDK 5.4 and above

我只是想解决我的新 firebase-admin SDK Java 代码中的弃用说明,该代码是在 5.3.1 版本中编写的,但在将版本升级到 5.5.0 后弃用出现注释,这是我的代码示例:

使用 FirebaseAuth(弃用:TaskaddOnSuccessListeneraddOnFailureListener):

private CompletableFuture<FirebaseToken> getDecryptedTokenCompletableFuture(String firebaseTokenString) {
        CompletableFuture<FirebaseToken> tokenFuture = new CompletableFuture<>();
        Task<FirebaseToken> tokenTask = FirebaseAuth.getInstance(firebaseApp).verifyIdToken(firebaseTokenString);
        tokenTask.addOnSuccessListener(tokenFuture::complete);
        tokenTask.addOnFailureListener(exception -> tokenFuture.completeExceptionally(new AuthorizationException("Failed to verify token", exception)));
        return tokenFuture;
    }

对于 FirebaseDatabase(弃用:TaskaddOnSuccessListeneraddOnFailureListenerupdateChildrenremoveValue) :

public static <T> CompletableFuture<T> toCompletableFuture(Task<T> task) {
    CompletableFuture<T> future = new CompletableFuture<>();
    task.addOnCompleteListener(result -> {
        future.complete(result.getResult());
    }).addOnFailureListener(future::completeExceptionally);
    return future;
}

/**
 * @param updatedParams if null it will removed child
 * @param path          path to update
 * @return void when complete
 */
public CompletableFuture<Void> updateObjectData(Map<String, Object> updatedParams, String path) {
    if (updatedParams == null) {
        return removeObjectData(path);
    }
    logger.debug("Update ObjectData in firebase of ref ({}) with data: {}", path, updatedParams.toString());
    DatabaseReference child = this.getUserDataReference().child(path);
    return toCompletableFuture(child.updateChildren(updatedParams));
}

/**
 * @param path path to of node to remove
 * @return void when complete
 */
public CompletableFuture<Void> removeObjectData(String path) {
    logger.debug("Remove ObjectData in firebase of ref ({})", path);
    DatabaseReference child = this.getUserDataReference().child(path);
    return toCompletableFuture(child.removeValue());
}

弃用说明说我必须使用 ApiFuture 正如发行说明所说:https://firebase.google.com/support/release-notes/admin/java

以及内源,例如:

  /**
   * Similar to {@link #updateChildrenAsync(Map)} but returns a Task.
   *
   * @param update The paths to update and their new values
   * @return The {@link Task} for this operation.
   * @deprecated Use {@link #updateChildrenAsync(Map)}
   */

/**
 * Represents an asynchronous operation.
 *
 * @param <T> the type of the result of the operation
 * @deprecated {@code Task} has been deprecated in favor of
 *     <a href="https://googleapis.github.io/api-common-java/1.1.0/apidocs/com/google/api/core/ApiFuture.html">{@code ApiFuture}</a>.
 *     For every method x() that returns a {@code Task<T>}, you should be able to find a
 *     corresponding xAsync() method that returns an {@code ApiFuture<T>}.
 */

看看 ApiFutures 实用程序 class,它允许将回调添加到 ApiFuture

使用 ApiFuture 从 Firebase Admin SDK Java 使用 FirebaseAuth 验证令牌的代码为:

ApiFutures.addCallback(FirebaseAuth.getInstance().verifyIdTokenAsync(token),
  new ApiFutureCallback<FirebaseToken>() {
      @Override
      public void onFailure(Throwable t) {
        // TODO handle failure
      }
      @Override
      public void onSuccess(FirebaseToken decodedToken) {
        // TODO handle success
      }
});

类似的方法可以用于使用 FirebaseDatabase 的代码。

Hiranya Jayathilaka 写了一篇非常详细的文章,解释了如何从 Task 迁移到 ApiFuture 及其背后的原理:https://medium.com/google-cloud/firebase-asynchronous-operations-with-admin-java-sdk-82ca9b4f6022

此代码适用于 5.4.0 - 5.8.0 版本,这是撰写本文时的最新版本。 发行说明可在此处获得:https://firebase.google.com/support/release-notes/admin/java

将近 2 年,但需要添加我的答案,当前的答案给了我一个指导但不是完整的解决方案,所以感谢 @Enylton Machado@Hiranya Jayathilaka 仍然可以参考他们的答案,给他们投票。

目前,我确实将 toCompletableFuture 更改为:

public <T> CompletableFuture<T> toCompletableFuture(ApiFuture<T> apiFuture) {
    final CompletableFuture<T> future = new CompletableFuture<>();
    apiFuture.addListener(() -> {
        try {
            future.complete(apiFuture.get());
        } catch (InterruptedException | ExecutionException ex) {
            logger.error(ex.getMessage());
            future.completeExceptionally(ex);
        }
    }, executionContext);
    return future;
}

在构造函数中注入 executionContext,因为我使用的是 Play Framework,或者您可以使用 Executors.newFixedThreadPool(10) 来初始化您自己的框架!但是你需要处理关闭它和其他东西,可以从这里了解更多详细信息ExecutorService in Java or A Guide to the Java ExecutorService

所以当我像 this.getUserDataReference().child(path).push().setValueAsync(value) 这样调用时,我会这样调用它:

public CompletableFuture<String> pushDataToArray(String path, Map<String, Object> paramsToAdd) {
    final DatabaseReference databaseReference = getUserDataReference().child(path).push();
    paramsToAdd.put("createdAt", ServerValue.TIMESTAMP);

    return toCompletableFuture(databaseReference.setValueAsync(paramsToAdd), this.executionContext)
            .thenApply(voidResult -> databaseReference.getKey());
}

编辑:如果你喜欢使用ApiFutures

public <T> CompletableFuture<T> toCompletableFuture(ApiFuture<T> apiFuture) {
    final CompletableFuture<T> future = new CompletableFuture<>();
    ApiFutures.addCallback(apiFuture, new ApiFutureCallback<T>() {
        @Override
        public void onFailure(Throwable t) {
            future.completeExceptionally(t);
        }

        @Override
        public void onSuccess(T result) {
            try {
                future.complete(apiFuture.get());
            } catch (InterruptedException | ExecutionException ex) {
                logger.error(ex.getMessage());
                future.completeExceptionally(ex);
            }
        }
    }, executionContext);
    return future;
}