ZooKeeper Java API 간단 한 예제

10557 단어
코드 예 시 는 다음 과 같다.
import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class TestZooKeeper {

	public static void main(String[] args) {

		ZooKeeper zk = null;
		try {

			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			
			System.out.println("    ZooKeeper...");

			//    ZooKeeper      zk
			String address = "192.168.1.226:2181";
			int sessionTimeout = 3000;
			zk = new ZooKeeper(address, sessionTimeout, new Watcher() {
				//           
				public void process(WatchedEvent event) {
					if (event.getType() == null || "".equals(event.getType())) {
						return;
					}
					System.out.println("     " + event.getType() + "  !");
				}
			});

			System.out.println("ZooKeeper      !");

			// //      register()      Watcher
			// Watcher watcher = new Watcher() {
			//
			// @Override
			// public void process(WatchedEvent event) {
			// // TODO Auto-generated method stub
			// System.out.println("     :     " + event.getType() + "  !");
			// }
			// };
			// zk.register(watcher);

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			//        
			//    /tmp_root_path
			//         "     /tmp_root_path"
			//      CreateMode.PERSISTENT
			System.out.println("         /tmp_root_path...");
			zk.create("/tmp_root_path", "     /tmp_root_path".getBytes(),
					Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			System.out.println("     /tmp_root_path    !");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			//           
			//    /tmp_root_path/childPath1
			//         "        /tmp_root_path/childPath1"
			//      CreateMode.PERSISTENT
			System.out.println("            /tmp_root_path/childPath1...");
			zk.create("/tmp_root_path/childPath1",
					"        /tmp_root_path/childPath1".getBytes(),
					Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			System.out.println("        /tmp_root_path/childPath1    !");
			
			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			//          
			System.out.println("       /tmp_root_path         ...");
			System.out.println(zk.getChildren("/tmp_root_path", true));
			System.out.println("   /tmp_root_path             !");
			
			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			//          
			System.out.println("            /tmp_root_path/childPath1  ...");
			zk.setData("/tmp_root_path/childPath1",
					"              /tmp_root_path/childPath1".getBytes(), -1);
			System.out.println("          /tmp_root_path/childPath1    !");
			
			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			//          
			System.out.println("           ...");
			System.out.println(zk.exists("/tmp_root_path", true));
			System.out.println("           ");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			//           
			//    /tmp_root_path/childPath2
			//         "        /tmp_root_path/childPath2"
			//      CreateMode.PERSISTENT
			System.out.println("            /tmp_root_path/childPath2...");
			zk.create("/tmp_root_path/childPath2",
					"        /tmp_root_path/childPath2".getBytes(),
					Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			System.out.println("        /tmp_root_path/childPath2    !");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			//           /tmp_root_path/childPath2    
			System.out.println("            /tmp_root_path/childPath2    ...");
			System.out.println(new String(zk.getData(
					"/tmp_root_path/childPath2", true, null)));
			System.out.println("        /tmp_root_path/childPath2        !");
			
			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			//          
			System.out.println("       /tmp_root_path         ...");
			System.out.println(zk.getChildren("/tmp_root_path", true));
			System.out.println("   /tmp_root_path             !");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			
			//          
			System.out.println("           ...");
			System.out.println(zk.exists("/tmp_root_path", true));
			System.out.println("           ");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			//           
			System.out.println("            /tmp_root_path/childPath1...");
			zk.delete("/tmp_root_path/childPath1", -1);
			System.out.println("        /tmp_root_path/childPath1    !");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			
			//          
			System.out.println("           ...");
			System.out.println(zk.exists("/tmp_root_path", true));
			System.out.println("           ");
			
			Thread.currentThread().sleep(1000l);

			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			//           
			System.out.println("            /tmp_root_path/childPath2...");
			zk.delete("/tmp_root_path/childPath2", -1);
			System.out.println("        /tmp_root_path/childPath2    !");
			
			Thread.currentThread().sleep(1000l);

			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			
			//          
			System.out.println("           ...");
			System.out.println(zk.exists("/tmp_root_path", true));
			System.out.println("           ");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			//        
			System.out.println("         /tmp_root_path...");
			zk.delete("/tmp_root_path", -1);
			System.out.println("     /tmp_root_path    !");
			
			Thread.currentThread().sleep(1000l);

			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			//          
			System.out.println("           ...");
			System.out.println(zk.exists("/tmp_root_path", true));
			System.out.println("           ");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

		} catch (IOException | KeeperException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//     
			if (zk != null) {
				try {
					zk.close();
					System.out.println("  ZooKeeper    !");

				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}
}
        출력 결 과 는 다음 과 같 습 니 다.
...
...
...
...
    ZooKeeper...
ZooKeeper      !
     None  !
...
...
...
...
         /tmp_root_path...
     /tmp_root_path    !
...
...
...
...
            /tmp_root_path/childPath1...
        /tmp_root_path/childPath1    !
...
...
...
...
       /tmp_root_path         ...
[childPath1]
   /tmp_root_path             !
...
...
...
...
            /tmp_root_path/childPath1  ...
          /tmp_root_path/childPath1    !
...
...
...
...
           ...
2006956,2006956,1458958753172,1458958753172,0,1,0,0,29,1,2006957

           
...
...
...
...
            /tmp_root_path/childPath2...
     NodeChildrenChanged  !
        /tmp_root_path/childPath2    !
...
...
...
...
            /tmp_root_path/childPath2    ...
        /tmp_root_path/childPath2
        /tmp_root_path/childPath2        !
...
...
...
...
       /tmp_root_path         ...
[childPath2, childPath1]
   /tmp_root_path             !
...
...
...
...
           ...
2006956,2006956,1458958753172,1458958753172,0,2,0,0,29,2,2006959

           
...
...
...
...
            /tmp_root_path/childPath1...
     NodeChildrenChanged  !
        /tmp_root_path/childPath1    !
...
...
...
...
           ...
2006956,2006956,1458958753172,1458958753172,0,3,0,0,29,1,2006960

           
...
...
...
...
            /tmp_root_path/childPath2...
     NodeDeleted  !
        /tmp_root_path/childPath2    !
...
...
...
...
           ...
2006956,2006956,1458958753172,1458958753172,0,4,0,0,29,0,2006961

           
...
...
...
...
         /tmp_root_path...
     NodeDeleted  !
     /tmp_root_path    !
...
...
...
...
           ...
null
           
...
...
...
...
  ZooKeeper    !
        왜 Watcher 가 그런 걸 감시 하 는 지 나중에 분석 해!

좋은 웹페이지 즐겨찾기