问题
I am trying to add Firebase to my Java Play! Framework project. I followed the steps here https://firebase.google.com/docs/admin/setup. There is very little documentation on the server setup. Lots on android, but it is not too helpful.
The Problem I am able to initialize the firebase library and even get the first call to the database to return successfully, but after that all of the next calls just hang. They never return in ether the onCancelled or onDataChange blocks. I don't get any exceptions in the terminal ether.
This is really strange behavior. Has anyone ever successfully added Firebase to a Java Play! project? My only thoughts are that it might have something to do with having a valid token or the threads Play uses.
Here are my classes:
public class FirebaseServices {
public static FirebaseOptions fbOptions;
public static FirebaseApp fbApp;
public static boolean firebaseLoaded = initFirebase();
private FirebaseServices(){
}
private static boolean initFirebase(){
System.out.println("Apps: " + FirebaseApp.getApps().size());
if(FirebaseApp.getApps().isEmpty()){
System.out.println("Initialize Firebase");
try {
fbOptions= new FirebaseOptions.Builder()
.setServiceAccount(new FileInputStream("conf/service-account.json"))
.setDatabaseUrl("https://my-project123.firebaseio.com").build();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
fbApp = FirebaseApp.initializeApp(fbOptions);
}
return true;
}
final static FirebaseDatabase database = FirebaseDatabase.getInstance();
public final static DatabaseReference USER_REF = database.getReference("User");
}
In My User class
public User getUserById(String unitId){
System.out.println("getUser");
CompletableFuture<Unit> resolution = new CompletableFuture<Unit>();
MyFirebaseService.USER_REF.child(userId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Unit u = dataSnapshot.getValue(User.class);
u.id = dataSnapshot.getKey();
System.out.println("OUTPUT:"+ u.getFirstName());
resolution.complete(u);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
resolution.complete(null);
}
});
try {
return resolution.join();
} catch(Exception e){
return null;
}
}
来源:https://stackoverflow.com/questions/40690192/firebase-and-java-play-framework-not-fully-working-together