Skip to main content

Concurrent Package

The org.apache.juneau.commons.concurrent package provides small concurrency helpers that make locks and atomic references work cleanly with try-with-resources and Optional-style APIs.

Auto-Closeable Locks​

SimpleLock​

An AutoCloseable wrapper around a java.util.concurrent.locks.Lock that acquires the lock on construction and releases it on close(), so it can be used directly in a try-with-resources block. The SimpleLock.NO_OP constant is a do-nothing lock.

try (SimpleLock ignored = new SimpleLock(myLock)) {
// critical section — lock released automatically
}

SimpleReadWriteLock​

An extension of ReentrantReadWriteLock whose read() and write() methods return auto-closeable SimpleLock instances. SimpleReadWriteLock.NO_OP is a do-nothing variant.

SimpleReadWriteLock rw = new SimpleReadWriteLock();

try (SimpleLock ignored = rw.read()) {
// read-locked section
}
try (SimpleLock ignored = rw.write()) {
// write-locked section
}

Atomic References​

NullableReference​

A thread-safe reference that combines AtomicReference with Optional-like convenience methods (isPresent(), map(), flatMap(), filter(), of()/ofNullable()/empty()).

NullableReference<String> ref = NullableReference.of("hello");
boolean present = ref.isPresent();
NullableReference<Integer> len = ref.map(String::length);

Replay Detection​

ReplayCache​

An SPI for detecting reuse ("replay") of a one-time-use identifier, such as a nonce or a single-use token. checkAndRecord(String, long) atomically checks whether an identifier has been seen before and records it as seen, as one indivisible operation — of any two concurrent calls with the same identifier, at most one returns true (first-seen).

When the backing store can't answer (for example a remote store is unreachable), the consumer decides how the failure is resolved by passing a ReplayCache.FailMode to checkAndRecord(String, long, ReplayCache.FailMode, Consumer):

  • ReplayCache.FailMode.FAIL_CLOSED (the safe default) — treats a store failure as a replay (reject).
  • ReplayCache.FailMode.FAIL_OPEN — treats a store failure as first-seen (allow), degrading to the multi-use-tolerant behavior a consumer would have with no ReplayCache wired at all.

ReplayCache.FailMode is a nested enum declared on ReplayCache itself — it is unrelated to any other FailMode class that may exist elsewhere in the framework.

ReplayCache cache = new InMemoryReplayCache();

long expiresAtMs = System.currentTimeMillis() + 60_000L;
boolean firstUse = cache.checkAndRecord(nonce, expiresAtMs, ReplayCache.FailMode.FAIL_CLOSED);
if (!firstUse) {
throw new SecurityException("Replayed nonce detected");
}

InMemoryReplayCache​

The built-in, zero-config ReplayCache implementation: a per-process ConcurrentHashMap-backed seen-identifier set, with self-eviction bounded by each record's own expiresAtMs. Eviction sweeps are throttled to at most once per InMemoryReplayCache.DEFAULT_SWEEP_INTERVAL_MS (1000 ms by default) to keep checkAndRecord() cheap under load.

Because it's per-process memory, InMemoryReplayCache only enforces single-use within one JVM — two instances behind a load balancer will not see each other's recorded identifiers. Consumers who need cross-node single-use enforcement must supply their own ReplayCache backed by a store shared across every node.

InMemoryReplayCache cache = new InMemoryReplayCache();