/// <summary> /// Create an persistent node with the given path and data. Create parents if necessary. /// </summary> public static void CreatePersistentPath(ZkClient client, string path, string data) { try { client.CreatePersistent(path, data); } catch (ZkNoNodeException e) { CreateParentPath(client, path); client.CreatePersistent(path, data); } }
/// <summary> /// make sure a persistent path exists in ZK. Create the path if not exist. /// </summary> /// <param name="client"></param> /// <param name="path"></param> public static void MakeSurePersistentPathExists(ZkClient client, string path) { if (!client.Exists(path)) { client.CreatePersistent(path, true); // won't throw NoNodeException or NodeExistsException } }
/// <summary> /// create the parent path /// </summary> /// <param name="client"></param> /// <param name="path"></param> private static void CreateParentPath(ZkClient client, string path) { var parentDir = path.Substring(0, path.LastIndexOf('/')); if (parentDir.Length != 0) { client.CreatePersistent(parentDir, true); } }
public void Init() { var zkOptions = new ZkOptions { ZkServers = "114.215.169.82:12181", SessionTimeout = 1000 * 100, ConnectionTimeout = 1000 * 5, OprationRetryTimeout = 1000 * 100 }; _zkClient = new ZkClient(zkOptions); _zkClient.SubscribeStateChange(new ZkStateListener()); _zkClient.SubscribeChildChange("/data", new ZkChildListener()); _zkClient.SubscribeDataChange("/data", new ZkDataListener()); _zkClient.CreatePersistent("/data").GetAwaiter().GetResult(); }
/// <summary> /// 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 /// </summary> /// <param name="client"></param> /// <param name="path"></param> /// <param name="data"></param> public static void UpdatePersistentPath(ZkClient client, string path, string data) { try { client.WriteData(path, data); } catch (ZkNoNodeException) { CreateParentPath(client, path); try { client.CreatePersistent(path, data); } catch (ZkNodeExistsException) { client.WriteData(path, data); } } }