Firebase不会调用侦听器的onDataChange().无法在Java客户端(而非Android)上使用新的Google Firebase [英] Listener's onDataChange() is not called by Firebase. Could not use new Google-Firebase on Java client, not Android

查看:45
本文介绍了Firebase不会调用侦听器的onDataChange().无法在Java客户端(而非Android)上使用新的Google Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正尝试一个星期,以便在Java Client上从Firebase迁移到google-firebase.一个重要的细节是客户端将在受控环境中运行,因此我可以允许将某些密钥存储在本地.

学习后,我可以根据本教程找到 的三种方式..>

作为使用服务帐户密钥的服务器",不会调用 onDataChange()回调.这是我付出更多努力的解决方案,也是我仍然相信的解决方案.我将把我的代码提交给这个问题.我怀疑我没有处理异步调用权限.

我对此示例的客观问题是本教程中的示例 自包含的还是我应该在Java程序上实现某些功能以使其正常工作,而我却丢失了?

此示例代码:

  class Conf{公共字符串activeMQPassword;公共字符串activeMQPath;public String activeMQUser;公共字符串jdbcPassword;公共字符串jdbcUrl;公共字符串jdbcUser;public String jmsserver;public String logPath;公共字符串s3AccessKey;public String s3Path;公共字符串s3SecretKey;公共Conf(String activeMQPassword,String activeMQPath,String activeMQUser,String jdbcPassword,String jdbcUrl,String jdbcUser,String jmsserver,String logPath,String s3AccessKey,String s3Path,String s3SecretKey){this.activeMQPassword = activeMQPassword;this.activeMQPath = activeMQPath;this.activeMQUser = activeMQUser;this.jdbcPassword = jdbcPassword;this.jdbcUrl = jdbcUrl;this.jdbcUser = jdbcUser;this.jmsserver = jmsserver;this.logPath = logPath;this.s3AccessKey = s3AccessKey;this.s3Path = s3Path;this.s3SecretKey = s3SecretKey;}}FirebaseOptions选项=新的FirebaseOptions.Builder().setServiceAccount(new FileInputStream("c:/chaveFB/TesteNovaApi-0efd51a8ad0c.json")).setDatabaseUrl("https://testenovaapi.firebaseio.com/")//.setDatabaseAuthVariableOverride(auth)//AQUI SETAMOSRESTRIÇÃODE ACESSO CRIADA LOGO ACIMA..建造();FirebaseApp.initializeApp(options);DatabaseReference ref = Firebase数据库.getInstance().getReference("/conf");ref.addListenerForSingleValueEvent(new ValueEventListener(){@Override公共无效onDataChange(DataSnapshot dataSnapshot){System.out.println("LA2LA2LA2LA2LA2LA2LA2LA2LA2LA2LA2");conf conf = dataSnapshot.getValue(Conf.class);System.out.println(conf.activeMQPassword);System.out.println(conf.activeMQPath);System.out.println(conf.activeMQUser);System.out.println(conf.jdbcPassword);System.out.println(conf.jdbcUser);System.out.println(conf.jmsserver);}@Override公共无效onCancelled(DatabaseError databaseError){}}); 

我能够在Stack Overflow主题中找到答案,从而导致转到此PDF解决回调问题,但是此解决方案似乎是Android驱动的,因为我无法在Java程序中遵循很多步骤.

作为Android或Web客户端,我无法将所需的库导入到Java应用程序中.

使用API​​,过去我使用带有自定义标记的API来下载json和检索数据.但是由于不建议使用数据库密钥,因此新的Java库仅在服务器模式"下工作,因此该服务器库创建的自定义密钥无法检索数据.Firebase响应错误,例如用户ID密钥预期收到自定义密钥.

如果我可以采用这种方法,那么对我来说还可以,因为安装Java客户端的位置受到控制并且足够安全.这是我的较老方法.

我一定错得很厉害,我更愿意做到.但是我的感觉是,我被锁在一个尚未涵盖或没有充分记录的案件中.行为类似于客户端的Java程序,而不是Android或Web.

解决方案

如果您有通过某个类的main函数在命令行上调用的java程序,则必须阻止该main函数在数据库侦听器触发之前返回.否则,程序将立即终止,并且监听器将永远不会触发.重要的是要记住,所有Firebase侦听器都是异步的,并且Firebase管理其自己的守护程序线程以与服务器进行通信.

对于使一个简单的程序等待侦听器触发的情况,您可以使用这种模式来阻塞主线程以等待侦听器:

  CountDownLatch闩锁=新的CountDownLatch(1);ref.addListenerForSingleValueEvent(new ValueEventListener(){@Override公共无效onDataChange(DataSnapshot dataSnapshot){System.out.println("onDataChange:" + dataSnapshot);latch.countDown();}@Override公共无效onCancelled(DatabaseError databaseError){System.out.println("onCanceled:" + databaseError);latch.countDown();}});闩锁.await(); 

使用CountDownLatch不是唯一的选择.您可以采取任何措施确保进程不会停止,包括简单地将主线程休眠的时间长于侦听器触发的时间.

I've being trying for a week right now to migrate from Firebase to google-firebase on a Java Client. One important detail is that the client will run in a controlled environment so I can allow some key to be stored locally.

After my studies I could find three ways based on this tutorial.

As a "server" using the service account key the onDataChange() callback is not being called. This is the solution I put more effort and the one I still believe. I'll put my code forward on this question. I'm suspecting that I'm not handling the asynchronous call right.

My objective question about this example is, are the examples in this tutorial self contained or is there something I should implement on a Java program to make this work and I'm missing?

This example code:

class Conf
{
    public String activeMQPassword;
    public String activeMQPath;
    public String activeMQUser;
    public String jdbcPassword;
    public String jdbcUrl;
    public String jdbcUser;
    public String jmsserver;
    public String logPath;
    public String s3AccessKey;
    public String s3Path;
    public String s3SecretKey;

    public Conf(String activeMQPassword, String activeMQPath, String activeMQUser, String jdbcPassword, String jdbcUrl, String jdbcUser, String jmsserver, String logPath, String s3AccessKey, String s3Path, String s3SecretKey)
    {
        this.activeMQPassword = activeMQPassword;
        this.activeMQPath = activeMQPath;
        this.activeMQUser = activeMQUser;
        this.jdbcPassword = jdbcPassword;
        this.jdbcUrl = jdbcUrl;
        this.jdbcUser = jdbcUser;
        this.jmsserver = jmsserver;
        this.logPath = logPath;
        this.s3AccessKey = s3AccessKey;
        this.s3Path = s3Path;
        this.s3SecretKey = s3SecretKey;
    }

}



FirebaseOptions  options = new FirebaseOptions.Builder()
                .setServiceAccount(new FileInputStream("c:/chaveFB/TesteNovaApi-0efd51a8ad0c.json"))
                .setDatabaseUrl("https://testenovaapi.firebaseio.com/")
                //.setDatabaseAuthVariableOverride(auth) //AQUI SETAMOS A RESTRIÇÃO DE ACESSO CRIADA LOGO ACIMA.
                .build();
FirebaseApp.initializeApp(options);

DatabaseReference ref = FirebaseDatabase
            .getInstance()
            .getReference("/conf");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        System.out.println("LA2LA2LA2LA2LA2LA2LA2LA2LA2LA2");
        Conf conf = dataSnapshot.getValue(Conf.class);
        System.out.println(conf.activeMQPassword);
        System.out.println(conf.activeMQPath);
        System.out.println(conf.activeMQUser);
        System.out.println(conf.jdbcPassword);
        System.out.println(conf.jdbcUser);
        System.out.println(conf.jmsserver);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

I was able to find an answer in a Stack Overflow topic that leads to this PDF to solve the callback problem, but this solution seems to be very Android driven since I could not follow lots of steps in my Java program.

As an Android or web client, I'm was not able to import the libs needed to my Java application.

Using the API, In the past I was using the API with custom token to download a json and retrieve data. But since the database secret keys were deprecated, the new java library works only in "server mode" so the custom-key created by this server library was not able to retrieve data. Firebase was responding with an error like user-id key expected received custom-key.

If I could make this approach to work, is OK for me since where the Java client is being installed is controlled and safe enough. And it was my older approach.

I must be terribly wrong, and I prefer to be. But my sensation is that I'm locked in a case that is not yet covered or well documented. A Java program that acts like a client and it's not Android or web.

解决方案

If you have a java program that's invoked on the command line via some class's main function, you have to prevent that main function from returning before the database listener fires. Otherwise, the program will immediately terminate and the listener will never fire. It's important to remember that all Firebase listeners are asynchronous, and Firebase manages its own daemon thread for communicating with the server.

For the case of making a simple program wait until a listener triggers, you can use this sort of pattern to block the main thread to wait for the listener:

CountDownLatch latch = new CountDownLatch(1);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        System.out.println("onDataChange: " + dataSnapshot);
        latch.countDown();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        System.out.println("onCanceled: " + databaseError);
        latch.countDown();
    }
});
latch.await();

Using a CountDownLatch is not your only option. You can do whatever you want to make sure the process doesn't stop, including simply sleeping the main thread for longer than it takes for the listener to fire.

这篇关于Firebase不会调用侦听器的onDataChange().无法在Java客户端(而非Android)上使用新的Google Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆