Java客户端如何进行操作

Java客户端操作

Maven工程和配置POM

xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

4.0.0

com.atguigu.mall

zk0919

0.0.1-SNAPSHOT

jar

zk0919

http://maven.apache.org

UTF-8

com.101tec

zkclient

0.10

org.apache.zookeeper

zookeeper

3.4.9

log4j

log4j

1.2.17

junit

junit

4.9

test

程序代码

package com.atguigu.zk3;

import java.io.IOException;

import org.apache.log4j.Logger;

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;

import org.apache.zookeeper.data.Stat;

public class HelloZK

{

/**

* Logger for this class

*/

private static final Logger logger = Logger.getLogger(HelloZK.class);

private static final String CONNECTSTRING = “192.168.67.167:2181”;

private static final String PATH = “/atguigu”;

private static final int SESSION_TIMEOUT = 50*1000;

public ZooKeeper startZK() throws IOException

{

return new ZooKeeper(CONNECTSTRING, SESSION_TIMEOUT, new Watcher() {

@Override

public void process(WatchedEvent event)

{

}

});

}

public void stopZK(ZooKeeper zk) throws InterruptedException

{

if(zk != null)

{

zk.close();

}

}

public void createZNode(ZooKeeper zk,String path,String nodeValue) throws KeeperException, InterruptedException

{

zk.create(path,nodeValue.getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

}

public String getZNode(ZooKeeper zk,String path) throws KeeperException, InterruptedException

{

byte[] byteArray = zk.getData(path, false, new Stat());

return new String(byteArray);

}

public static void main(String[] args) throws IOException, KeeperException, InterruptedException

{

HelloZK hello = new HelloZK();

ZooKeeper zk = hello.startZK();

Stat stat = zk.exists(PATH, false);

if(stat == null)

{

hello.createZNode(zk, PATH, “zk1014”);

String result = hello.getZNode(zk, PATH);

System.out.println(“**********result: “+result);

}else{

System.out.println(“***********znode has already ok***********”);

}

hello.stopZK(zk);

}

}

java培训

你可能感兴趣的:(java,java,java客户端)