Class DefaultCredentialsRefreshService
- java.lang.Object
-
- com.rabbitmq.client.impl.DefaultCredentialsRefreshService
-
- All Implemented Interfaces:
CredentialsRefreshService
public class DefaultCredentialsRefreshService extends java.lang.Object implements CredentialsRefreshService
Scheduling-based implementation ofCredentialsRefreshService.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 aCredentialsProviderhave 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 Classes Modifier and Type Class Description (package private) static classDefaultCredentialsRefreshService.CredentialsProviderStateState and refresh behavior for aCredentialsProviderand its registered entities.static classDefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilderBuilder to create instances ofDefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilder.private static classDefaultCredentialsRefreshService.FixedDelayBeforeExpirationRefreshDelayStrategyprivate static classDefaultCredentialsRefreshService.FixedTimeApproachingExpirationStrategyprivate static classDefaultCredentialsRefreshService.RatioRefreshDelayStrategy(package private) static classDefaultCredentialsRefreshService.Registration
-
Field Summary
Fields Modifier and Type Field Description private java.util.function.Function<java.time.Duration,java.lang.Boolean>approachingExpirationStrategyStrategy to provide a hint about whether credentials should be renewed now or not before attempting to connect.private java.util.concurrent.ConcurrentMap<CredentialsProvider,DefaultCredentialsRefreshService.CredentialsProviderState>credentialsProviderStatesprivate static org.slf4j.LoggerLOGGERprivate booleanprivateSchedulerprivate java.util.function.Function<java.time.Duration,java.time.Duration>refreshDelayStrategyStrategy to schedule credentials refresh after credentials retrieval.private java.util.concurrent.ScheduledExecutorServiceschedulerScheduler used to schedule credentials refresh.
-
Constructor Summary
Constructors Constructor Description DefaultCredentialsRefreshService(java.util.concurrent.ScheduledExecutorService scheduler, java.util.function.Function<java.time.Duration,java.time.Duration> refreshDelayStrategy, java.util.function.Function<java.time.Duration,java.lang.Boolean> approachingExpirationStrategy)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()static java.util.function.Function<java.time.Duration,java.time.Duration>fixedDelayBeforeExpirationRefreshDelayStrategy(java.time.Duration duration)Delay before refresh istime before expiration - specified duration.static java.util.function.Function<java.time.Duration,java.lang.Boolean>fixedTimeApproachingExpirationStrategy(java.time.Duration limitBeforeExpiration)Advise to refresh credentials ifTTL <= limit.booleanisApproachingExpiration(java.time.Duration timeBeforeExpiration)Provide a hint about whether credentials should be renewed now or not before attempting to connect.static java.util.function.Function<java.time.Duration,java.time.Duration>ratioRefreshDelayStrategy(double ratio)Delay before refresh is a ratio of the time before expiration.private static java.lang.Runnablerefresh(java.util.concurrent.ScheduledExecutorService scheduler, DefaultCredentialsRefreshService.CredentialsProviderState credentialsProviderState, java.util.function.Function<java.time.Duration,java.time.Duration> refreshDelayStrategy)java.lang.Stringregister(CredentialsProvider credentialsProvider, java.util.concurrent.Callable<java.lang.Boolean> refreshAction)Register a new entity that needs credentials renewal.voidunregister(CredentialsProvider credentialsProvider, java.lang.String registrationId)Unregister the entity with the given registration ID.
-
-
-
Field Detail
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
scheduler
private final java.util.concurrent.ScheduledExecutorService 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 java.util.concurrent.ConcurrentMap<CredentialsProvider,DefaultCredentialsRefreshService.CredentialsProviderState> credentialsProviderStates
-
privateScheduler
private final boolean privateScheduler
-
refreshDelayStrategy
private final java.util.function.Function<java.time.Duration,java.time.Duration> 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).
-
approachingExpirationStrategy
private final java.util.function.Function<java.time.Duration,java.lang.Boolean> 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 Detail
-
DefaultCredentialsRefreshService
public DefaultCredentialsRefreshService(java.util.concurrent.ScheduledExecutorService scheduler, java.util.function.Function<java.time.Duration,java.time.Duration> refreshDelayStrategy, java.util.function.Function<java.time.Duration,java.lang.Boolean> approachingExpirationStrategy)Constructor. Consider usingDefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilderto create instances.- Parameters:
scheduler-refreshDelayStrategy-approachingExpirationStrategy-
-
-
Method Detail
-
ratioRefreshDelayStrategy
public static java.util.function.Function<java.time.Duration,java.time.Duration> ratioRefreshDelayStrategy(double ratio)
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 java.util.function.Function<java.time.Duration,java.time.Duration> fixedDelayBeforeExpirationRefreshDelayStrategy(java.time.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 java.util.function.Function<java.time.Duration,java.lang.Boolean> fixedTimeApproachingExpirationStrategy(java.time.Duration limitBeforeExpiration)
Advise to refresh credentials ifTTL <= limit.- Parameters:
limitBeforeExpiration-- Returns:
- true if credentials should be refreshed, false otherwise
-
refresh
private static java.lang.Runnable refresh(java.util.concurrent.ScheduledExecutorService scheduler, DefaultCredentialsRefreshService.CredentialsProviderState credentialsProviderState, java.util.function.Function<java.time.Duration,java.time.Duration> refreshDelayStrategy)
-
register
public java.lang.String register(CredentialsProvider credentialsProvider, java.util.concurrent.Callable<java.lang.Boolean> refreshAction)
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
public void unregister(CredentialsProvider credentialsProvider, java.lang.String registrationId)
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
public boolean isApproachingExpiration(java.time.Duration timeBeforeExpiration)
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- Returns:
- true if credentials should be renewed, false otherwise
-
close
public void close()
-
-