Firebase Java SDK不会保留数据,也不会触发回调。任务似乎永远不会完成

时间:2017-04-06 13:05:43

标签: java scala firebase

我正在尝试根据本指南https://firebase.google.com/docs/database/server/start并使用Play Framework + Scala在Firebase上编写一些数据。

调用DatabaseReference.setValue后没有任何反应:Firebase上没有持久保存数据,也没有触发我的回调。

以下是代码:

  lazy val conf = ConfigFactory.load()

  lazy val options = new FirebaseOptions.Builder()
                                        .setCredential(FirebaseCredentials.fromCertificate(new FileInputStream(conf.getString("firebase.credentials.path"))))
                                        .setDatabaseUrl("https://orsz-xxxxx-xxxxx.firebaseio.com/")
                                        .build()

  lazy val propsDB = FirebaseDatabase.getInstance().getReference()

  /** Initialize block to instantiates FirebaseApp **/
  {
    println("[FirebasePersistence] - Initialize FirebaseApp")
    FirebaseApp.initializeApp(options)
    FirebaseDatabase.getInstance().setLogLevel(Logger.Level.DEBUG)
    println("[FirebasePersistence] - Initialize FirebaseApp Done!")
  }

  def persistProposition(prop: Proposition): Future[Proposition] = Future {
    val task = propsDB.setValue(1, new DatabaseReference.CompletionListener() {
      override def onComplete(databaseError: DatabaseError, databaseReference: DatabaseReference) {
        if (databaseError != null) {
          println("Error: " + databaseError.getMessage)
        } else {
          println("[FirebasePersistence.persistProposition] - OK")
        }
      }
    })
    prop
  }

我可以看到,在SBT控制台上测试,即使在几秒钟后,setValue上生成的任务也永远不会完成:

scala> task.isComplete
 res6: Boolean = false

使用Firebase日志级别DEBUG执行我得到此日志:

Thu Apr 06 08:53:37 BRT 2017 [DEBUG] PersistentConnection: pc_0 - Trying to fetch auth token
Thu Apr 06 08:53:37 BRT 2017 [DEBUG] RepoOperation: set: /
Thu Apr 06 08:53:37 BRT 2017 [DEBUG] DataOperation: set: / 1
Thu Apr 06 08:53:37 BRT 2017 [DEBUG] RepoOperation: Aborting transactions for path: /. Affected: /
Thu Apr 06 08:53:38 BRT 2017 [DEBUG] PersistentConnection: pc_0 - Successfully fetched token, opening connection
Thu Apr 06 08:53:38 BRT 2017 [DEBUG] Connection: conn_0 - Opening a connection
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] WebSocket: ws_0 - websocket opened
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] WebSocket: ws_0 - Reset keepAlive
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] WebSocket: ws_0 - ws message: {"t":"c","d":{"t":"h","d":{"ts":1491479619239,"v":"5","h":"s-usc1c-nss-135.firebaseio.com","s":"bxE2HQCxtNNs4EznaO5xjWsInnIpu5Fi"}}}
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] WebSocket: ws_0 - Reset keepAlive. Remaining: 44991
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] WebSocket: ws_0 - HandleNewFrameCount: 1
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] WebSocket: ws_0 - handleIncomingFrame complete frame: {t=c, d={t=h, d={h=s-usc1c-nss-135.firebaseio.com, s=bxE2HQCxtNNs4EznaO5xjWsInnIpu5Fi, v=5, ts=1491479619239}}}
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] Connection: conn_0 - Got control message: {t=h, d={h=s-usc1c-nss-135.firebaseio.com, s=bxE2HQCxtNNs4EznaO5xjWsInnIpu5Fi, v=5, ts=1491479619239}}
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] Connection: conn_0 - realtime connection established
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] PersistentConnection: pc_0 - onReady
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] PersistentConnection: pc_0 - handling timestamp
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] PersistentConnection: pc_0 - Sending first connection stats
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] Connection: conn_0 - Sending data: {t=d, d={a=s, r=0, b={c={sdk.java.3-0-0=1}}}}
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] WebSocket: ws_0 - Reset keepAlive. Remaining: 44993
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] PersistentConnection: pc_0 - calling restore state
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] PersistentConnection: pc_0 - Restoring auth.
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] Connection: conn_0 - Sending data (contents hidden)
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] WebSocket: ws_0 - Reset keepAlive. Remaining: 44996
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] WebSocket: ws_0 - ws message: {"t":"d","d":{"r":0,"b":{"s":"ok","d":""}}}
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] WebSocket: ws_0 - Reset keepAlive. Remaining: 44836
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] WebSocket: ws_0 - HandleNewFrameCount: 1
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] WebSocket: ws_0 - handleIncomingFrame complete frame: {t=d, d={r=0, b={d=, s=ok}}}
Thu Apr 06 08:53:39 BRT 2017 [DEBUG] Connection: conn_0 - received data message: {r=0, b={d=, s=ok}}
Thu Apr 06 08:53:40 BRT 2017 [DEBUG] WebSocket: ws_0 - ws message: {"t":"d","d":{"r":1,"b":{"s":"permission_denied","d":"Access denied."}}}
Thu Apr 06 08:53:40 BRT 2017 [DEBUG] WebSocket: ws_0 - Reset keepAlive. Remaining: 43628
Thu Apr 06 08:53:40 BRT 2017 [DEBUG] WebSocket: ws_0 - HandleNewFrameCount: 1
Thu Apr 06 08:53:40 BRT 2017 [DEBUG] WebSocket: ws_0 - handleIncomingFrame complete frame: {t=d, d={r=1, b={d=Access denied., s=permission_denied}}}
Thu Apr 06 08:53:40 BRT 2017 [DEBUG] Connection: conn_0 - received data message: {r=1, b={d=Access denied., s=permission_denied}}
Thu Apr 06 08:53:40 BRT 2017 [DEBUG] PersistentConnection: pc_0 - Authentication failed: permission_denied (Access denied.)
Thu Apr 06 08:53:40 BRT 2017 [DEBUG] Connection: conn_0 - closing realtime connection
Thu Apr 06 08:53:40 BRT 2017 [DEBUG] WebSocket: ws_0 - websocket is being closed
Thu Apr 06 08:53:40 BRT 2017 [DEBUG] PersistentConnection: pc_0 - Got on disconnect due to OTHER
Thu Apr 06 08:53:40 BRT 2017 [DEBUG] PersistentConnection: pc_0 - Scheduling connection attempt
Thu Apr 06 08:53:40 BRT 2017 [DEBUG] ConnectionRetryHelper: Scheduling retry in 821ms
Thu Apr 06 08:53:41 BRT 2017 [DEBUG] WebSocket: ws_0 - closed
Thu Apr 06 08:53:41 BRT 2017 [DEBUG] PersistentConnection: pc_0 - Trying to fetch auth token
Thu Apr 06 08:53:41 BRT 2017 [DEBUG] PersistentConnection: pc_0 - Successfully fetched token, opening connection

我可以在日志中看到一些拒绝权限,但无法找出原因。

我已经在FileInputStream上测试了凭据,但没关系。

Firebase项目的权限:

{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}

我已经使用firebase-admin-4.1.6和firebase-server-sdk-3.0.1进行了测试并得到了相同的结果

编辑: 我在调用setValue之前打印了FirebaseApp,FirebaseDatabase和DatabaseReference对象并得到了上面的行。 我如何确保正确初始化admin sdk?

FirebaseApp=FirebaseApp{name=[DEFAULT], options=FirebaseOptions{databaseUrl=https://orsz-xxxxx-xxxxx.firebaseio.com/, credential=com.google.firebase.auth.FirebaseCredentials$CertCredential@558b77e8, databaseAuthVariableOverride={}}}
FirebaseApp.name=[DEFAULT]

FirebaseDatabase.getInstance()=com.google.firebase.database.FirebaseDatabase@124cb42b

DatabaseReference=https://orsz-xxxxx-xxxxx.firebaseio.com/12345

1 个答案:

答案 0 :(得分:0)

我刚刚测试了一个新项目和Admin SDK版本4.1.6。

使用此Java代码:

final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("43256111");
ref.setValue(1, new DatabaseReference.CompletionListener() {
    @Override
    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
        if (databaseError != null) {
            System.err.println(databaseError);
        } else {
            System.out.println("Written");
        }
    }
});

43256111是我用于测试的数据库中的位置。

几秒钟后我就明白了:

  

在这种情况下,规则甚至不重要,因为Admin SDK以管理权限运行,它甚至绕过我的这些规则:

"43256111": {
  ".write": false
}

所以我不确定你的情况会发生什么,但我无法重现它。您确定已正确初始化管理员SDK吗?