Interface DistributedLock

All Superinterfaces:
AutoCloseable, Lock<String>

public interface DistributedLock extends Lock<String>
Distributed Lock Interface with TTL and Extension Support 支持TTL和延长的分布式锁接口

Extends the base Lock interface with distributed lock specific features such as TTL management, lock extension, and fencing token support.

扩展基础Lock接口,增加分布式锁特定功能,如TTL管理、锁延长和防护令牌支持。

Features | 主要功能:

  • TTL-based lock expiration - 基于TTL的锁过期
  • Lock extension/renewal - 锁延长/续期
  • Remaining TTL query - 剩余TTL查询
  • Expiration check - 过期检查

Usage Examples | 使用示例:

// Create distributed lock | 创建分布式锁
DistributedLock lock = provider.createLock("order:12345", config);

try (var guard = lock.lock()) {
    // Critical section | 临界区
    processOrder("12345");

    // Extend lock if needed | 如需要可延长锁
    if (!lock.isExpired()) {
        lock.extend(Duration.ofSeconds(30));
    }
}

// Check remaining TTL | 检查剩余TTL
lock.getRemainingTtl().ifPresent(ttl ->
    System.out.println("Remaining: " + ttl));

Security | 安全性:

  • Thread-safe: Implementation dependent - 线程安全: 依赖实现
  • Supports fencing tokens - 支持防护令牌
Since:
JDK 25, opencode-base-lock V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getName

      String getName()
      Gets the unique name of this distributed lock 获取此分布式锁的唯一名称
      Returns:
      the lock name | 锁名称
    • getValue

      String getValue()
      Gets the lock value/token for ownership verification 获取用于所有权验证的锁值/令牌
      Returns:
      the lock value | 锁值
    • extend

      boolean extend(Duration duration)
      Extends the lock TTL by the specified duration 将锁的TTL延长指定时长
      Parameters:
      duration - the duration to extend | 要延长的时长
      Returns:
      true if extended successfully | true表示延长成功
    • getRemainingTtl

      Optional<Duration> getRemainingTtl()
      Gets the remaining time-to-live of the lock 获取锁的剩余生存时间
      Returns:
      remaining TTL, or empty if lock not held | 剩余TTL,如果未持有锁则为空
    • isExpired

      boolean isExpired()
      Checks if the lock has expired 检查锁是否已过期
      Returns:
      true if expired | true表示已过期