java 操作 zookeeper API
来源:互联网 发布:金耳朵英语软件 编辑:程序博客网 时间:2024/05/21 11:12
/** * ZooKeeper Java Api 使用样例<br> * * @author <a href="mailto:zhangdongfang@knet.cn">East271536394</a> * @version 2013-6-20 下午12:39:41 */import java.io.IOException;import java.util.concurrent.CountDownLatch;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.Watcher.Event.KeeperState;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.ZooKeeper;public class JavaApiSample implements Watcher {private static final int SESSION_TIMEOUT = 10000;private static final String CONNECTION_STRING = "202.173.8.57:4181,202.173.8.58:4181,202.173.8.59:4181";private static final String ZK_PATH = "/knet";private ZooKeeper zk = null;private CountDownLatch connectedSemaphore = new CountDownLatch(1);private boolean init = false;/** * 创建ZK连接 * @param connectString ZK服务器地址列表 * @param sessionTimeout Session超时时间 */public void createConnection(String connectString, int sessionTimeout) {this.releaseConnection();try {zk = new ZooKeeper(connectString, sessionTimeout, this);init = true;connectedSemaphore.await();init = false;} catch (InterruptedException e) {System.out.println("连接创建失败,发生 InterruptedException");e.printStackTrace();} catch (IOException e) {System.out.println("连接创建失败,发生 IOException");e.printStackTrace();}}/** * 关闭ZK连接 */public void releaseConnection() {if (this.zk != null) {try {this.zk.close();} catch (InterruptedException e) {// ignore e.printStackTrace();}}}/** * 创建节点 * @param path 节点path * @param data 初始数据内容 * @return */public boolean createPath(String path, String data) {try {System.out.println("节点创建成功, Path: " + this.zk.create(path, // data.getBytes(), // Ids.OPEN_ACL_UNSAFE, // CreateMode.EPHEMERAL) + ", content: " + data);} catch (KeeperException e) {System.out.println("节点创建失败,发生KeeperException");e.printStackTrace();} catch (InterruptedException e) {System.out.println("节点创建失败,发生 InterruptedException");e.printStackTrace();}return true;}/** * 读取指定节点数据内容 * @param path 节点path * @return */public String readData(String path) {try {System.out.println("获取数据成功,path:" + path);return new String(this.zk.getData(path, true, null));} catch (KeeperException e) {System.out.println("读取数据失败,发生KeeperException,path: " + path);e.printStackTrace();return "";} catch (InterruptedException e) {System.out.println("读取数据失败,发生 InterruptedException,path: " + path);e.printStackTrace();return "";}}/** * 更新指定节点数据内容 * @param path 节点path * @param data 数据内容 * @return */public boolean writeData(String path, String data) {try {System.out.println("更新数据成功,path:" + path + ", stat: " + this.zk.setData(path, data.getBytes(), -1));} catch (KeeperException e) {System.out.println("更新数据失败,发生KeeperException,path: " + path);e.printStackTrace();} catch (InterruptedException e) {System.out.println("更新数据失败,发生 InterruptedException,path: " + path);e.printStackTrace();}return false;}/** * 删除指定节点 * @param path 节点path */public void deleteNode(String path) {try {this.zk.delete(path, -1);System.out.println("删除节点成功,path:" + path);} catch (KeeperException e) {System.out.println("删除节点失败,发生KeeperException,path: " + path);e.printStackTrace();} catch (InterruptedException e) {System.out.println("删除节点失败,发生 InterruptedException,path: " + path);e.printStackTrace();}}public static void main(String[] args) {JavaApiSample sample = new JavaApiSample();sample.createConnection(CONNECTION_STRING, SESSION_TIMEOUT);if (sample.createPath(ZK_PATH, "我是节点初始内容")) {System.out.println();System.out.println("数据内容: " + sample.readData(ZK_PATH) + "\n");sample.writeData(ZK_PATH, "更新后的数据");System.out.println("数据内容: " + sample.readData(ZK_PATH) + "\n");sample.deleteNode(ZK_PATH);}sample.releaseConnection();}/** * 收到来自Server的Watcher通知后的处理。 */@Overridepublic void process(WatchedEvent event) {System.out.println("收到事件通知:" + event.getState() + ",type :" + event.getType() + ",path :" + event.getPath()+ "\n");if (KeeperState.SyncConnected == event.getState()) {if (init) {System.out.println("connectedSemaphore.countDown()>>>>>>>>>>>>>>>>");connectedSemaphore.countDown();}}}}
运行结果如下:
收到事件通知:SyncConnected,type :None,path :nullconnectedSemaphore.countDown()>>>>>>>>>>>>>>>>节点创建成功, Path: /knet, content: 我是节点初始内容获取数据成功,path:/knet数据内容: 我是节点初始内容收到事件通知:SyncConnected,type :NodeDataChanged,path :/knet更新数据成功,path:/knet, stat: 30064771186,30064771187,1371704002257,1371704002267,1,0,0,89895709067640842,18,0,30064771186获取数据成功,path:/knet数据内容: 更新后的数据收到事件通知:SyncConnected,type :NodeDeleted,path :/knet删除节点成功,path:/knet2013-06-20 12:53:18,835 [myid:] - INFO [main:ZooKeeper@679] - Session: 0x13f5fabcdcf000a closed2013-06-20 12:53:18,835 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@511] - EventThread shut down
- java 操作 zookeeper API
- 使用java api操作zookeeper
- Zookeeper 命令行操作及Java api
- 使用Java客户端API操作Zookeeper
- zookeeper api 操作znode
- ZooKeeper操作api
- ZooKeeper API操作
- Zookeeper Java API
- Zookeeper Java API 实例
- Zookeeper API JAVA 解析
- Zookeeper JAVA API
- Zookeeper Java API
- Zookeeper之JAVA API
- Zookeeper JAVA API
- Zookeeper Java API使用
- Zookeeper Api(java)入门
- Zookeeper使用--Java API
- ZooKeeper Java API使用
- 做一个专业软件开发必须精通的事项,你还差多少
- 带盘子序号的汉诺塔(递归方法)
- 设计模式--6大原则--接口隔离原则
- ExecutorService线程池
- Windows7 CMD命令提示符下的5个小技巧
- java 操作 zookeeper API
- android 通用wifi调试 应用方法
- 多项式运算(渣算法)
- SPI总线协议及SPI时序图详解
- Spring Ioc-依赖注入的几种方式
- UML 之 C++类图关系全面剖析
- 文本输入框内容字母自动大小写控制
- 实现页面无刷新,在gridview中添加,删除,修改数据
- EXTJS4两个ComboBox的数据源联动,解决遇到第二个ComboBox第二次以后显示忙的状态问题