说明

在JVM上运行的计数信号量。使用相同锁定路径的所有JVM中的所有进程将实现一个进程间限制的租约集合。此外,这个信号灯大多是“公平的” - 每个用户将按照要求的顺序(从ZK的角度)获得租约。

确定信号量的最大租约有两种模式。在第一模式中,最大租约是由给定路径的用户维护的约定。在第二种模式下,使用SharedCountReader作为给定路径的信号量的方法来确定最大租约。

如果未使用SharedCountReader,则不进行任何内部检查,以防止进程A表现为有10个租约,而且Process B的行为就好像有20个。因此,确保所有进程中的所有实例都使用相同的numberOfLeases值。

各种收购方式返回代表所得租赁的租赁物品。客户必须注意关闭租赁对象(理想情况下是最终块),否则租约将会丢失。但是,如果客户端会话丢失(崩溃等),则客户端保留的任何租约将自动关闭,并可供其他客户端使用。

参与类

  • InterProcessSemaphoreV2

  • Lease

  • SharedCountReader

用法

创建一个InterProcessSemaphoreV2

public InterProcessSemaphoreV2(CuratorFramework client,
                             String path,
                             int numberOfLeases)
Parameters:
client - client
path - the path to lock
numberOfLeases - the number of leases allowed by this semaphore
public InterProcessSemaphoreV2(CuratorFramework client,
                             String path,
                             SharedCountReader count)
Parameters:
client - the client
path - path for the semaphore
count - the shared count to use for the max leases

一般用法

要获得一个租赁/使用,请使用以下方法之一:

public Lease acquire()

Acquire a lease. If no leases are available, this method blocks until either the maximum number of
leases is increased or another client/process closes a lease.
The client must close the lease when it is done with it. You should do this in a finally block.
Returns:
the new lease
public Collection<Lease> acquire(int qty)

Acquire qty leases. If there are not enough leases available, this method blocks until either the
maximum number of leases is increased enough or other clients/processes close enough leases.
The client must close the leases when it is done with them. You should do this in a finally block.
NOTE: You can use returnAll(Collection) for this.
Parameters:
qty - number of leases to acquire
Returns:
the new leases
public Lease acquire(long time,
                     TimeUnit unit)
Acquire a lease. If no leases are available, this method blocks until either the maximum number of
leases is increased or another client/process closes a lease. However, this method will only block
to a maximum of the time parameters given.
The client must close the lease when it is done with it. You should do this in a finally block.
Parameters:
time - time to wait
unit - time unit
Returns:
the new lease or null if time ran out
public Collection<Lease> acquire(int qty,
                               long time,
                               TimeUnit unit)
Acquire qty leases. If there are not enough leases available, this method blocks until either the
maximum number of leases is increased enough or other clients/processes close enough leases. However,
this method will only block to a maximum of the time parameters given. If time expires before all
leases are acquired, the subset of acquired leases are automatically closed.
The client must close the leases when it is done with them. You should do this in a finally block.
NOTE: You can use returnAll(Collection) for this.
Parameters:
qty - number of leases to acquire
time - time to wait
unit - time unit

租赁实例可以直接关闭,也可以使用这些方便的方法:

public void returnAll(Collection<Lease> leases)
public void returnLease(Lease lease)

错误处理

强烈建议您添加一个ConnectionStateListener并观察SUSPENDED和LOST状态更改。 如果报告了SUSPENDED状态,则无法确定您仍然保持锁定状态,除非您随后收到RECONNECTED状态。 如果报告了LOST状态,则确定您不再持有该锁。