zookeeper code

sample code:

package com.marulin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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.ZooDefs.Perms;
import org.apache.zookeeper.ZooKeeper;

import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;

public class Test {

	/**
	 * @param args
	 * @throws IOException
	 * @throws InterruptedException
	 * @throws KeeperException
	 */
	public static void main(String[] args) throws IOException, KeeperException,
			InterruptedException {
		Watcher watcher = new MyWatch();
		Stat st = new Stat();
		ZooKeeper zk = new ZooKeeper("localhost:2181", 1000, null);
		// CreateMode createMode = new CreateMode(1, false, true);

		ArrayList<ACL> OPEN_ACL_UNSAFE = new ArrayList<ACL>(
				Collections.singletonList(new ACL(Perms.ALL, new Id("world",
						"anyone"))));
		// zk.create("/test", "abc".getBytes(), OPEN_ACL_UNSAFE,
		// CreateMode.PERSISTENT_SEQUENTIAL);

		// zk.create("/test", "abc".getBytes(), OPEN_ACL_UNSAFE,
		// CreateMode.PERSISTENT);

		zk.create("/test/a", "abc1".getBytes(), OPEN_ACL_UNSAFE,
				CreateMode.PERSISTENT_SEQUENTIAL);

		List<String> list = zk.getChildren("/test", watcher, st);
		for (String s : list) {
			System.out.println(s);
		}
		//
		// byte b[] = zk.getData("/test0000000029", watcher, st);
		// System.out.println(new String(b));
		// System.out.println(st.getMzxid());
		// zk.create("/testt", "abct".getBytes(), OPEN_ACL_UNSAFE,
		// CreateMode.EPHEMERAL);
		//
		// byte b1[] = zk.getData("/testt", watcher, st);
		// System.out.println(new String(b1));
		// System.out.println();

		// zk.delete("/test0000000002", 0);
		// zk.delete("/test0000000029", -1);
		zk.exists("/test/a0000000002", watcher);
		zk.delete("/test/a0000000002", -1);
	}

}

class MyWatch implements Watcher {

	@Override
	public void process(WatchedEvent event) {
		System.out.println("event");
		System.out.println(event.getPath());
	}

}


posted on 2012-04-02 21:45  疯子123  阅读(148)  评论(0编辑  收藏  举报

导航