2017-04-21 31 views
1

我一直在尋找一些類似的問題,但據我所知他們都不是Java相關的。 我想調用一個AWS lambda函數,在其中連接到Firebase數據庫。問題是,處理程序在我從Firebase獲取所需數據之前執行。如何等待AWS lambda中的(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."; 
} 

我需要從火力地堡數據庫中讀取數據,做一些計算與它的計算結果返回給用戶。我怎樣才能做到這一點? :)

回答

0

您可以等待異步代碼完成使用CountDownLatch。

調用異步代碼之前創建CountDownLatch:

final CountDownLatch countDownLatch = new CountDownLatch(1); 

然後在回調方法結束時,你倒計數鎖存器:

countDownLatch.countDown(); 

然後你等待異步方法後CountDownLatch被倒計數:

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(); 
    } 
}