简体   繁体   中英

JavaEE and Firebase admin sdk - setValueAsync not pushing data to realtime firebase

Am using firebase admin sdk and JavaEE on intellij built on gradle and glassfish server.

am trying to push a value to realtime database, but sadly am unable to do so. I've been searching online for weeks now and gotten nothing. I also followed some solutions in stackoverflow answers like: Firebase Java Admin SDK don't work but nothing works for me.

I've read a lot of reasons why such a problem would occur with firebase admin sdk but i have no solutions.

here's my code:

package sample;

package sample;

import com.google.api.core.ApiFuture;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseToken;
import com.google.firebase.auth.UserRecord;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import sample.model.FireBaseAuth;
import sample.model.FireBaseUtils;
import sample.model.Owner;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

@WebServlet("/success")
public class SuccessServlet extends HttpServlet {

    public void init() throws ServletException {
        super.init();
        FireBaseUtils.initilizeFirebase();
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        String email = request.getParameter("email");
        String password  = request.getParameter("pass");

        //System.out.println(name);

        try{
            //a hashmap for the number of shopsOwned
            HashMap<String, String> shopsOwned = new HashMap<>();
            shopsOwned.put("shopName" , "shopName");

            //get the database instance and the database reference
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference ref = database.getReference("Business");

            DatabaseReference ownersRef = ref.child("Owners"); //further get the reference to the owners node

            //create a new owner with the values of the new user, using the Owner class
            Owner newOwner = new Owner("userRecord2.getUid()", "userRecord2.getDisplayName()",
                    "userRecord2.getPhoneNumber()", "userRecord2.getEmail()", shopsOwned);

            //create a hashmap of the users, in this case, just one user
            Map<String, Owner> users = new HashMap<>();
            users.put("userRecord2getPhoneNumber", newOwner); //add the new owner to the hashmap

            System.out.println("this is the user :" + newOwner.getFull_name());

            //push the new owner hashmap to the database reference
            ApiFuture<Void> future = ownersRef.push().setValueAsync(users);

            //Object o = future.get(8, TimeUnit.SECONDS);
            System.out.println(future.isDone());
            //System.out.println(future.isDone());

            request.getRequestDispatcher("success.jsp").forward(request, response);

        }catch(Exception e){e.printStackTrace();}



    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        doGet(request, response);

    }

}

any ideas will be appreciated.

Edit: I dont get any errors whatsoever, the webapp runs normally but the realtime db at firebase isn't updated

You need to wait until the future is complete, before the request thread is returned. Otherwise there's no guarantee that the update is completed, and any errors are silently discarded. So try something like the following:

ApiFuture<Void> future = ownersRef.push().setValueAsync(users);
future.get();
request.getRequestDispatcher("success.jsp").forward(request, response);

Writing to Firestore (like interaction with most cloud APIs) happens asynchronously, and on a different thread. When you call future.isDone() , the operation isn't done yet.

You'll want to add a callback that gets called when the operation has completed:

ApiFuture<Void> future = ownersRef.push().setValueAsync(users);
future.addCallback(future, new ApiFutureCallback<String>() {
    @Override
    public void onSuccess(String result) {
      System.out.println("Operation completed with result: " + result);
      System.out.println(future.isDone());
      request.getRequestDispatcher("success.jsp").forward(request, response);
    }

    @Override
    public void onFailure(Throwable t) {
      System.err.println("Operation failed with error: " + t);
    }

Also see:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM