I have a blocking rest call in my code, and I would like to run it in a different context than the GlobalScope one (which is the best practice all over) but I do not understand if I also need to create a new scope for that operation as well.
On the one hand, it is said to run also on a different scope than the Global one, and with that, that effectively, scope and context are almost the same things.
I am running in a Spring WebFlux (netty) application and consuming rest calls. Consider the following code samples:
This one seems to be the "best practice one" (where this blogpost https://medium.com/@elizarov/coroutine-context-and-scope-c8b255d59055#:~:text=The%20difference%20between%20a%20context,is%20in%20their%20intended%20purpose.&text=It%20makes%20the%20scope%20in,it%20in%20the%20function%20signature. specifically says not to use CoroutineScope(Dispatchers.IO).launch
val blockingScope = CoroutineScope(Dispatchers.IO)
suspend fun one() = withContext(blockingScope.coroutineContext) {
val queryParams = arrayOf(
BasicNameValuePair("name", "key"),
)
blockingClient.get("...", JsonNode::class.java, queryParams)
}
As well, do I need to take care of the blockingScope.cancel() here?
But what prevents me from using this all the same (or how will it affect the application)
suspend fun one() = withContext(Dispatchers.IO) {
val queryParams = arrayOf(
BasicNameValuePair("name", "key"),
)
blockingClient.get("...", JsonNode::class.java, queryParams)
}