Last active
December 14, 2015 19:59
-
-
Save wsky/5140593 to your computer and use it in GitHub Desktop.
zookeeper sample code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void runZk() throws IOException, InterruptedException { | |
int numConnections = 5000; | |
int tickTime = 2000; | |
String dataDirectory = System.getProperty("java.io.tmpdir"); | |
File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile(); | |
ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime); | |
NIOServerCnxn.Factory standaloneServerFactory = new NIOServerCnxn.Factory(new InetSocketAddress(clientPort), numConnections); | |
standaloneServerFactory.startup(server); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.net.InetSocketAddress; | |
import static org.junit.Assert.*; | |
import org.apache.zookeeper.CreateMode; | |
import org.apache.zookeeper.ZooDefs; | |
import org.apache.zookeeper.ZooKeeper; | |
import org.apache.zookeeper.data.Stat; | |
import org.apache.zookeeper.server.NIOServerCnxn; | |
import org.apache.zookeeper.server.ZooKeeperServer; | |
import org.junit.Test; | |
public class ZookeeperTest { | |
@Test | |
public void create_get_test() throws Exception { | |
// zk server | |
int clientPort = 21818; | |
int numConnections = 5000; | |
int tickTime = 2000; | |
String dataDirectory = System.getProperty("java.io.tmpdir"); | |
File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile(); | |
ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime); | |
NIOServerCnxn.Factory standaloneServerFactory = new NIOServerCnxn.Factory(new InetSocketAddress(clientPort), numConnections); | |
standaloneServerFactory.startup(server); | |
ZooKeeper zk1 = new ZooKeeper("127.0.0.1:" + clientPort, 30000, null); | |
ZooKeeper zk2 = new ZooKeeper("127.0.0.1:" + clientPort, 30000, null); | |
try { | |
String path = "/clientId"; | |
String ip = "10.13.3.1"; | |
zk1.create(path, ip.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); | |
assertEquals(ip, new String(zk1.getData(path, null, null))); | |
// zk1 first set it | |
String ipChange = "10.13.3.2"; | |
Stat stat = zk1.exists(path, true); | |
assertEquals(0, stat.getVersion()); | |
zk1.setData(path, ipChange.getBytes(), stat.getVersion()); | |
// version | |
assertEquals(1, zk1.exists(path, true).getVersion()); | |
assertEquals(1, zk2.exists(path, true).getVersion()); | |
// data | |
assertEquals(ipChange, new String(zk1.getData(path, null, null))); | |
assertEquals(ipChange, new String(zk2.getData(path, null, null))); | |
// zk2 change it, version plus 1 | |
ipChange = "10.13.3.3"; | |
stat = zk2.exists(path, true); | |
assertEquals(1, stat.getVersion()); | |
zk2.setData(path, ipChange.getBytes(), stat.getVersion()); | |
// version | |
assertEquals(2, zk1.exists(path, true).getVersion()); | |
assertEquals(2, zk2.exists(path, true).getVersion()); | |
// data | |
assertEquals(ipChange, new String(zk1.getData(path, null, null))); | |
assertEquals(ipChange, new String(zk2.getData(path, null, null))); | |
// zk1 change it again | |
ipChange = "10.13.3.4"; | |
stat = zk1.exists(path, true); | |
assertEquals(2, stat.getVersion()); | |
zk1.setData(path, ipChange.getBytes(), stat.getVersion()); | |
// version | |
assertEquals(3, zk1.exists(path, true).getVersion()); | |
assertEquals(3, zk2.exists(path, true).getVersion()); | |
// data | |
assertEquals(ipChange, new String(zk1.getData(path, null, null))); | |
assertEquals(ipChange, new String(zk2.getData(path, null, null))); | |
} catch (Exception e) { | |
throw e; | |
} finally { | |
zk1.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment