这篇文章主要介绍“Curator的分布式Barrier怎么实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Curator的分布式Barrier怎么实现”文章能帮助大家解决问题。
分布式Barrier,直接看代码好了:
package curator.barrier;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.barriers.DistributedBarrier;
import org.apache.curator.retry.ExponentialBackoffRetry;
public class BarrierDemo {
public static void main(String[] args) throws Exception {
final List<DistributedBarrier> barriers = new ArrayList<DistributedBarrier>(3);
ExecutorService service = Executors.newCachedThreadPool();
for (int i = 0; i < 3; i++) {
Runnable run = new Runnable() {
public void run() {
try {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(
1000, 3);
final CuratorFramework client = CuratorFrameworkFactory
.builder().connectString("127.0.0.1:2181")
.sessionTimeoutMs(5000)
.connectionTimeoutMs(10000)
.retryPolicy(retryPolicy).namespace("text")
.build();
client.start();
DistributedBarrier barrier = new DistributedBarrier(client, "/barrier");
barriers.add(barrier);
barrier.setBarrier(); // 设置barrier
System.out.println(" : setBarrier...");
barrier.waitOnBarrier();// 等待其他进程移除barrier,此后所有的waitOnBarrier进程都将解除阻塞.
System.err.println(" : run ing...");
} catch (Exception e) {
}
}
};
service.execute(run);
}
Thread.currentThread().sleep(5000);
for (DistributedBarrier b : barriers) {
b.removeBarrier();
}
System.in.read();
}
}
关于“Curator的分布式Barrier怎么实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注天达云行业资讯频道,小编每天都会为大家更新不同的知识点。