如何从Firebase Java Admin SDK中停止线程



通过运行https://github.com/firebase/firebase-admin-java/blob/master/src/src/test/java/java/google/google/firebase/database/integration/integration/shutegration/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()后,仍然有一些线程运行:

参考处理程序终结器信号调度器firebasedatabaseworker pool-5-thread-1

如何清洁它?

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

最新更新