I have a Ktor
class where I need to do action on unauthorized exception(when token is expired), for this action I need to have synchronized
action, otherwise it is not working correctly, the problem is that @Synchronized
is not synchronize the action and is not waiting for action to finish for next one.
fun ktorFunction(){
HttpResponseValidator {
handleResponseException { exception ->
kermit.e { "In ${exception.message}" }
val clientException =
exception as? ClientRequestException ?: return@handleResponseException
val exceptionResponse = clientException.response
when (exceptionResponse.status) {
HttpStatusCode.Unauthorized -> {
test(){
kermit.v { "Error message" }
}
}
}
}
}
@Synchronized
fun test(messageTest: () -> Unit) {
CoroutineScope(Dispatchers.Default).launch {
delay(3000)
messageTest()
}
}
The idea is I want test
function to not be called from other thread until it is finished, whatever the action is in it.