如何从Firebase Java Admin SDK

时间:2017-09-12 23:24:10

标签: java firebase firebase-realtime-database

运行示例代码 https://github.com/firebase/firebase-admin-java/blob/master/src/test/java/com/google/firebase/database/integration/ShutdownExample.java

public class ShutdownExample {

  public static void main(String[] args) {
    final Semaphore shutdownLatch = new Semaphore(0);

FirebaseApp app =
    FirebaseApp.initializeApp(
        new FirebaseOptions.Builder()
            .setDatabaseUrl("https://admin-java-sdk.firebaseio.com")
            .build());

FirebaseDatabase db = FirebaseDatabase.getInstance(app);
db.setLogLevel(Level.DEBUG);
DatabaseReference ref = db.getReference();

ValueEventListener listener =
    ref.child("shutdown")
        .addValueEventListener(
            new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot snapshot) {
                Boolean shouldShutdown = snapshot.getValue(Boolean.class);
                if (shouldShutdown != null && shouldShutdown) {
                  System.out.println("Should shut down");
                  shutdownLatch.release(1);
                } else {
                  System.out.println("Not shutting down: " + shouldShutdown);
                }
              }

              @Override
              public void onCancelled(DatabaseError error) {
                System.err.println("Shouldn't happen");
              }
            });

try {
  // Keeps us running until we receive the notification to shut down
  shutdownLatch.acquire(1);
  ref.child("shutdown").removeEventListener(listener);
  db.goOffline();
  System.out.println("Done, should exit");
} catch (InterruptedException e) {
  throw new RuntimeException(e);
}

调用db.goOffline()后,仍有一些线程在运行:

  

Reference Handler Finalizer Signal Dispatcher FirebaseDatabaseWorker   池-5-线程1

如何清洁呢?

1 个答案:

答案 0 :(得分:1)

脱机仅意味着Firebase实时数据库SDK将不再进行网络连接。这并不意味着它的线程必须消失或者程序应该退出。如果您已完成工作并希望退出流程,请致电System.exit(0)