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 aCredentialsProvider
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 Classes Modifier and Type Class Description (package private) static class
DefaultCredentialsRefreshService.CredentialsProviderState
State and refresh behavior for aCredentialsProvider
and its registered entities.static class
DefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilder
Builder to create instances ofDefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilder
.private static class
DefaultCredentialsRefreshService.FixedDelayBeforeExpirationRefreshDelayStrategy
private static class
DefaultCredentialsRefreshService.FixedTimeApproachingExpirationStrategy
private static class
DefaultCredentialsRefreshService.RatioRefreshDelayStrategy
(package private) static class
DefaultCredentialsRefreshService.Registration
-
Field Summary
Fields Modifier and Type Field Description private 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.private java.util.concurrent.ConcurrentMap<CredentialsProvider,DefaultCredentialsRefreshService.CredentialsProviderState>
credentialsProviderStates
private static org.slf4j.Logger
LOGGER
private boolean
privateScheduler
private java.util.function.Function<java.time.Duration,java.time.Duration>
refreshDelayStrategy
Strategy to schedule credentials refresh after credentials retrieval.private java.util.concurrent.ScheduledExecutorService
scheduler
Scheduler 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 void
close()
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
.boolean
isApproachingExpiration(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.Runnable
refresh(java.util.concurrent.ScheduledExecutorService scheduler, DefaultCredentialsRefreshService.CredentialsProviderState credentialsProviderState, java.util.function.Function<java.time.Duration,java.time.Duration> refreshDelayStrategy)
java.lang.String
register(CredentialsProvider credentialsProvider, java.util.concurrent.Callable<java.lang.Boolean> refreshAction)
Register a new entity that needs credentials renewal.void
unregister(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
DefaultCredentialsRefreshService
is 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
refreshDelayStrategy
chosen. For example, for a validity of 60 minutes and arefreshDelayStrategy
that 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.DefaultCredentialsRefreshServiceBuilder
to 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:CredentialsRefreshService
Register 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:
register
in 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:CredentialsRefreshService
Unregister the entity with the given registration ID.Its state is cleaned up and its registered callback will not be called again.
- Specified by:
unregister
in interfaceCredentialsRefreshService
- Parameters:
credentialsProvider
- the credentials providerregistrationId
- the registration ID
-
isApproachingExpiration
public boolean isApproachingExpiration(java.time.Duration timeBeforeExpiration)
Description copied from interface:CredentialsRefreshService
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.
- Specified by:
isApproachingExpiration
in interfaceCredentialsRefreshService
- Returns:
- true if credentials should be renewed, false otherwise
-
close
public void close()
-
-