Java tutorial
/* * Copyright 2010 shanyong.wang Licensed under the Apache License, Version 2.0 (the "License"); you * may not use this file except in compliance with the License. You may obtain a copy of the License * at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in * writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific * language governing permissions and limitations under the License. */ package com.anteam.demo.zookeeper; import org.apache.zookeeper.*; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.server.auth.DigestAuthenticationProvider; import java.io.IOException; /** * @author <a href="mailto:wsysisibeibei@gmail.com">sisibeibei</a> * @ClassName: ZooKeeperOperationDemo * @Package com.anteam.demo.zookeeper * @Description: zookeeper ?demo * @date 2013-4-23 ?3:01:51 */ public class ZooKeeperOperationDemo { /** * ? */ private static final int SESSION_TIMEOUT = 30000; /** * ZooKeeper */ protected ZooKeeper zk; /** * Watcher */ Watcher wh = new Watcher() { public void process(WatchedEvent event) { System.out.println(event.toString()); } }; /** * @return void() * @throws IOException * @Title: createZKInstance * @Description: ? ZooKeeper */ private void createZKInstance() throws IOException { zk = new ZooKeeper("localhost:6701", ZooKeeperOperationDemo.SESSION_TIMEOUT, this.wh); zk.addAuthInfo("digest", "test:test.123".getBytes()); } /** * @return void() * @throws IOException * @throws InterruptedException * @throws KeeperException * @Title: ZKOperations * @Description: zk? */ private void ZKOperations() throws IOException, InterruptedException, KeeperException { System.out.println( "1. ZooKeeper (znode zoo2, ? myData2 ?? OPEN_ACL_UNSAFE Persistent"); zk.create("/zoo2", "myData2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println("2. ?? "); System.out.println(new String(zk.getData("/zoo2", false, null))); System.out.println("3. ? "); zk.setData("/zoo2", "shenlan211314".getBytes(), -1); System.out.println("4. ?? "); System.out.println(new String(zk.getData("/zoo2", false, null))); System.out.println("5. "); zk.delete("/zoo2", -1); System.out.println("6. ? "); System.out.println(" ? [" + zk.exists("/zoo2", false) + "]"); } /** * @return void() * @throws InterruptedException * @Title: ZKClose * @Description: zk */ private void ZKClose() throws InterruptedException { zk.close(); } /** * @param args * @return void() * @throws IOException * @throws InterruptedException * @throws KeeperException * @Title: main * @Description: ? */ public static void main(String[] args) throws Exception { String password = DigestAuthenticationProvider.generateDigest("admin:admin.pwd"); System.out.println(password); // ZooKeeperOperationDemo dm = new ZooKeeperOperationDemo(); // dm.createZKInstance(); // dm.ZKOperations(); // dm.ZKClose(); } }