Class AsyncRefreshableSupplier<V,E extends Exception>
- Type Parameters:
V- input type of the function parameterE- supported exception type of the function parameter
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classAsyncRefreshableSupplier.Ticket<V,E extends Exception> A holder for the currently active promise of this supplier. -
Constructor Summary
ConstructorsConstructorDescriptionAsyncRefreshableSupplier(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.voidinvalidate(AsyncRefreshableSupplier.Ticket<V, E> heldTicket) Flag the value carried byheldTicketas being invalid, to force a recomputation on the nextget(Context).refresh(Context context, AsyncRefreshableSupplier.Ticket<V, E> heldTicket) Returns a refreshedAsyncRefreshableSupplier.Ticket, 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:
- a ticket of the computed promise
-
refresh
public AsyncRefreshableSupplier.Ticket<V,E> refresh(Context context, AsyncRefreshableSupplier.Ticket<V, E> heldTicket) Returns a refreshedAsyncRefreshableSupplier.Ticket, in a thread-safe manner.heldTicketis theAsyncRefreshableSupplier.Ticketthe caller was previously given byget(Context)and has since been flagged as invalid when used. If theAsyncRefreshableSupplier.Tickethas already been replaced then the caller gets that replacement back instead of triggering another computation, so one rejectedAsyncRefreshableSupplier.Ticketleads to one recomputation however many callers report it. This matters when computing a new ticket invalidates the previous one — for example, an AM agent limited to a single session, where a recomputation would revoke the value a concurrent caller is currently using.- Parameters:
context- Service contextheldTicket- theAsyncRefreshableSupplier.Ticketthe caller currently holds- Returns:
- a refreshed
AsyncRefreshableSupplier.Ticket
-
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 refreshed, aAsyncRefreshableSupplier.Ticketcontaining anullresultPromiseis returned.- Returns:
- a ticket containing the current valid promise, or a null result promise if the value is not valid
-
invalidate
Flag the value carried byheldTicketas being invalid, to force a recomputation on the nextget(Context).If that
AsyncRefreshableSupplier.Tickethas already been replaced then the report is ignored, leaving the replacement in place. This matters when computing a new value invalidates the previous one — an unconditional invalidation would discard a value another caller is currently using, and every discarded value costs a recomputation.- Parameters:
heldTicket- theAsyncRefreshableSupplier.Ticketthe caller found unusable
-