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 TypeClassDescriptionstatic classBuilder to create instances ofDefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilder. -
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.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.
-
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
-
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()
-