Package io.camunda.client.impl.oauth
Class OAuthCredentialsCache
java.lang.Object
io.camunda.client.impl.oauth.OAuthCredentialsCache
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an in-memory-only credentials cache.OAuthCredentialsCache(File cacheFile) Creates a credentials cache backed bycacheFile. -
Method Summary
Modifier and TypeMethodDescriptioncomputeIfMissingOrInvalid(String clientId, SupplierWithIO<CamundaClientCredentials> zeebeClientCredentialsConsumer) computeIfMissingOrInvalid(String clientId, SupplierWithIO<CamundaClientCredentials> zeebeClientCredentialsConsumer, Runnable proactiveRefreshCallback, Duration proactiveTokenRefreshThreshold) Returns a valid cached token, or fetches a new one if missing/invalid.booleanforceRefreshIfChanged(String clientId, SupplierWithIO<CamundaClientCredentials> credentialsSupplier) Ensures that a fresh token is available in the cache, and returnstrueif the caller should retry its request (i.e. credentials were refreshed — either by this thread or by a concurrent one that completed while this thread was waiting for the lock).put(String clientId, CamundaClientCredentials credentials) voidputAndWrite(String clientId, CamundaClientCredentials credentials) Atomically puts credentials into the cache and writes to disk.intsize()<T> Optional<T> withCache(String clientId, FunctionWithIO<CamundaClientCredentials, T> function) void
-
Constructor Details
-
OAuthCredentialsCache
public OAuthCredentialsCache()Creates an in-memory-only credentials cache. Equivalent toOAuthCredentialsCache(File)withnull— no filesystem persistence; tokens live only for the lifetime of this instance. -
OAuthCredentialsCache
Creates a credentials cache backed bycacheFile. IfcacheFileisnull, the cache operates in memory only:readCache()andwriteCache()become no-ops and tokens are not persisted across restarts. IfcacheFileis non-null, the file (and its parent directories, if missing) are created on first write and used to round-trip credentials between runs.
-
-
Method Details
-
readCache
- Throws:
IOException
-
writeCache
- Throws:
IOException
-
get
-
computeIfMissingOrInvalid
public CamundaClientCredentials computeIfMissingOrInvalid(String clientId, SupplierWithIO<CamundaClientCredentials> zeebeClientCredentialsConsumer) throws IOException - Throws:
IOException
-
computeIfMissingOrInvalid
public CamundaClientCredentials computeIfMissingOrInvalid(String clientId, SupplierWithIO<CamundaClientCredentials> zeebeClientCredentialsConsumer, Runnable proactiveRefreshCallback, Duration proactiveTokenRefreshThreshold) throws IOException Returns a valid cached token, or fetches a new one if missing/invalid. When aproactiveRefreshCallbackis provided and the cached token is valid but withinproactiveTokenRefreshThresholdof expiry (as determined byCamundaClientCredentials.shouldRefreshProactively(Duration)), the callback is invoked to trigger a background refresh while the still-valid token is returned immediately.This method first checks the in-memory cache (a fast HashMap lookup) before falling back to reading from the on-disk YAML cache. Under high throughput (~300 req/s), this avoids hundreds of file reads per second while holding the synchronized lock.
- Throws:
IOException
-
forceRefreshIfChanged
public boolean forceRefreshIfChanged(String clientId, SupplierWithIO<CamundaClientCredentials> credentialsSupplier) throws IOException Ensures that a fresh token is available in the cache, and returnstrueif the caller should retry its request (i.e. credentials were refreshed — either by this thread or by a concurrent one that completed while this thread was waiting for the lock).When a concurrent call already refreshed the token (detected via a generation counter read before acquiring the monitor), the expensive OAuth HTTP call is skipped and
trueis returned immediately. This reduces lock hold time from N × ~200ms (where N is the number of concurrent 401 retries) to ~200ms + (N−1) × O(μs), preventing a cascading freeze of the HTTP client's I/O reactor.- Returns:
trueif credentials were refreshed (by any thread) and the request should be retried;falseif the freshly fetched credentials are identical to the previously cached ones (i.e. the 401 was not caused by a stale token)- Throws:
IOException
-
putAndWrite
Atomically puts credentials into the cache and writes to disk. This method is synchronized on the same monitor ascomputeIfMissingOrInvalid(java.lang.String, io.camunda.client.impl.util.SupplierWithIO<io.camunda.client.impl.CamundaClientCredentials>), ensuring that a concurrentreadCache()cannot clobber the in-memory state between the put and the write. This is designed for the background proactive refresh: the caller fetches the token outside any lock (the slow HTTP call), then calls this method to briefly acquire the monitor for the fast put+write.- Throws:
IOException
-
withCache
public <T> Optional<T> withCache(String clientId, FunctionWithIO<CamundaClientCredentials, T> function) throws IOException- Throws:
IOException
-
put
-
size
public int size()
-