public ZookeeprTickCondition(String workPath, ZkClient zkClient) {
    this.zkClient = zkClient;
    this.workPath = ZookeeperConstant.ROOT + "/" + workPath;

    zkClient.createPersistent(this.workPath, true);
    zkClient.subscribeChildChanges(
        this.workPath,
        new IZkChildListener() {

          @Override
          public void handleChildChange(String parentPath, List<String> currentChilds)
              throws Exception {
            if (currentChilds != null && !currentChilds.isEmpty()) {
              return;
            }
            tryOccupy();
          }
        });
  }
示例#2
0
 @SuppressWarnings("unchecked")
 protected Element<T> getFirstElement() throws InterruptedException {
   final Object mutex = new Object();
   IZkChildListener notifyListener =
       new IZkChildListener() {
         @Override
         public void handleChildChange(String parentPath, List<String> currentChilds)
             throws Exception {
           synchronized (mutex) {
             mutex.notify();
           }
         }
       };
   try {
     while (true) {
       List<String> elementNames;
       synchronized (mutex) {
         elementNames = _zkClient.subscribeChildChanges(_elementsPath, notifyListener);
         while (elementNames == null || elementNames.isEmpty()) {
           mutex.wait();
           elementNames = _zkClient.getChildren(_elementsPath);
         }
       }
       String elementName = getSmallestElement(elementNames);
       try {
         String elementPath = getElementPath(elementName);
         return new Element<T>(elementName, (T) _zkClient.readData(elementPath));
       } catch (ZkNoNodeException e) {
         // somebody else picked up the element first, so we have to
         // retry with the new first element
       }
     }
   } catch (InterruptedException e) {
     throw e;
   } catch (Exception e) {
     throw ExceptionUtil.convertToRuntimeException(e);
   } finally {
     _zkClient.unsubscribeChildChanges(_elementsPath, notifyListener);
   }
 }
示例#3
0
 @Override
 public List<String> addChildListener(String path, IZkChildListener listener) {
   return client.subscribeChildChanges(path, listener);
 }
 public List<String> addTargetChildListener(String path, final IZkChildListener listener) {
   return client.subscribeChildChanges(path, listener);
 }
 public static void subscribeChildChanges(String path, IZkChildListener listener) {
   zkClient.subscribeChildChanges(path, listener);
 }