@Test public void test() throws Exception { class ConnectionWatcher implements Watcher { private ZooKeeper zk; private CountDownLatch latch = new CountDownLatch(1); public void connect(String hosts) throws IOException, InterruptedException { zk = new ZooKeeper(hosts, 5000, this); latch.await(); } public ZooKeeper getZooKeeper() { return zk; } @Override public void process(WatchedEvent event) { if (event.getState() == KeeperState.SyncConnected) { latch.countDown(); } } public void close() throws InterruptedException { if (zk != null) { zk.close(); } } } String path = "/data"; String data = "Hello"; ConnectionWatcher watcher = new ConnectionWatcher(); watcher.connect(cluster.getHosts()); watcher.getZooKeeper().create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); watcher.close(); watcher = new ConnectionWatcher(); watcher.connect(cluster.getHosts()); byte[] actualData = watcher.getZooKeeper().getData(path, false, null); assertEquals(data, new String(actualData)); watcher.close(); }
private List<String> getHosts(List<NodeMetadata> nodes) { return Lists.transform(Lists.newArrayList(nodes), new Function<NodeMetadata, String>() { @Override public String apply(NodeMetadata node) { String publicIp = Iterables.get(node.getPublicAddresses(), 0) .getHostName(); return String.format("%s:%d", publicIp, CLIENT_PORT); } }); }
ZooKeeperService extends Service { private List<String> getHosts(List<NodeMetadata> nodes) { return Lists.transform(Lists.newArrayList(nodes), new Function<NodeMetadata, String>() { @Override public String apply(NodeMetadata node) { String publicIp = Iterables.get(node.getPublicAddresses(), 0) .getHostName(); return String.format("%s:%d", publicIp, CLIENT_PORT); } }); } }
ZooKeeperService extends Service { private List<String> getHosts(List<NodeMetadata> nodes) { return Lists.transform(Lists.newArrayList(nodes), new Function<NodeMetadata, String>() { @Override public String apply(NodeMetadata node) { String publicIp = Iterables.get(node.getPublicAddresses(), 0) .getHostName(); return String.format("%s:%d", publicIp, CLIENT_PORT); } }); } ZooKeeperService(ServiceSpec serviceSpec); }
ZooKeeperService extends Service { private List<String> getHosts(List<NodeMetadata> nodes) { return Lists.transform(Lists.newArrayList(nodes), new Function<NodeMetadata, String>() { @Override public String apply(NodeMetadata node) { String publicIp = Iterables.get(node.getPublicAddresses(), 0) .getHostName(); return String.format("%s:%d", publicIp, CLIENT_PORT); } }); } ZooKeeperService(ServiceSpec serviceSpec); @Override ZooKeeperCluster launchCluster(ClusterSpec clusterSpec); }
ZooKeeperService extends Service { private List<String> getHosts(List<NodeMetadata> nodes) { return Lists.transform(Lists.newArrayList(nodes), new Function<NodeMetadata, String>() { @Override public String apply(NodeMetadata node) { String publicIp = Iterables.get(node.getPublicAddresses(), 0) .getHostName(); return String.format("%s:%d", publicIp, CLIENT_PORT); } }); } ZooKeeperService(ServiceSpec serviceSpec); @Override ZooKeeperCluster launchCluster(ClusterSpec clusterSpec); static final String ZOOKEEPER_ROLE; }