Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在AWS lambda中等待(Firebase)回调?_Java_Amazon Web Services_Firebase_Callback_Aws Lambda - Fatal编程技术网

Java 如何在AWS lambda中等待(Firebase)回调?

Java 如何在AWS lambda中等待(Firebase)回调?,java,amazon-web-services,firebase,callback,aws-lambda,Java,Amazon Web Services,Firebase,Callback,Aws Lambda,我想问一些类似的问题,但据我所知,没有一个与Java相关。 我想调用AWS lambda函数,在该函数中我连接到Firebase数据库。问题是处理程序在我从Firebase获得所需数据之前执行 @Override public String handleRequest(Request input, Context context) { try { FileInputStream serviceAccountInputStream = new FileInputStream

我想问一些类似的问题,但据我所知,没有一个与Java相关。 我想调用AWS lambda函数,在该函数中我连接到Firebase数据库。问题是处理程序在我从Firebase获得所需数据之前执行

@Override
public String handleRequest(Request input, Context context) {
    try {
        FileInputStream serviceAccountInputStream = new FileInputStream(FIREBASE_SERVICE_ACCOUNT_CREDENTIALS_PATH);

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredential(FirebaseCredentials.fromCertificate(serviceAccountInputStream))
                .setDatabaseUrl(FIREBASE_DATABASE_URL)
                .build();
        FirebaseApp.initializeApp(options);

        DatabaseReference ref = FirebaseDatabase
                .getInstance()
                .getReference("users/" + input.getUid());
        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                System.out.print(dataSnapshot);
                // TODO: Do computations on data and return results
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.print("Canceled");
                // TODO: Return error
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    // TODO: Return computed results
    return "This could be the start of something new.";
}

我需要从Firebase数据库读取数据,用它进行一些计算,并将计算结果返回给用户。我怎样才能做到这一点?:)

您可以使用倒计时锁存器等待异步代码完成

在调用异步代码之前,请创建CountDownLatch:

final CountDownLatch countDownLatch = new CountDownLatch(1);
然后在回调方法结束时,对闩锁进行倒计时:

countDownLatch.countDown();
然后,在异步方法之后,等待倒计时闩锁倒计时:

waitForCountdownLatch(countDownLatch);

private static void waitForCountdownLatch(CountDownLatch countDownLatch) {
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        log.error(e);
        e.printStackTrace();
    }
}
因此,对于您的代码,它将是:

@Override
public String handleRequest(Request input, Context context) {

    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final Object[] singleValue = new Object[1];
    final DatabaseError[] firebaseError = new DatabaseError[1];

    try {
        FileInputStream serviceAccountInputStream = new FileInputStream(FIREBASE_SERVICE_ACCOUNT_CREDENTIALS_PATH);

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredential(FirebaseCredentials.fromCertificate(serviceAccountInputStream))
                .setDatabaseUrl(FIREBASE_DATABASE_URL)
                .build();
        FirebaseApp.initializeApp(options);

        DatabaseReference ref = FirebaseDatabase
                .getInstance()
                .getReference("users/" + input.getUid());

        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                System.out.print(dataSnapshot);
                Object snapshotValue = dataSnapshot.getValue();
                if(snapshotValue != null) {
                    singleValue[0] = snapshotValue;
                }
                countDownLatch.countDown();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.print("Canceled");
                firebaseError0] = databaseError;
                countDownLatch.countDown();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        countDownLatch.countDown();
    }

    waitForCountdownLatch(countDownLatch);
    if(firebaseError[0] != null) {
        System.out.print(firebaseError[0].toException().getMessage());
    }
    if(singleValue[0] != null) {
        // do something with result
    }
    return "This could be the start of something new.";
}

private void waitForCountdownLatch(CountDownLatch countDownLatch) {
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        log.error(e);
        e.printStackTrace();
    }
}