本文整理匯總了Java中org.apache.zookeeper.ZooDefs類的典型用法代碼示例。如果您正苦於以下問題:Java ZooDefs類的具體用法?Java ZooDefs怎麽用?Java ZooDefs使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ZooDefs類屬於org.apache.zookeeper包,在下文中一共展示了ZooDefs類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testCreate
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
@Test
public void testCreate()
throws IOException, KeeperException, InterruptedException {
Stat stat = new Stat();
zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, stat, 100);
Assert.assertEquals(0, stat.getEphemeralOwner());
final AtomicLong fakeElapsed = new AtomicLong(0);
ContainerManager containerManager = newContainerManager(fakeElapsed);
containerManager.checkContainers();
Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false));
fakeElapsed.set(1000);
containerManager.checkContainers();
Assert.assertNull("Ttl node should have been deleted", zk.exists("/foo", false));
}
示例2: connect
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
private void connect(TxZookeeperConfig config) {
try {
zooKeeper = new ZooKeeper(config.getHost(), config.getSessionTimeOut(), watchedEvent -> {
if (watchedEvent.getState() == Watcher.Event.KeeperState.SyncConnected) {
// 放開閘門, wait在connect方法上的線程將被喚醒
COUNT_DOWN_LATCH.countDown();
}
});
COUNT_DOWN_LATCH.await();
Stat stat = zooKeeper.exists(rootPath, false);
if (stat == null) {
zooKeeper.create(rootPath, rootPath.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
} catch (Exception e) {
throw new TransactionIoException(e);
}
}
開發者ID:yu199195,項目名稱:happylifeplat-transaction,代碼行數:20,代碼來源:ZookeeperTransactionRecoverRepository.java
示例3: updateRetry
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
/**
* 更改恢複次數
*
* @param id 事務id
* @param retry 恢複次數
* @param applicationName 應用名稱
* @return true 成功
*/
@Override
public Boolean updateRetry(String id, Integer retry, String applicationName) {
if (StringUtils.isBlank(id) || StringUtils.isBlank(applicationName) || Objects.isNull(retry)) {
return Boolean.FALSE;
}
final String rootPath = RepositoryPathUtils.buildZookeeperPath(applicationName);
final String path = buildRootPath(rootPath, id);
try {
byte[] content = zooKeeper.getData(path,
false, new Stat());
final TransactionRecoverAdapter adapter = objectSerializer.deSerialize(content, TransactionRecoverAdapter.class);
adapter.setLastTime(DateUtils.getDateYYYY());
adapter.setRetriedCount(retry);
zooKeeper.create(path,
objectSerializer.serialize(adapter),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
return Boolean.TRUE;
} catch (Exception e) {
e.printStackTrace();
}
return Boolean.FALSE;
}
開發者ID:yu199195,項目名稱:happylifeplat-transaction,代碼行數:32,代碼來源:ZookeeperRecoverTransactionServiceImpl.java
示例4: testNodeChildrenChanged
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
@Test
public void testNodeChildrenChanged() throws Exception {
QuorumUtil qu = new QuorumUtil(1);
qu.startAll();
EventsWatcher watcher = new EventsWatcher();
ZooKeeper zk1 = createClient(qu, 1, watcher);
ZooKeeper zk2 = createClient(qu, 2);
String path = "/test-children-changed";
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.getChildren(path, watcher);
qu.shutdown(1);
zk2.create(path + "/children-1", new byte[2],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeChildrenChanged);
qu.shutdownAll();
}
示例5: getItemPerms
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
private int getItemPerms(TableItem item) {
int perms = 0;
Button allCheckBox = getItemPermCheckBox(item, ZooDefs.Perms.ALL);
if (allCheckBox.getSelection()) {
perms = ZooDefs.Perms.ALL;
}
else {
for (int perm : PERMS) {
Button permCheckBox = getItemPermCheckBox(item, perm);
if (permCheckBox.getSelection()) {
perms |= perm;
}
}
}
return perms;
}
示例6: ZkDistributeLock
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
/**
* @param zkServers
* @param lockName
* @param sessionTimeout
*/
public ZkDistributeLock(String zkServers, String lockName, int sessionTimeout) {
if (lockName.contains(LOCK_KEY_SUFFIX)) {
throw new LockException("lockName 不能包含[" + LOCK_KEY_SUFFIX + "]");
}
this.lockName = lockName;
this.sessionTimeout = sessionTimeout;
try {
zk = new ZooKeeper(zkServers, sessionTimeout, this);
Stat stat = zk.exists(ROOT_PATH, false);
if (stat == null) {
// 創建根節點
zk.create(ROOT_PATH, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
} catch (Exception e) {
throw new LockException(e);
}
}
示例7: sendSaslPacket
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
private void sendSaslPacket(byte[] saslToken, ClientCnxn cnxn)
throws SaslException{
if (LOG.isDebugEnabled()) {
LOG.debug("ClientCnxn:sendSaslPacket:length="+saslToken.length);
}
GetSASLRequest request = new GetSASLRequest();
request.setToken(saslToken);
SetSASLResponse response = new SetSASLResponse();
ServerSaslResponseCallback cb = new ServerSaslResponseCallback();
try {
cnxn.sendPacket(request,response,cb, ZooDefs.OpCode.sasl);
} catch (IOException e) {
throw new SaslException("Failed to send SASL packet to server.",
e);
}
}
示例8: addRequestToSyncProcessor
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
private void addRequestToSyncProcessor() {
long zxid = ZxidUtils.makeZxid(3, 7);
TxnHeader hdr = new TxnHeader(1, 1, zxid, 1,
ZooDefs.OpCode.setData);
Record txn = new SetDataTxn("/foo" + zxid, new byte[0], 1);
byte[] buf;
try {
buf = Util.marshallTxnEntry(hdr, txn);
} catch (IOException e) {
LOG.error("IOException while adding request to SyncRequestProcessor", e);
Assert.fail("IOException while adding request to SyncRequestProcessor!");
return;
}
NettyServerCnxnFactory factory = new NettyServerCnxnFactory();
final MockNettyServerCnxn nettyCnxn = new MockNettyServerCnxn(null,
this, factory);
Request req = new Request(nettyCnxn, 1, 1, ZooDefs.OpCode.setData,
ByteBuffer.wrap(buf), null);
req.hdr = hdr;
req.txn = txn;
syncProcessor.processRequest(req);
}
示例9: testNodeCreated
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
@Test
public void testNodeCreated() throws Exception {
QuorumUtil qu = new QuorumUtil(1);
qu.startAll();
EventsWatcher watcher = new EventsWatcher();
ZooKeeper zk1 = createClient(qu, 1, watcher);
ZooKeeper zk2 = createClient(qu, 2);
String path = "/test1-created";
zk1.exists(path, watcher);
qu.shutdown(1);
zk2.create(path, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeCreated);
qu.shutdownAll();
}
示例10: resetAcls
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
private static void resetAcls(final ZooKeeperWatcher zkw, final String znode,
final boolean eraseAcls) throws Exception {
List<String> children = ZKUtil.listChildrenNoWatch(zkw, znode);
if (children != null) {
for (String child: children) {
resetAcls(zkw, ZKUtil.joinZNode(znode, child), eraseAcls);
}
}
ZooKeeper zk = zkw.getRecoverableZooKeeper().getZooKeeper();
if (eraseAcls) {
LOG.info(" - erase ACLs for " + znode);
zk.setACL(znode, ZooDefs.Ids.OPEN_ACL_UNSAFE, -1);
} else {
LOG.info(" - set ACLs for " + znode);
zk.setACL(znode, ZKUtil.createACL(zkw, znode, true), -1);
}
}
示例11: testSessionEstablishment
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
/**
* Tests a situation when client firstly connects to a read-only server and
* then connects to a majority server. Transition should be transparent for
* the user.
*/
@Test(timeout = 90000)
public void testSessionEstablishment() throws Exception {
qu.shutdown(2);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT);
Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
.getState());
long fakeId = zk.getSessionId();
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.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
Assert.assertFalse("fake session and real session have same id", zk
.getSessionId() == fakeId);
zk.close();
}
示例12: updateParticipant
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
/**
* 更新 List<Participant> 隻更新這一個字段數據
*
* @param tccTransaction 實體對象
*/
@Override
public int updateParticipant(TccTransaction tccTransaction) {
final String path = RepositoryPathUtils.buildZookeeperRootPath(rootPathPrefix, tccTransaction.getTransId());
try {
byte[] content = zooKeeper.getData(path,
false, new Stat());
final CoordinatorRepositoryAdapter adapter = objectSerializer.deSerialize(content, CoordinatorRepositoryAdapter.class);
adapter.setContents(objectSerializer.serialize(tccTransaction.getParticipants()));
zooKeeper.create(path,
objectSerializer.serialize(adapter),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
return 1;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
示例13: updateStatus
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
/**
* 更新補償數據狀態
*
* @param id 事務id
* @param status 狀態
* @return rows 1 成功 0 失敗
*/
@Override
public int updateStatus(String id, Integer status) {
final String path = RepositoryPathUtils.buildZookeeperRootPath(rootPathPrefix,id);
try {
byte[] content = zooKeeper.getData(path,
false, new Stat());
final CoordinatorRepositoryAdapter adapter = objectSerializer.deSerialize(content, CoordinatorRepositoryAdapter.class);
adapter.setStatus(status);
zooKeeper.create(path,
objectSerializer.serialize(adapter),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
return 1;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
示例14: connect
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
private void connect(TccZookeeperConfig config) {
try {
zooKeeper = new ZooKeeper(config.getHost(), config.getSessionTimeOut(), watchedEvent -> {
if (watchedEvent.getState() == Watcher.Event.KeeperState.SyncConnected) {
// 放開閘門, wait在connect方法上的線程將被喚醒
LATCH.countDown();
}
});
LATCH.await();
Stat stat = zooKeeper.exists(rootPathPrefix, false);
if (stat == null) {
zooKeeper.create(rootPathPrefix, rootPathPrefix.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
} catch (Exception e) {
throw new TccRuntimeException(e);
}
}
示例15: updateRetry
import org.apache.zookeeper.ZooDefs; //導入依賴的package包/類
/**
* 更改恢複次數
*
* @param id 事務id
* @param retry 恢複次數
* @param applicationName 應用名稱
* @return true 成功
*/
@Override
public Boolean updateRetry(String id, Integer retry, String applicationName) {
if (StringUtils.isBlank(id) || StringUtils.isBlank(applicationName) || Objects.isNull(retry)) {
return Boolean.FALSE;
}
final String rootPathPrefix = RepositoryPathUtils.buildZookeeperPathPrefix(applicationName);
final String path = RepositoryPathUtils.buildZookeeperRootPath(rootPathPrefix, id);
try {
byte[] content = zooKeeper.getData(path,
false, new Stat());
final CoordinatorRepositoryAdapter adapter = objectSerializer.deSerialize(content, CoordinatorRepositoryAdapter.class);
adapter.setLastTime(DateUtils.getDateYYYY());
adapter.setRetriedCount(retry);
zooKeeper.create(path,
objectSerializer.serialize(adapter),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
return Boolean.TRUE;
} catch (Exception e) {
e.printStackTrace();
}
return Boolean.FALSE;
}