
原创技术文章,转载请注明:转自http://newliferen.github.io/
MyCommandOptions类为命令处理器类,该类作为用户控制台和ZooKeeper核心类的一个适配接口,解析客户端输入命令,将用户输入内容解析为command和args。针对不同的command调用ZooKeeper类的对应方法,将args中path作为参数传给ZooKeeper类,由ZooKeeper进行请求对象封装,并向ZK server发起请求和处理响应结果。
解析命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public boolean parseCommand( String cmdstring ) { StringTokenizer cmdTokens = new StringTokenizer(cmdstring, " "); String[] args = new String[cmdTokens.countTokens()]; int tokenIndex = 0; while (cmdTokens.hasMoreTokens()) { args[tokenIndex] = cmdTokens.nextToken(); tokenIndex++; } if (args.length == 0){ return false; } command = args[0]; cmdArgs = Arrays.asList(args); return true; }
|
处理命令
以“ls /”命令为例,实际就是调用ZooKeeper对象的获取子节点方法。
1 2 3 4 5
|
if (cmd.equals("ls") && args.length >= 2) { path = args[1]; List<String> children = zk.getChildren(path, watch); System.out.println(children); }
|
近期评论