Class TokenRefresher
java.lang.Object
cloud.opencode.base.oauth2.token.TokenRefresher
- All Implemented Interfaces:
AutoCloseable
Token Refresher
Token 刷新器
Handles automatic token refresh with de-duplication.
处理自动 Token 刷新,带有去重功能。
Features | 主要功能:
- Automatic refresh before expiration - 过期前自动刷新
- Concurrent refresh de-duplication - 并发刷新去重
- Background refresh scheduling - 后台刷新调度
- Refresh failure handling - 刷新失败处理
Usage Examples | 使用示例:
// Create refresher
TokenRefresher refresher = new TokenRefresher(config, httpClient);
// Refresh a token
OAuth2Token newToken = refresher.refresh(oldToken);
// Check if refresh is needed
if (refresher.needsRefresh(token)) {
token = refresher.refresh(token);
}
Thread Safety | 线程安全:
This class is thread-safe.
此类是线程安全的。
- Since:
- JDK 25, opencode-base-oauth2 V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionTokenRefresher(OAuth2Config config, OAuth2HttpClient httpClient) Create a new token refresher 创建新的 Token 刷新器TokenRefresher(OAuth2Config config, OAuth2HttpClient httpClient, Duration refreshThreshold) Create a new token refresher with custom threshold 使用自定义阈值创建新的 Token 刷新器 -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()booleanisClosed()Check if the refresher is closed 检查刷新器是否已关闭booleanneedsRefresh(OAuth2Token token) Check if a token needs refresh 检查 Token 是否需要刷新refresh(OAuth2Token token) Refresh a token 刷新 TokenrefreshAsync(OAuth2Token token) Refresh a token asynchronously 异步刷新 TokenGet the refresh threshold 获取刷新阈值
-
Constructor Details
-
TokenRefresher
Create a new token refresher 创建新的 Token 刷新器- Parameters:
config- the OAuth2 configuration | OAuth2 配置httpClient- the HTTP client | HTTP 客户端
-
TokenRefresher
Create a new token refresher with custom threshold 使用自定义阈值创建新的 Token 刷新器- Parameters:
config- the OAuth2 configuration | OAuth2 配置httpClient- the HTTP client | HTTP 客户端refreshThreshold- the refresh threshold | 刷新阈值
-
-
Method Details
-
needsRefresh
Check if a token needs refresh 检查 Token 是否需要刷新- Parameters:
token- the token | Token- Returns:
- true if refresh needed | 如果需要刷新返回 true
-
refresh
Refresh a token 刷新 Token- Parameters:
token- the token to refresh | 要刷新的 Token- Returns:
- the new token | 新 Token
- Throws:
OAuth2Exception- if refresh fails | 如果刷新失败
-
refreshAsync
Refresh a token asynchronously 异步刷新 Token- Parameters:
token- the token to refresh | 要刷新的 Token- Returns:
- future with the new token | 包含新 Token 的 Future
-
refreshThreshold
Get the refresh threshold 获取刷新阈值- Returns:
- the refresh threshold | 刷新阈值
-
isClosed
public boolean isClosed()Check if the refresher is closed 检查刷新器是否已关闭- Returns:
- true if closed | 如果已关闭返回 true
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-