I’m trying to implement the BulkWriter from Google Cloud Firestore in Kotlin.
When I create documents individually (without the BulkWriter) my application works normally. However, when using the BulkWriter instead everything works as expected but the JVM won’t shutdown at the end.
To reproduce the issue I’ve written a small test file:
import com.google.cloud.firestore.FirestoreOptions
fun main() {
val firestore = FirestoreOptions.newBuilder()
.setEmulatorHost("localhost:8768")
.setProjectId("bulkwritertest")
.build().service
// Using BulkWriter to create documents. Documents are created but JVM won't shutdown using this
firestore.bulkWriter().use { bulk ->
repeat(10) { index ->
bulk.create(
firestore.collection("bulk").document("test-" + index),
mapOf("name" to "test " + index)
)
}
}
// Not using BulkWriter to create documents. Documents are created and JVM will shutdown at the end
// Disable the BulkWriter block and enable this block to test
// repeat(10) { index ->
// firestore.collection("bulk").document("test-" + index)
// .create(mapOf("name" to "test " + index)).get()
// }
// In all cases the application will print all documents created (i.e. the BulkWriter is actually finished and closed)
firestore.collection("bulk").listDocuments()
.forEach { document ->
println(document.get().get().data)
}
}
In all cases (BulkWriter or no BulkWriter) this function will get to print all the documents at the end. So there is no blocking operation within the use
block of the BulkWriter and the BulkWriter must’ve been closed at this point.
Since there is no other code, I’d expect the JVM to shutdown at this point (which it does when using the code in comments that is not using the BulkWriter)
I can’t find good documentation or samples on the use of BulkWriter with Java/Kotlin. Are there mechanics I should be aware about? Am I doing something wrong on the usage?