Interface DistributedLockProvider


public interface DistributedLockProvider
Distributed Lock Provider Service Provider Interface (SPI) 分布式锁提供者服务提供者接口(SPI)

Service Provider Interface for implementing distributed locks using various backends such as Redis, Zookeeper, Etcd, or databases.

用于使用各种后端(如Redis、Zookeeper、Etcd或数据库)实现分布式锁的服务提供者接口。

Features | 主要功能:

  • Pluggable lock backends - 可插拔的锁后端
  • Lock and read-write lock support - 支持锁和读写锁
  • Availability checking - 可用性检查
  • Graceful shutdown - 优雅关闭

Implementation Examples | 实现示例:

// Implement for Redis | Redis实现
public class RedisLockProvider implements DistributedLockProvider {
    private final RedisClient client;

    @Override
    public String getName() {
        return "redis";
    }

    @Override
    public DistributedLock createLock(String lockName, DistributedLockConfig config) {
        return new RedisDistributedLock(client, lockName, config);
    }

    @Override
    public boolean isAvailable() {
        return client.isConnected();
    }

    @Override
    public void shutdown() {
        client.close();
    }
}

// Register in META-INF/services | 在META-INF/services中注册
// cloud.opencode.base.lock.spi.DistributedLockProvider
// com.example.RedisLockProvider

Supported Backends | 支持的后端:

  • Redis - distributed in-memory store | 分布式内存存储
  • Zookeeper - coordination service | 协调服务
  • Etcd - distributed key-value store | 分布式键值存储
  • Database - relational databases | 关系型数据库

Security | 安全性:

  • Thread-safe: Implementation-dependent - 线程安全: 取决于实现
  • Null-safe: No - 空值安全: 否
Since:
JDK 25, opencode-base-lock V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a new distributed lock with the specified name and configuration 使用指定名称和配置创建新的分布式锁
    Creates a distributed read-write lock 创建分布式读写锁
    Gets the unique name of this provider 获取此提供者的唯一名称
    boolean
    Checks if this provider is currently available 检查此提供者当前是否可用
    default void
    Shuts down the provider and releases resources 关闭提供者并释放资源
  • Method Details

    • getName

      String getName()
      Gets the unique name of this provider 获取此提供者的唯一名称
      Returns:
      the provider name (e.g., "redis", "zookeeper", "etcd") | 提供者名称(如"redis"、"zookeeper"、"etcd")
    • createLock

      DistributedLock createLock(String lockName, DistributedLockConfig config)
      Creates a new distributed lock with the specified name and configuration 使用指定名称和配置创建新的分布式锁
      Parameters:
      lockName - the lock name | 锁名称
      config - the lock configuration | 锁配置
      Returns:
      the distributed lock | 分布式锁
    • createReadWriteLock

      default ReadWriteLock<String> createReadWriteLock(String lockName, DistributedLockConfig config)
      Creates a distributed read-write lock 创建分布式读写锁
      Parameters:
      lockName - the lock name | 锁名称
      config - the lock configuration | 锁配置
      Returns:
      the read-write lock | 读写锁
      Throws:
      UnsupportedOperationException - if not supported by this provider | 如果此提供者不支持则抛出异常
    • isAvailable

      boolean isAvailable()
      Checks if this provider is currently available 检查此提供者当前是否可用
      Returns:
      true if available and ready to create locks | true表示可用并准备好创建锁
    • shutdown

      default void shutdown()
      Shuts down the provider and releases resources 关闭提供者并释放资源