独自のZooKeeperクライアントツールの開発


自分用のzookeeperツールを簡単に書き、zookeeperの基本的なノードの作成、ノードの修正、ノードの削除、ノードのクエリーの基本的な操作を実現し、その上で必要な機能を拡張し、皆さんと共有することができます.

package cn.basttg.demo.zk;

import java.util.List;

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

public class ZkTool {
	//     
	private final static String TOOL_NAME = "ZooKeeper   ";
	private final static String TOOL_VERSION = "V0.1";
	private final static String TOOL_AUTHOR = "Jason Chen";
	private final static String TOOL_COPYRIGHT = "©2012 Jason     ";

	//       
	private final static int ACTION_QUERY = 1;
	private final static int ACTION_CREATE = 2;
	private final static int ACTION_MODIFY = 3;
	private final static int ACTION_DELETE = 4;
	private final static int ACTION_CONFIG = 8;
	private final static int ACTION_ABOUT = 9;
	private final static int ACTION_QUIT = 0;

	//       
	private final static String DEFAULT_HOST = "172.16.160.196";
	private final static int DEFAULT_PORT = 2181;
	private final static int DEFAULT_TIMEOUT = 30000;

	//       
	private static String zkHost = DEFAULT_HOST;
	private static int zkPort = DEFAULT_PORT;
	private static int zkTimeout = DEFAULT_TIMEOUT;
	
	private static ZooKeeper zooKeeper = null;

	public static void main(String[] args) {
		try {
			openZk();
			
			StartMenu();
			while (true) {
				int operate = Integer.parseInt(getCommand());
				switch (operate) {
				case ACTION_QUERY:
					queryData();
					break;
				case ACTION_CREATE:
					createData();
					break;
				case ACTION_MODIFY:
					modifyData();
					break;
				case ACTION_DELETE:
					deleteData();
					break;
				case ACTION_CONFIG:
					configConnection();
					break;
				case ACTION_ABOUT:
					about();
					break;
				case ACTION_QUIT:
					exit();
					break;
				default:
					System.out.println("      " + operate);
					break;
				}
			}
		} catch (Exception e) {
			System.out.println("    ,      : ");
			e.printStackTrace();
		}
	}

	//     
	public static void StartMenu() {
		System.out.println("**********" + TOOL_NAME + " " + TOOL_VERSION + "**********");
		System.out.println(ACTION_QUERY + "、      ");
		System.out.println(ACTION_CREATE + "、      ");
		System.out.println(ACTION_MODIFY + "、      ");
		System.out.println(ACTION_DELETE + "、      ");
		System.out.println(ACTION_CONFIG + "、      ");
		System.out.println(ACTION_ABOUT + "、    ");
		System.out.println(ACTION_QUIT + "、  ");
		System.out.println("********************************");
	}

	//       
	public static String getCommand() {
		return getCommand("     ", 1);
	}

	public static String getCommand(String message) {
		return getCommand(message, null, 100);
	}

	public static String getCommand(String message, int limit) {
		return getCommand(message, null, limit);
	}

	public static String getCommand(String message, String defaultValue) {
		return getCommand(message, defaultValue, 100);
	}

	public static String getCommand(String message, String defaultValue, int limit) {
		String strCommand = "";
		try {
			do {
				System.out.println();
				if (defaultValue == null) {
					System.out.print(message + ": ");
				} else {
					System.out.print(message + " [" + defaultValue + "]: ");
				}

				byte[] command = new byte[100];
				System.in.read(command);
				strCommand = new String(command);
				strCommand = strCommand.replaceAll("\r
", "").trim(); // , if (defaultValue != null && "".equals(strCommand)) { strCommand = defaultValue; } } while (strCommand.length() > limit); } catch (Exception e) { System.out.println(" !!!"); } return strCommand; } public static void queryData() { System.out.println("***** *****"); String zpath = getCommand(" ", "/App"); try { openZk(); // System.out.println(zooKeeper.getChildren(zpath, true)); } catch (Exception e) { e.printStackTrace(); } } public static void createData() { System.out.println("***** *****"); String zpath = getCommand(" ", "/App"); String zdata = getCommand(" ", zpath); String zacl = getCommand(" ", "0"); String ztype = getCommand(" ", "E"); try { openZk(); zooKeeper.create(zpath, zdata.getBytes(), getAcl(zacl), getCreateMode(ztype)); } catch (Exception e) { e.printStackTrace(); } } public static void modifyData() { System.out.println("***** *****"); String zpath = getCommand(" ", "/App"); String zdata = getCommand(" ", zpath); try { openZk(); zooKeeper.setData(zpath, zdata.getBytes(), -1); String parent = StringUtil.strLeftBack(zpath, "/"); System.out.println(" :[" + zooKeeper.exists(parent, false) + "]"); } catch (Exception e) { e.printStackTrace(); } } public static void deleteData() { System.out.println("***** *****"); String zpath = getCommand(" ", "/App"); String zversion = getCommand(" ", "-1"); try { openZk(); zooKeeper.delete(zpath, Integer.valueOf(zversion)); } catch (Exception e) { e.printStackTrace(); } } /** */ public static void configConnection() { System.out.println("***** *****"); do { zkHost = getCommand(" ", zkHost); zkPort = Integer.valueOf(getCommand(" ", "" + zkPort)); zkTimeout = Integer.valueOf(getCommand(" ", "" + zkTimeout)); try { // zooKeeper = new ZooKeeper(zkHost + ":" + zkPort, zkTimeout, new Watcher() { // public void process(WatchedEvent event) { System.out.println(" [" + event.getType() + "] !"); } }); } catch (Exception e) { zooKeeper = null; e.printStackTrace(); } } while (zooKeeper == null); System.out.println(">>> !"); } /** */ public static void about() { System.out.println("************ " + TOOL_NAME + "****************"); System.out.println("|     :" + TOOL_VERSION + "           |"); System.out.println("|     :" + TOOL_AUTHOR + "          |"); System.out.println("|                    |"); System.out.println("| :               |"); System.out.println("|V0.1                  |"); System.out.println("|  1.          |"); System.out.println("|  2.          |"); System.out.println("|  3.          |"); System.out.println("|  4.          |"); System.out.println("|  5.            |"); System.out.println("|                    |"); System.out.println("|   " + TOOL_COPYRIGHT + "      |"); System.out.println("*****************************************"); } /** */ public static void exit() { System.out.println(" , !"); try { closeZk(); Thread.sleep(1000); } catch (Exception e) { } System.exit(0); } private static ZooKeeper openZk() { if(zooKeeper==null) { // try { zooKeeper = new ZooKeeper(zkHost + ":" + zkPort, zkTimeout, new Watcher() { // public void process(WatchedEvent event) { System.out.println(" [" + event.getType() + "] !"); } }); } catch (Exception e) { e.printStackTrace(); } } return zooKeeper; } private static void closeZk() { if(zooKeeper!=null) { // try { zooKeeper.close(); } catch (Exception e) { e.printStackTrace(); } } } private static List<ACL> getAcl(String zacl) { return Ids.OPEN_ACL_UNSAFE; } private static CreateMode getCreateMode(String ztype) { if("P".equalsIgnoreCase(ztype)) { return CreateMode.PERSISTENT; } else if("PS".equalsIgnoreCase(ztype)) { return CreateMode.PERSISTENT_SEQUENTIAL; } else if("E".equalsIgnoreCase(ztype)) { return CreateMode.EPHEMERAL; } else{ return CreateMode.EPHEMERAL_SEQUENTIAL; } } }