Firebase admin sdk java 实时数据库监听器
Firebase admin sdk java listener for realtime databse
我正在尝试在 Kotlin 中实现 firebase admin SDK。我想在实时数据库中有一个侦听器来侦听来自该数据库的更新。
我的问题是当我 运行 主函数时,运行 开始并立即完成该过程,而没有实时数据库中的监听更新。
这是我的代码:
fun main() {
val serviceAccount = FileInputStream("path/to/key.json")
val options = FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("URLDATABASE")
.build()
FirebaseApp.initializeApp(options)
val ref = FirebaseDatabase
.getInstance()
.getReference("/waitingList")
ref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
print("TEST")
}
override fun onCancelled(databaseError: DatabaseError) {
println("The read failed: " + databaseError.code)
}
})
}
你的程序应该什么时候停止?你可以:
1 -> 在新协程中启动它:
fun main() {
GlobalScope.launch { // launch a new coroutine in background and continue
val serviceAccount = FileInputStream("path/to/key.json")
val options = FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("URLDATABASE")
.build()
FirebaseApp.initializeApp(options)
val ref = FirebaseDatabase
.getInstance()
.getReference("/waitingList")
ref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
print("TEST")
}
override fun onCancelled(databaseError: DatabaseError) {
println("The read failed: " + databaseError.code)
}
})
}
}
2 -> 最后再睡一会:
fun main() {
val serviceAccount = FileInputStream("path/to/key.json")
val options = FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("URLDATABASE")
.build()
FirebaseApp.initializeApp(options)
val ref = FirebaseDatabase
.getInstance()
.getReference("/waitingList")
ref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
print("TEST")
}
override fun onCancelled(databaseError: DatabaseError) {
println("The read failed: " + databaseError.code)
}
})
while (true) {
Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive
}
}
我正在尝试在 Kotlin 中实现 firebase admin SDK。我想在实时数据库中有一个侦听器来侦听来自该数据库的更新。 我的问题是当我 运行 主函数时,运行 开始并立即完成该过程,而没有实时数据库中的监听更新。
这是我的代码:
fun main() {
val serviceAccount = FileInputStream("path/to/key.json")
val options = FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("URLDATABASE")
.build()
FirebaseApp.initializeApp(options)
val ref = FirebaseDatabase
.getInstance()
.getReference("/waitingList")
ref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
print("TEST")
}
override fun onCancelled(databaseError: DatabaseError) {
println("The read failed: " + databaseError.code)
}
})
}
你的程序应该什么时候停止?你可以: 1 -> 在新协程中启动它:
fun main() {
GlobalScope.launch { // launch a new coroutine in background and continue
val serviceAccount = FileInputStream("path/to/key.json")
val options = FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("URLDATABASE")
.build()
FirebaseApp.initializeApp(options)
val ref = FirebaseDatabase
.getInstance()
.getReference("/waitingList")
ref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
print("TEST")
}
override fun onCancelled(databaseError: DatabaseError) {
println("The read failed: " + databaseError.code)
}
})
}
}
2 -> 最后再睡一会:
fun main() {
val serviceAccount = FileInputStream("path/to/key.json")
val options = FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("URLDATABASE")
.build()
FirebaseApp.initializeApp(options)
val ref = FirebaseDatabase
.getInstance()
.getReference("/waitingList")
ref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
print("TEST")
}
override fun onCancelled(databaseError: DatabaseError) {
println("The read failed: " + databaseError.code)
}
})
while (true) {
Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive
}
}