Class DefaultCredentialsRefreshService
- All Implemented Interfaces:
CredentialsRefreshService
CredentialsRefreshService.
This implementation keeps track of entities (typically AMQP connections) that need
to renew credentials. Token renewal is scheduled based on token expiration, using
a Function<Duration, Long> refreshDelayStrategy. Once credentials
for a CredentialsProvider have been renewed, the callback registered
by each entity/connection is performed. This callback typically propagates
the new credentials in the entity state, e.g. sending the new password to the
broker for AMQP connections.
Instances are preferably created with DefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilder.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classState and refresh behavior for aCredentialsProviderand its registered entities.static classBuilder to create instances ofDefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilder.private static classprivate static classprivate static class(package private) static class -
Field Summary
FieldsModifier and TypeFieldDescriptionStrategy to provide a hint about whether credentials should be renewed now or not before attempting to connect.private final ConcurrentMap<CredentialsProvider, DefaultCredentialsRefreshService.CredentialsProviderState> private static final org.slf4j.Loggerprivate final booleanStrategy to schedule credentials refresh after credentials retrieval.private final ScheduledExecutorServiceScheduler used to schedule credentials refresh. -
Constructor Summary
ConstructorsConstructorDescriptionDefaultCredentialsRefreshService(ScheduledExecutorService scheduler, Function<Duration, Duration> refreshDelayStrategy, Function<Duration, Boolean> approachingExpirationStrategy) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Delay before refresh istime before expiration - specified duration.fixedTimeApproachingExpirationStrategy(Duration limitBeforeExpiration) Advise to refresh credentials ifTTL <= limit.booleanisApproachingExpiration(Duration timeBeforeExpiration) Provide a hint about whether credentials should be renewed now or not before attempting to connect.ratioRefreshDelayStrategy(double ratio) Delay before refresh is a ratio of the time before expiration.private static Runnablerefresh(ScheduledExecutorService scheduler, DefaultCredentialsRefreshService.CredentialsProviderState credentialsProviderState, Function<Duration, Duration> refreshDelayStrategy) register(CredentialsProvider credentialsProvider, Callable<Boolean> refreshAction) Register a new entity that needs credentials renewal.voidunregister(CredentialsProvider credentialsProvider, String registrationId) Unregister the entity with the given registration ID.
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGER -
scheduler
Scheduler used to schedule credentials refresh.Default is a single-threaded scheduler, which should be enough for most scenarios, assuming that credentials expire after a few minutes or hours. This default scheduler is automatically disposed of when the
DefaultCredentialsRefreshServiceis closed.If an external scheduler is passed in, it is the developer's responsibility to close it.
-
credentialsProviderStates
private final ConcurrentMap<CredentialsProvider,DefaultCredentialsRefreshService.CredentialsProviderState> credentialsProviderStates -
privateScheduler
private final boolean privateScheduler -
refreshDelayStrategy
Strategy to schedule credentials refresh after credentials retrieval.Typical strategies schedule refresh after a ratio of the time before expiration (e.g. 80 % of the time before expiration) or after a fixed time before expiration (e.g. 20 seconds before credentials expire).
- See Also:
-
approachingExpirationStrategy
Strategy to provide a hint about whether credentials should be renewed now or not before attempting to connect.This can avoid a connection to use almost expired credentials if this connection is created just before credentials are refreshed in the background, but does not benefit from the refresh.
Note setting such a strategy may require knowledge of the credentials validity and must be consistent with the
refreshDelayStrategychosen. For example, for a validity of 60 minutes and arefreshDelayStrategythat instructs to refresh 10 minutes before credentials expire, this strategy could hint that credentials that expire in 11 minutes or less (1 minute before a refresh is actually scheduled) should be refreshed, which would trigger an early refresh.The default strategy always return false.
-
-
Constructor Details
-
DefaultCredentialsRefreshService
public DefaultCredentialsRefreshService(ScheduledExecutorService scheduler, Function<Duration, Duration> refreshDelayStrategy, Function<Duration, Boolean> approachingExpirationStrategy) Constructor. Consider usingDefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilderto create instances.- Parameters:
scheduler-refreshDelayStrategy-approachingExpirationStrategy-
-
-
Method Details
-
ratioRefreshDelayStrategy
Delay before refresh is a ratio of the time before expiration.E.g. if time before expiration is 60 minutes and specified ratio is 0.8, refresh will be scheduled in 60 x 0.8 = 48 minutes.
- Parameters:
ratio-- Returns:
- the delay before refreshing
-
fixedDelayBeforeExpirationRefreshDelayStrategy
public static Function<Duration,Duration> fixedDelayBeforeExpirationRefreshDelayStrategy(Duration duration) Delay before refresh istime before expiration - specified duration.E.g. if time before expiration is 60 minutes and specified duration is 10 minutes, refresh will be scheduled in 60 - 10 = 50 minutes.
- Parameters:
duration-- Returns:
- the delay before refreshing
-
fixedTimeApproachingExpirationStrategy
public static Function<Duration,Boolean> fixedTimeApproachingExpirationStrategy(Duration limitBeforeExpiration) Advise to refresh credentials ifTTL <= limit.- Parameters:
limitBeforeExpiration-- Returns:
- true if credentials should be refreshed, false otherwise
-
refresh
private static Runnable refresh(ScheduledExecutorService scheduler, DefaultCredentialsRefreshService.CredentialsProviderState credentialsProviderState, Function<Duration, Duration> refreshDelayStrategy) -
register
Description copied from interface:CredentialsRefreshServiceRegister a new entity that needs credentials renewal.The registered callback must return true if the action was performed correctly, throw an exception if something goes wrong, and return false if it became stale and wants to be unregistered.
Implementations are free to automatically unregister an entity whose callback has failed a given number of times.
- Specified by:
registerin interfaceCredentialsRefreshService- Parameters:
credentialsProvider- the credentials providerrefreshAction- the action to perform after credentials renewal- Returns:
- a tracking ID for the registration
-
unregister
Description copied from interface:CredentialsRefreshServiceUnregister the entity with the given registration ID.Its state is cleaned up and its registered callback will not be called again.
- Specified by:
unregisterin interfaceCredentialsRefreshService- Parameters:
credentialsProvider- the credentials providerregistrationId- the registration ID
-
isApproachingExpiration
Description copied from interface:CredentialsRefreshServiceProvide a hint about whether credentials should be renewed now or not before attempting to connect.This can avoid a connection to use almost expired credentials if this connection is created just before credentials are refreshed in the background, but does not benefit from the refresh.
- Specified by:
isApproachingExpirationin interfaceCredentialsRefreshService- Parameters:
timeBeforeExpiration-- Returns:
- true if credentials should be renewed, false otherwise
-
close
public void close()
-