override fun uploadFileAndNotifyCadmium(
validatedPages: List<KycFile>,
documentId: UUID,
): Mono<Unit> {
validatedPages.forEach { it ->
s3Service.uploadToToxicBucket(
documentId, it.fileName })
.then()
}
return cadmiumClient.notifyCadmium(documentId) // API call to other microservice
}
I have this method where I am trying to call a method (s3Service.uploadToToxicBucket()
)which returns a Mono of Unit while iterating a list. I want to ensure that this method is called for each of the list elements. Therefore I have added then() to the resulting Mono. Also I want the method cadmiumClient.notifyCadmium()
to be executed only after all the files have been uploaded. Is this the correct way of doing this or can I use some other operator. cadmiumClient.notifyCadmium() returns a Mono of Unit as well.
Also I am calling uploadFileAndNotifyCadmium().then()
when I am calling uploadFileAndNotifyCadmium()
.
I am using then() because the execution is lazy, and I don't want to call subscribe() to make sure of the execution of these methods. Also the method uploadFileAndNotifyCadmium() is called in the controller therefore it is auto subscribed by the Spring Webflux. My understanding regarding the usage of then() maybe wrong.
Approach 2: I have also thought of doing this:
override fun uploadFileAndNotifyCadmium(
validatedPages: List<KycFile>,
documentId: UUID,
): Mono<Unit> {
val list = mutableListOf<Mono<Unit>>()
validatedPages.forEach { it ->
val result =s3Service.uploadToToxicBucket(
documentId, it.fileName })
list.add(result)
}
//Zip all the elements in list together into some variable zippedResult
return zippedResult.then(cadmiumClient.notifyCadmium(documentId)) // API call to other microservice
}
For this approach I can't find any operator to zip all the elements together when the size of list is not known before.