Zookeeper实例Curator API-使用Curator的异步接口
来源:互联网 发布:tensorflow可视化界面 编辑:程序博客网 时间:2024/04/30 12:07
import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.framework.api.BackgroundCallback;import org.apache.curator.framework.api.CuratorEvent;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;/** * * @ClassName: Create_Node_Background_Sample * @Description: TODO(使用Curator的异步接口) * @author RongShu * @date 2017年6月17日 下午1:16:40 * */public class Create_Node_Background_Sample {static String path = "/zk-book";static CuratorFramework client = CuratorFrameworkFactory.builder().connectString("localhost:2181").sessionTimeoutMs(5000).retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();static CountDownLatch semaphore = new CountDownLatch(2);static ExecutorService tp = Executors.newFixedThreadPool(2);public static void main(String[] args) throws Exception {client.start();System.out.println("Main thread: " + Thread.currentThread().getName());// 此处传入了自定义的Executorclient.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).inBackground(new BackgroundCallback() {public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {System.out.println("event[code: " + event.getResultCode() + ", type: " + event.getType() + "]");System.out.println("Thread of processResult: " + Thread.currentThread().getName());semaphore.countDown();}}, tp).forPath(path, "init".getBytes());// 此处没有传入自定义的Executorclient.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).inBackground(new BackgroundCallback() {public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {System.out.println("event[code: " + event.getResultCode() + ", type: " + event.getType() + "]");System.out.println("Thread of processResult: " + Thread.currentThread().getName());semaphore.countDown();}}).forPath(path, "init".getBytes());semaphore.await();tp.shutdown();}}输出Main thread: mainevent[code: -110, type: CREATE]Thread of processResult: main-EventThreadevent[code: 0, type: CREATE]Thread of processResult: pool-3-thread-1
参考
1.《从Paxos到Zookeeper:分布式一致性原理与实践》
2.https://zookeeper.apache.org/doc/r3.5.3-beta/javaExample.html
阅读全文
0 0
- Zookeeper实例Curator API-使用Curator的异步接口
- Zookeeper实例Curator API-使用Fluent风格的API接口来创建一个ZooKeeper客户端
- Zookeeper实例Curator API-使用Curator创建节点
- Zookeeper实例Curator API-使用Curator删除节点
- Zookeeper实例Curator API-使用Curator获取数据内容
- Zookeeper实例Curator API-使用Curator更新数据内容
- Zookeeper实例Curator API-使用curator来创建一个含隔离命名空间的ZooKeeper客户端
- Zookeeper实例Curator API-NodeCache使用实例
- Zookeeper实例Curator API-ZKPaths使用
- Zookeeper实例Curator API-EnsurePath使用
- Zookeeper实例Curator API-PathChildrenCache
- Zookeeper实例Curator API-PathChildrenCache_NoCacheData
- Zookeeper实例Curator API-TestingServer
- Zookeeper实例Curator API-TestingCluster
- Zookeeper实例Curator API-使用curator来创建一个ZooKeeper客户端
- Zookeeper实例Curator API-Master选举
- Zookeeper实例Curator API-分布式锁
- Zookeeper实例Curator API-分布式计数器
- MATLAB 基本命令
- 【框架整合】一、spring+springMVC框架搭建
- PyCharm使用技巧:Inspect Code(代码静态审查)
- 电力拖动自动控制系统(序)
- android命名规范
- Zookeeper实例Curator API-使用Curator的异步接口
- 安卓制作瞬间响应的splash启动页
- intellj idea点击导航栏打开的一个类,怎么才能定位到类的目录
- 大数据竞赛平台——Kaggle 入门
- tensorflow 中的reduction_indices
- 数据库知识点
- VMware下Ubuntu系统扩展硬盘方法(全图解)
- . Servlet创建一个响应,并返回到Web容器,Web容器返回响应到客户端
- windows 安装后软件一览表