Exemplo n.º 1
0
  @SuppressWarnings("unchecked")
  public T poll() throws Exception {

    try {

      List<String> list = zkClient.getChildren(root);
      if (list.size() == 0) {
        return null;
      }
      Collections.sort(
          list,
          new Comparator<String>() {
            public int compare(String lhs, String rhs) {
              return getNodeNumber(lhs, Node_NAME).compareTo(getNodeNumber(rhs, Node_NAME));
            }
          });

      for (String nodeName : list) {

        String nodeFullPath = root.concat("/").concat(nodeName);
        try {
          T node = (T) zkClient.readData(nodeFullPath);
          zkClient.delete(nodeFullPath);
          return node;
        } catch (ZkNoNodeException e) {
          // ignore
        }
      }

      return null;

    } catch (Exception e) {
      throw ExceptionUtil.convertToRuntimeException(e);
    }
  }
Exemplo n.º 2
0
 public String enqueue(T element) {
   try {
     String sequential = _zkClient.createPersistentSequential(getElementRoughPath(), element);
     String elementId = sequential.substring(sequential.lastIndexOf('/') + 1);
     return elementId;
   } catch (Exception e) {
     throw ExceptionUtil.convertToRuntimeException(e);
   }
 }
Exemplo n.º 3
0
  public boolean offer(T element) throws Exception {

    String nodeFullPath = root.concat("/").concat(Node_NAME);
    try {
      zkClient.createPersistentSequential(nodeFullPath, element);
    } catch (ZkNoNodeException e) {
      zkClient.createPersistent(root);
      offer(element);
    } catch (Exception e) {
      throw ExceptionUtil.convertToRuntimeException(e);
    }
    return true;
  }
Exemplo n.º 4
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);
   }
 }