Firebase and Java Play! Framework not fully working together

最后都变了- 提交于 2019-12-25 09:17:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!