Class AsyncRefreshableSupplier<V,E extends Exception>
java.lang.Object
org.forgerock.openig.tools.authentication.chf.AsyncRefreshableSupplier<V,E>
- Type Parameters:
V
- input type of the function parameterE
- supported exception type of the function parameter
This utility class supports a lock-downgrading strategy to make sure that 2 concurrent calls to compute
the "cached" value will result in a single computation. It supports concurrent refreshment.
-
Constructor Summary
ConstructorDescriptionAsyncRefreshableSupplier
(AsyncFunction<Context, V, E> compute) Creates a provider ensuring "only-once" invocation of the given compute function. -
Method Summary
Modifier and TypeMethodDescriptionComputes (or re-computes) the value in a thread-safe manner, making sure that only 1 thread will compute the value, the other being blocked, waiting for the result to be computed.Computes the value in a thread-safe manner, making sure that only 1 thread will compute the value, the other being blocked, waiting for the result to be computed.void
Flag the current token as being invalid to force a refresh on next get().Refresh (and returns) the value, in a thread-safe manner.
-
Constructor Details
-
AsyncRefreshableSupplier
Creates a provider ensuring "only-once" invocation of the given compute function.- Parameters:
compute
- computation function
-
-
Method Details
-
get
Computes (or re-computes) the value in a thread-safe manner, making sure that only 1 thread will compute the value, the other being blocked, waiting for the result to be computed.- Parameters:
context
- Service context- Returns:
- computed promise
-
refresh
Refresh (and returns) the value, in a thread-safe manner.It works like this:
- The thread try to take the refresh lock, if it succeed that means that no refresh is in progress, therefore it can safely invalidate the value and returns the newly computed value.
- Other concurrent threads will not have the lock (using the else branch of the condition) and will all be together waiting for the lock to be released.
- When the lock is released, all concurrent threads are notified (one after the other) and returns the refreshed value.
- Parameters:
context
- Service context- Returns:
- refreshed value
-
getWithoutRefresh
Computes the value in a thread-safe manner, making sure that only 1 thread will compute the value, the other being blocked, waiting for the result to be computed. If the value needs to be refreshednull
is returned.- Returns:
- computed promise or null if the promise is not valid and needs refresh.
-
invalidate
public void invalidate()Flag the current token as being invalid to force a refresh on next get().
-