Curator-Barriers(七)

摘要

栅栏,curator中有两种实现:Barrier 和 Double Barrier。

Barrier,分布式应用使用栅栏阻塞一堆节点执行,直到达到某个条件后,所有节点才被放行。

Double Barrier,双重栅栏可以是客户端同步计算开始和结束。当足够的进程加入到栅栏,计算过程开始,当进程完成后,退出栅栏。

Barrier

DistributedBarrier

用法

创建 DistributedBarrier

1
2
3
4
5
public DistributedBarrier(CuratorFramework client,
String barrierPath)
Parameters:
client - client
barrierPath - path to use as the barrier

等待栅栏

waitOnBarrier()

设置栅栏

setBarrier()

删除栅栏

removeBarrier()

错误处理

DistributedBarrier 实例监听连接断开,并在阻塞方法waitOnBarrier()上抛出异常。

Double Barrier

DistributedDoubleBarrier

用法

创建 DistributedBarrier

1
2
3
4
5
6
7
8
9
10
public DistributedDoubleBarrier(CuratorFramework client,
String barrierPath,
int memberQty)
Creates the barrier abstraction. memberQty is the number of members in the barrier. When enter() is called, it blocks until
all members have entered. When leave() is called, it blocks until all members have left.

Parameters:
client - the client
barrierPath - path to use
memberQty - the number of members in the barrier

进入栅栏

enter()

退出栅栏

leave()

错误处理

DistributedDoubleBarrier实例也是监听连接断开,并在enter()或者leave()方法抛出异常。

坚持技术分享,您的支持将鼓励我继续创作!