public abstract class RefreshableTokenSource extends Object implements TokenSource
This class supports both synchronous and asynchronous token refresh. When async is enabled, stale tokens will trigger a background refresh, while expired tokens will block until a new token is fetched.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
RefreshableTokenSource.TokenState
Enum representing the state of the token.
|
| Constructor and Description |
|---|
RefreshableTokenSource()
Constructs a new
RefreshableTokenSource with no initial token. |
RefreshableTokenSource(Token token)
Constructor with initial token.
|
| Modifier and Type | Method and Description |
|---|---|
Token |
getToken()
Gets the current token, refreshing if necessary.
|
protected Token |
getTokenAsync()
Get the current token, possibly triggering an async refresh if stale.
|
protected Token |
getTokenBlocking()
Get the current token, blocking to refresh if expired.
|
protected RefreshableTokenSource.TokenState |
getTokenState(Token t)
Determine the state of the current token (fresh, stale, or expired).
|
protected abstract Token |
refresh()
Refresh the OAuth token.
|
protected static Token |
retrieveToken(HttpClient hc,
String clientId,
String clientSecret,
String tokenUrl,
Map<String,String> params,
Map<String,String> headers,
AuthParameterPosition position)
Helper method implementing OAuth token refresh.
|
RefreshableTokenSource |
withAsyncRefresh(boolean enabled)
Enable or disable asynchronous token refresh.
|
RefreshableTokenSource |
withClockSupplier(ClockSupplier clockSupplier)
Set the clock supplier for current time.
|
RefreshableTokenSource |
withExpiryBuffer(Duration buffer)
Set the expiry buffer.
|
protected volatile Token token
public RefreshableTokenSource()
RefreshableTokenSource with no initial token.public RefreshableTokenSource(Token token)
token - The initial token to use.public RefreshableTokenSource withClockSupplier(ClockSupplier clockSupplier)
Experimental: This method may change or be removed in future releases.
clockSupplier - The clock supplier to use.public RefreshableTokenSource withAsyncRefresh(boolean enabled)
Experimental: This method may change or be removed in future releases.
enabled - true to enable async refresh, false to disablepublic RefreshableTokenSource withExpiryBuffer(Duration buffer)
Experimental: This method may change or be removed in future releases.
buffer - the expiry buffer durationprotected abstract Token refresh()
This method may throw an exception if the token cannot be refreshed. The specific exception type depends on the implementation.
public Token getToken()
This method may throw an exception if the token cannot be refreshed, depending on the
implementation of refresh().
getToken in interface TokenSourceprotected RefreshableTokenSource.TokenState getTokenState(Token t)
protected Token getTokenBlocking()
This method may throw an exception if the token cannot be refreshed, depending on the
implementation of refresh().
protected Token getTokenAsync()
This method may throw an exception if the token cannot be refreshed, depending on the
implementation of refresh().
protected static Token retrieveToken(HttpClient hc, String clientId, String clientSecret, String tokenUrl, Map<String,String> params, Map<String,String> headers, AuthParameterPosition position)
hc - The HTTP client to use for the request.clientId - The client ID to authenticate with.clientSecret - The client secret to authenticate with.tokenUrl - The authorization URL for fetching tokens.params - Additional request parameters.headers - Additional headers.position - The position of the authentication parameters in the request.DatabricksException - if the refresh failsIllegalArgumentException - if the OAuth response contains an errorCopyright © 2025. All rights reserved.