本文整理汇总了Java中org.apache.zookeeper.ZooKeeper.setData方法的典型用法代码示例。如果您正苦于以下问题:Java ZooKeeper.setData方法的具体用法?Java ZooKeeper.setData怎么用?Java ZooKeeper.setData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.zookeeper.ZooKeeper
的用法示例。
在下文中一共展示了ZooKeeper.setData方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: preAuth
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public void preAuth() throws Exception {
ZooKeeper zk = createClient();
zk.addAuthInfo("key", "25".getBytes());
try {
createNodePrintAcl(zk, "/pre", "testPreAuth");
zk.setACL("/", Ids.CREATOR_ALL_ACL, -1);
zk.getChildren("/", false);
zk.create("/abc", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
zk.setData("/abc", "testData1".getBytes(), -1);
zk.create("/key", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
zk.setData("/key", "5".getBytes(), -1);
Thread.sleep(1000);
} catch (KeeperException e) {
Assert.fail("test failed :" + e);
} finally {
zk.close();
}
}
示例2: main
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public static void main(String[] args) {
String connStr = "127.0.0.1:2181";
try {
ZooKeeper zookeeper = new ZooKeeper(connStr, 100000,
new Watcher() {
public void process(WatchedEvent event) {
logger.debug("监控被触发的事件");
}
});
Stat stat = new Stat();
byte[] result = zookeeper.getData("/saf_service/com.ipd.testjsf.HelloBaontService/providers", false, stat);
zookeeper.setData("/saf_service/com.ipd.testjsf.HelloBaontService/providers", result, stat.getVersion());
System.out.println(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
示例3: testReadOnlyClient
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* Basic test of read-only client functionality. Tries to read and write
* during read-only mode, then regains a quorum and tries to write again.
*/
@Test(timeout = 90000)
public void testReadOnlyClient() throws Exception {
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected
final String data = "Data to be read in RO mode";
final String node = "/tnode";
zk.create(node, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
watcher.reset();
qu.shutdown(2);
watcher.waitForConnected(CONNECTION_TIMEOUT);
// read operation during r/o mode
String remoteData = new String(zk.getData(node, false, null));
Assert.assertEquals(data, remoteData);
try {
zk.setData(node, "no way".getBytes(), -1);
Assert.fail("Write operation has succeeded during RO mode");
} catch (NotReadOnlyException e) {
// ok
}
watcher.reset();
qu.start(2);
Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
"127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
watcher.waitForConnected(CONNECTION_TIMEOUT);
zk.setData(node, "We're in the quorum now".getBytes(), -1);
zk.close();
}
示例4: opretion
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Override
public Stat opretion(ZooKeeper zkClient, String nodePath) throws KeeperException, InterruptedException {
Stat stat = null;
if (null == zkClient) {
return stat;
}
stat = zkClient.setData(nodePath, AgentType.FREE, -1);
return stat;
}
示例5: opretion
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Override
public Stat opretion(ZooKeeper zkClient, String nodePath) throws KeeperException, InterruptedException {
Stat stat = null;
if (null == zkClient) {
return stat;
}
stat = zkClient.setData(nodePath, AgentType.BUSYNESS, -1);
return stat;
}
示例6: opretion
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Override
public Stat opretion(ZooKeeper zkClient, String nodePath) throws KeeperException, InterruptedException {
Stat stat = null;
if (null == zkClient) {
return stat;
}
stat = zkClient.setData(nodePath, AgentType.STOP, -1);
return stat;
}
示例7: delQuota
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* this method deletes quota for a node.
*
* @param zk the zookeeper client
* @param path the path to delete quota for
* @param bytes true if number of bytes needs to be unset
* @param numNodes true if number of nodes needs to be unset
* @return true if quota deletion is successful
* @throws KeeperException
* @throws IOException
* @throws InterruptedException
*/
public static boolean delQuota(ZooKeeper zk, String path,
boolean bytes, boolean numNodes)
throws KeeperException, IOException, InterruptedException, MalformedPathException {
String parentPath = Quotas.quotaZookeeper + path;
String quotaPath = Quotas.quotaZookeeper + path + "/" +
Quotas.limitNode;
if (zk.exists(quotaPath, false) == null) {
System.out.println("Quota does not exist for " + path);
return true;
}
byte[] data = null;
try {
data = zk.getData(quotaPath, false, new Stat());
} catch (IllegalArgumentException ex) {
throw new MalformedPathException(ex.getMessage());
} catch (KeeperException.NoNodeException ne) {
System.err.println("quota does not exist for " + path);
return true;
}
StatsTrack strack = new StatsTrack(new String(data));
if (bytes && !numNodes) {
strack.setBytes(-1L);
zk.setData(quotaPath, strack.toString().getBytes(), -1);
} else if (!bytes && numNodes) {
strack.setCount(-1);
zk.setData(quotaPath, strack.toString().getBytes(), -1);
} else if (bytes && numNodes) {
// delete till you can find a node with more than
// one child
List<String> children = zk.getChildren(parentPath, false);
/// delete the direct children first
for (String child : children) {
zk.delete(parentPath + "/" + child, -1);
}
// cut the tree till their is more than one child
trimProcQuotas(zk, parentPath);
}
return true;
}
示例8: testServerHasConfig
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public static String testServerHasConfig(ZooKeeper zk,
List<String> joiningServers, List<String> leavingServers)
throws KeeperException, InterruptedException {
boolean testNodeExists = false;
byte[] config = null;
for (int j = 0; j < 30; j++) {
try {
if (!testNodeExists) {
createZNode(zk, "/dummy", "dummy");
testNodeExists = true;
}
// Use setData instead of sync API to force a view update.
// Check ZOOKEEPER-2137 for details.
zk.setData("/dummy", "dummy".getBytes(), -1);
config = zk.getConfig(false, new Stat());
break;
} catch (KeeperException.ConnectionLossException e) {
if (j < 29) {
Thread.sleep(1000);
} else {
// test fails if we still can't connect to the quorum after
// 30 seconds.
Assert.fail("client could not connect to reestablished quorum: giving up after 30+ seconds.");
}
}
}
String configStr = new String(config);
if (joiningServers != null) {
for (String joiner : joiningServers) {
Assert.assertTrue(configStr.contains(joiner));
}
}
if (leavingServers != null) {
for (String leaving : leavingServers)
Assert.assertFalse(configStr.contains("server.".concat(leaving)));
}
return configStr;
}
示例9: testNormalOperation
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public static void testNormalOperation(ZooKeeper writer, ZooKeeper reader)
throws KeeperException, InterruptedException {
boolean testReaderNodeExists = false;
boolean testWriterNodeExists = false;
for (int j = 0; j < 30; j++) {
try {
if (!testWriterNodeExists) {
createZNode(writer, "/test", "test");
testWriterNodeExists = true;
}
if (!testReaderNodeExists) {
createZNode(reader, "/dummy", "dummy");
testReaderNodeExists = true;
}
String data = "test" + j;
writer.setData("/test", data.getBytes(), -1);
// Use setData instead of sync API to force a view update.
// Check ZOOKEEPER-2137 for details.
reader.setData("/dummy", "dummy".getBytes(), -1);
byte[] res = reader.getData("/test", null, new Stat());
Assert.assertEquals(data, new String(res));
break;
} catch (KeeperException.ConnectionLossException e) {
if (j < 29) {
Thread.sleep(1000);
} else {
// test fails if we still can't connect to the quorum after
// 30 seconds.
Assert.fail("client could not connect to reestablished quorum: giving up after 30+ seconds.");
}
}
}
}
示例10: validAuth
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public void validAuth() throws Exception {
ZooKeeper zk = createClient();
// any multiple of 5 will do...
zk.addAuthInfo("key", "25".getBytes());
try {
createNodePrintAcl(zk, "/valid", "testValidAuth");
zk.getData("/abc", false, null);
zk.setData("/abc", "testData3".getBytes(), -1);
} catch (KeeperException.AuthFailedException e) {
Assert.fail("test failed :" + e);
} finally {
zk.close();
}
}
示例11: validAuth2
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public void validAuth2() throws Exception {
ZooKeeper zk = createClient();
// any multiple of 5 will do...
zk.addAuthInfo("key", "125".getBytes());
try {
createNodePrintAcl(zk, "/valid2", "testValidAuth2");
zk.getData("/abc", false, null);
zk.setData("/abc", "testData3".getBytes(), -1);
} catch (KeeperException.AuthFailedException e) {
Assert.fail("test failed :" + e);
} finally {
zk.close();
}
}
示例12: testQuota
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testQuota() throws IOException,
InterruptedException, KeeperException, Exception {
final ZooKeeper zk = createClient();
final String path = "/a/b/v";
// making sure setdata works on /
zk.setData("/", "some".getBytes(), -1);
zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.create("/a/b/v/d", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
ZooKeeperMain.createQuota(zk, path, 5L, 10);
// see if its set
String absolutePath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode;
byte[] data = zk.getData(absolutePath, false, new Stat());
StatsTrack st = new StatsTrack(new String(data));
Assert.assertTrue("bytes are set", st.getBytes() == 5L);
Assert.assertTrue("num count is set", st.getCount() == 10);
String statPath = Quotas.quotaZookeeper + path + "/" + Quotas.statNode;
byte[] qdata = zk.getData(statPath, false, new Stat());
StatsTrack qst = new StatsTrack(new String(qdata));
Assert.assertTrue("bytes are set", qst.getBytes() == 8L);
Assert.assertTrue("count is set", qst.getCount() == 2);
//force server to restart and load from snapshot, not txn log
stopServer();
startServer();
stopServer();
startServer();
ZooKeeperServer server = getServer(serverFactory);
Assert.assertNotNull("Quota is still set",
server.getZKDatabase().getDataTree().getMaxPrefixWithQuota(path) != null);
}
示例13: testChrootSynchronous
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testChrootSynchronous()
throws IOException, InterruptedException, KeeperException
{
ZooKeeper zk1 = createClient();
try {
zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
} finally {
if(zk1 != null)
zk1.close();
}
ZooKeeper zk2 = createClient(hostPort + "/ch1");
try {
Assert.assertEquals("/ch2",
zk2.create("/ch2", null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT));
} finally {
if(zk2 != null)
zk2.close();
}
zk1 = createClient();
zk2 = createClient(hostPort + "/ch1");
try {
// check get
MyWatcher w1 = new MyWatcher("/ch1");
Assert.assertNotNull(zk1.exists("/ch1", w1));
MyWatcher w2 = new MyWatcher("/ch1/ch2");
Assert.assertNotNull(zk1.exists("/ch1/ch2", w2));
MyWatcher w3 = new MyWatcher("/ch2");
Assert.assertNotNull(zk2.exists("/ch2", w3));
// set watches on child
MyWatcher w4 = new MyWatcher("/ch1");
zk1.getChildren("/ch1",w4);
MyWatcher w5 = new MyWatcher("/");
zk2.getChildren("/",w5);
// check set
zk1.setData("/ch1", "1".getBytes(), -1);
zk2.setData("/ch2", "2".getBytes(), -1);
// check watches
Assert.assertTrue(w1.matches());
Assert.assertTrue(w2.matches());
Assert.assertTrue(w3.matches());
// check exceptions
try {
zk2.setData("/ch3", "3".getBytes(), -1);
} catch (KeeperException.NoNodeException e) {
Assert.assertEquals("/ch3", e.getPath());
}
Assert.assertTrue(Arrays.equals("1".getBytes(),
zk1.getData("/ch1", false, null)));
Assert.assertTrue(Arrays.equals("2".getBytes(),
zk1.getData("/ch1/ch2", false, null)));
Assert.assertTrue(Arrays.equals("2".getBytes(),
zk2.getData("/ch2", false, null)));
// check delete
zk2.delete("/ch2", -1);
Assert.assertTrue(w4.matches());
Assert.assertTrue(w5.matches());
zk1.delete("/ch1", -1);
Assert.assertNull(zk1.exists("/ch1", false));
Assert.assertNull(zk1.exists("/ch1/ch2", false));
Assert.assertNull(zk2.exists("/ch2", false));
} finally {
if(zk1 != null)
zk1.close();
if(zk2 != null)
zk2.close();
}
}
示例14: testChrootSynchronous
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testChrootSynchronous()
throws IOException, InterruptedException, KeeperException
{
ZooKeeper zk1 = createClient();
try {
zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
} finally {
if(zk1 != null)
zk1.close();
}
ZooKeeper zk2 = createClient(hostPort + "/ch1");
try {
Assert.assertEquals("/ch2",
zk2.create("/ch2", null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT));
} finally {
if(zk2 != null)
zk2.close();
}
zk1 = createClient();
zk2 = createClient(hostPort + "/ch1");
try {
// check get
MyWatcher w1 = new MyWatcher("/ch1");
Assert.assertNotNull(zk1.exists("/ch1", w1));
MyWatcher w2 = new MyWatcher("/ch1/ch2");
Assert.assertNotNull(zk1.exists("/ch1/ch2", w2));
MyWatcher w3 = new MyWatcher("/ch2");
Assert.assertNotNull(zk2.exists("/ch2", w3));
// set watches on child
MyWatcher w4 = new MyWatcher("/ch1");
zk1.getChildren("/ch1",w4);
MyWatcher w5 = new MyWatcher("/");
zk2.getChildren("/",w5);
// check set
zk1.setData("/ch1", "1".getBytes(), -1);
zk2.setData("/ch2", "2".getBytes(), -1);
// check watches
Assert.assertTrue(w1.matches());
Assert.assertTrue(w2.matches());
Assert.assertTrue(w3.matches());
// check exceptions
try {
zk2.setData("/ch3", "3".getBytes(), -1);
} catch (KeeperException.NoNodeException e) {
Assert.assertEquals("/ch3", e.getPath());
}
Assert.assertTrue(Arrays.equals("1".getBytes(),
zk1.getData("/ch1", false, null)));
Assert.assertTrue(Arrays.equals("2".getBytes(),
zk1.getData("/ch1/ch2", false, null)));
Assert.assertTrue(Arrays.equals("2".getBytes(),
zk2.getData("/ch2", false, null)));
// check delete
zk2.delete("/ch2", -1);
Assert.assertTrue(w4.matches());
Assert.assertTrue(w5.matches());
zk1.delete("/ch1", -1);
Assert.assertNull(zk1.exists("/ch1", false));
Assert.assertNull(zk1.exists("/ch1/ch2", false));
Assert.assertNull(zk2.exists("/ch2", false));
} finally {
if(zk1 != null)
zk1.close();
if(zk2 != null)
zk2.close();
}
}