本文整理汇总了Java中org.I0Itec.zkclient.exception.ZkNodeExistsException类的典型用法代码示例。如果您正苦于以下问题:Java ZkNodeExistsException类的具体用法?Java ZkNodeExistsException怎么用?Java ZkNodeExistsException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ZkNodeExistsException类属于org.I0Itec.zkclient.exception包,在下文中一共展示了ZkNodeExistsException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: NodeMonitor
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
public NodeMonitor(){
childListener = new IZkChildListener() {
public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
if (currentChilds != null) {
initNodes(currentChilds);
}
}
};
List<String> childs = zookeeper.subscribeChildChanges(ArbitrateConstants.NODE_NID_ROOT, childListener);
if (childs == null) {//如果为null,代表系统节点为初始化
try {
zookeeper.createPersistent(ArbitrateConstants.NODE_NID_ROOT, true);
} catch (ZkNodeExistsException e) {
//ignore
}
childs = zookeeper.getChildren(ArbitrateConstants.NODE_NID_ROOT);
}
initNodes(childs);
// syncNodes();// 开始监视node节点的变化
MonitorScheduler.register(this);
}
示例2: registerBrokerInZk
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
public static void registerBrokerInZk(ZkClient zkClient, int id, String host, int port, int timeout, int jmxPort) {
String brokerIdPath = ZkUtils.BrokerIdsPath + "/" + id;
String timestamp = SystemTime.instance.milliseconds() + "";
String brokerInfo = Json.encode(ImmutableMap.of("version", 1, "host", host, "port", port, "jmx_port", jmxPort, "timestamp", timestamp));
Broker expectedBroker = new Broker(id, host, port);
try {
createEphemeralPathExpectConflictHandleZKBug(zkClient, brokerIdPath, brokerInfo, expectedBroker,
new Function2<String, Object, Boolean>() {
@Override
public Boolean apply(String brokerString, Object broker) {
return Brokers.createBroker(((Broker) broker).id, brokerString).equals(broker);
}
}, timeout);
} catch (ZkNodeExistsException e) {
throw new RuntimeException("A broker is already registered on the path " + brokerIdPath
+ ". This probably " + "indicates that you either have configured a brokerid that is already in use, or "
+ "else you have shutdown this broker and restarted it faster than the zookeeper "
+ "timeout so it appears to be re-registering.");
}
logger.info("Registered broker {} at path {} with address {}:{}.", id, brokerIdPath, host, port);
}
示例3: createEphemeralPathExpectConflict
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
/**
* Create an ephemeral node with the given path and data.
* Throw NodeExistException if node already exists.
*/
public static void createEphemeralPathExpectConflict(ZkClient client, String path, String data) {
try {
createEphemeralPath(client, path, data);
} catch (ZkNodeExistsException e) {
// this can happen when there is connection loss; make sure the data is what we intend to write
String storedData = null;
try {
storedData = readData(client, path)._1;
} catch (ZkNoNodeException e1) {
// the node disappeared; treat as if node existed and let caller handles this
}
if (storedData == null || storedData != data) {
logger.info("conflict in {} data: {} stored data: {}", path, data, storedData);
throw e;
} else {
// otherwise, the creation succeeded, return normally
logger.info("{} exists with value {} during connection loss; this is ok", path, data);
}
}
}
示例4: init
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
private void init() {
zkClient = new ZkClient(url, sessionTimeout, connectionTimeout,
new BytesPushThroughSerializer());
try {
zkClient.createPersistent(HOME.concat(root).concat("/").
concat(parent), true);
} catch (ZkNodeExistsException e) {
e.printStackTrace();
}
cleanExecutors = Executors.newCachedThreadPool();
}
示例5: startDetection
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
void startDetection() {
try {
_zkClient.createPersistent(_failedBrokersZkPath);
} catch (ZkNodeExistsException znee) {
// let it go.
}
// Load the failed broker information from zookeeper.
loadPersistedFailedBrokerList();
// Detect broker failures.
detectBrokerFailures();
_zkClient.subscribeChildChanges(ZkUtils.BrokerIdsPath(), new BrokerFailureListener());
}
示例6: initMainstem
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
public void initMainstem() {
if (isStop()) {
return;
}
PermitMonitor permitMonitor = ArbitrateFactory.getInstance(getPipelineId(), PermitMonitor.class);
ChannelStatus status = permitMonitor.getChannelPermit(true);
if (status.isStop()) {
return; // 如果已经关闭则退出
}
Long nid = ArbitrateConfigUtils.getCurrentNid();
String path = StagePathUtils.getMainStem(getPipelineId());
MainStemEventData data = new MainStemEventData();
data.setStatus(MainStemEventData.Status.TAKEING);
data.setPipelineId(getPipelineId());
data.setNid(nid);// 设置当前的nid
// 序列化
byte[] bytes = JsonUtils.marshalToByte(data);
try {
mutex.set(false);
zookeeper.create(path, bytes, CreateMode.EPHEMERAL);
activeData = data;
processActiveEnter();// 触发一下事件
mutex.set(true);
} catch (ZkNodeExistsException e) {
bytes = zookeeper.readData(path, true);
if (bytes == null) {// 如果不存在节点,立即尝试一次
initMainstem();
} else {
activeData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
if (nid.equals(activeData.getNid())) { // reload时会重复创建,如果是自己就触发一下
mutex.set(true);
}
}
}
}
示例7: createPersistent
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
@Override
public void createPersistent(String path) {
try {
client.createPersistent(path, true);
} catch (ZkNodeExistsException e) {
}
}
示例8: createEphemeral
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
@Override
public void createEphemeral(String path) {
try {
client.createEphemeral(path);
} catch (ZkNodeExistsException e) {
}
}
示例9: doCreateData
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
@Override
public void doCreateData(String path, String json) {
try {
client.writeData(path, json);
} catch (ZkNodeExistsException e) {
}
}
示例10: upsertZkNode
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
void upsertZkNode(final String clientId) {
final String node = "/" + clientId;
LOG.debug("Trying to upsert node={}, to host={}", node, this.host);
try {
this.zkClient.createPersistent(node, this.host);
} catch (final ZkNodeExistsException e) {
this.zkClient.writeData(node, this.host);
}
LOG.info("Upserted node={}, to host={}", node, this.host);
}
示例11: maybeCreateZkPath
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
private void maybeCreateZkPath(String path) {
try {
_zkClient.createPersistent(path, true);
} catch (ZkNodeExistsException e) {
LOGGER.debug("Path={} is created in zk already", path);
}
}
示例12: createPersistent
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
protected String createPersistent(String path, boolean sequential) {
try {
if (sequential) {
return zkClient.createPersistentSequential(path, true);
} else {
zkClient.createPersistent(path, true);
return path;
}
} catch (ZkNodeExistsException ignored) {
}
return null;
}
示例13: createEphemeral
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
protected String createEphemeral(String path, boolean sequential) {
try {
if (sequential) {
return zkClient.createEphemeralSequential(path, true);
} else {
zkClient.createEphemeral(path);
return path;
}
} catch (ZkNodeExistsException ignored) {
}
return null;
}
示例14: createEphemeralPathExpectConflictHandleZKBug
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
/**
* Create an ephemeral node with the given path and data.
* Throw NodeExistsException if node already exists.
* Handles the following ZK session timeout bug:
* <p/>
* https://issues.apache.org/jira/browse/ZOOKEEPER-1740
* <p/>
* Upon receiving a NodeExistsException, read the data from the conflicted path and
* trigger the checker function comparing the read data and the expected data,
* If the checker function returns true then the above bug might be encountered, back off and retry;
* otherwise re-throw the exception
*/
public static void createEphemeralPathExpectConflictHandleZKBug(ZkClient zkClient, String path, String data,
Object expectedCallerData,
Function2<String, Object, Boolean> checker,
int backoffTime) {
while (true) {
try {
createEphemeralPathExpectConflict(zkClient, path, data);
return;
} catch (ZkNodeExistsException e) {
// An ephemeral node may still exist even after its corresponding session has expired
// due to a Zookeeper bug, in this case we need to retry writing until the previous node is deleted
// and hence the write succeeds without ZkNodeExistsException
String writtenData = ZkUtils.readDataMaybeNull(zkClient, path)._1;
if (writtenData == null) {
// the node disappeared; retry creating the ephemeral node immediately
} else {
if (checker.apply(writtenData, expectedCallerData)) {
logger.info("I wrote this conflicted ephemeral node [{}] at {} a while back in a different session, "
+ "hence I will backoff for this node to be deleted by Zookeeper and retry", data, path);
SystemTime.instance.sleep(backoffTime);
} else {
throw e;
}
}
}
}
}
示例15: updatePersistentPath
import org.I0Itec.zkclient.exception.ZkNodeExistsException; //导入依赖的package包/类
/**
* Update the value of a persistent node with the given path and data.
* create parrent directory if necessary. Never throw NodeExistException.
* Return the updated path zkVersion
*/
public static void updatePersistentPath(ZkClient client, String path, String data) {
try {
client.writeData(path, data);
} catch (ZkNoNodeException e) {
createParentPath(client, path);
try {
client.createPersistent(path, data);
} catch (ZkNodeExistsException e1) {
client.writeData(path, data);
}
}
}