firebase数据库连接挂起

时间:2017-10-23 17:26:04

标签: database firebase firebase-realtime-database service-accounts mbaas

我不清楚如何解决firebase连接问题。尽管我可以建立连接,但是..

这是否显示连接?

thufir@dur:~/NetBeansProjects/firebase$ 
thufir@dur:~/NetBeansProjects/firebase$ gradle runShadow

> Task :runShadow
[main] INFO net.bounceme.dur.firebase.FirebaseQuery - starting query..
[main] INFO net.bounceme.dur.firebase.FirebaseQuery - [DEFAULT]
[main] INFO net.bounceme.dur.firebase.FirebaseQuery - https://<identifier>.firebaseio.com
<===========--> 85% EXECUTING [23s]
> :runShadow
^Cthufir@dur:~/NetBeansProjects/firebase$ 
thufir@dur:~/NetBeansProjects/firebase$ 

当然看起来,至少在最初,它是连接的。 Java代码:

package net.bounceme.dur.firebase;

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.slf4j.LoggerFactory;

public class FirebaseQuery {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(FirebaseQuery.class);

    private DatabaseReference databaseReference = null;

    public FirebaseQuery() {
    }

    /*
    void pushUsers(List<User> users) {
        for (User u : users) {
            databaseReference.child("users").child(u.uuid.toString()).setValue(u);
        }
    }
     */
    void tryQuery(String url, String serviceAccountKey) {
        try {
            query(url, serviceAccountKey);
        } catch (IOException | InterruptedException ex) {
            java.util.logging.Logger.getLogger(FirebaseQuery.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    }

    private void query(String url, String serviceAccountKey) throws FileNotFoundException, IOException, InterruptedException {
        logger.info("starting query..");
        FileInputStream serviceAccount = new FileInputStream(serviceAccountKey);
        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
                .setDatabaseUrl(url)
                .build();
        FirebaseApp firebaseApp = FirebaseApp.initializeApp(options);
        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance(firebaseApp, url);
        databaseReference = firebaseDatabase.getReference().getRoot();

        logger.info(firebaseApp.getName());
        logger.info(databaseReference.toString());

        CountDownLatch latch = new CountDownLatch(1);
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                logger.info("listener added");
                String key = null;
                String value = null;
                logger.info("initialized key/value pair");
                for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                    key = childSnapshot.getKey();
                    value = (String) childSnapshot.getValue();
                    logger.info(key + "\t\t" + value);
                }
                latch.countDown();
                logger.info("finished countdown");
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                latch.countDown();
                throw databaseError.toException();
            }
        });
        latch.await();
    }
}

我如何明确确定是否连接?

(不使用Android,只使用常规Java SE。)

0 个答案:

没有答案