I need to get an access token from a server lazily, so I have this code:
object MyObj {
val accessToken: String by lazy { getAccessTokenFromHttpApi(username, password) }
}
This works well and is thread-safe, which is a requirement as I get the access token from multiple threads.
Now I learn that this server gives out access tokens that expire after an hour. If the application has been running for more than an hour, the access token will be rejected. When that happens, it needs to be refreshed using another call to getAccessToken...()
. However, I can't refresh the value of accessToken
as it's a val, which is immutable. How can I refresh while still keeping it thread-safe?