zookeeper(8)分布式队列

1. element  方法  获取对列头部第一个元素

 查找队列znode 下所有的子节点名称   使用TreeMap给顺序编号排序  返回第一个znode对应的值

  1. public byte[] element() throws NoSuchElementException, KeeperException, InterruptedException {  
  2.     TreeMap<Long,String> orderedChildren;  
  3.   
  4.     while(true){  
  5.         try{  
  6.             // 返回对列中的全部元素name  这里使用到了TreeMap  
  7.             // map的key为znode的顺序编号    
  8.             orderedChildren = orderedChildren(null);  
  9.         }catch(KeeperException.NoNodeException e){  
  10.             throw new NoSuchElementException();  
  11.         }  
  12.         if(orderedChildren.size() == 0 ) throw new NoSuchElementException();  
  13.   
  14.         for(String headNode : orderedChildren.values()){  
  15.             if(headNode != null){  
  16.                 try{  
  17.                     // 返回对列头部第一个znode对应数据.  
  18.                     return zookeeper.getData(dir+"/"+headNode, falsenull);  
  19.                 }catch(KeeperException.NoNodeException e){  
  20.                 }  
  21.             }  
  22.         }  
  23.   
  24.     }  
  25. }  

获取所有子节点名称
  1. private TreeMap<Long,String> orderedChildren(Watcher watcher) throws KeeperException, InterruptedException {  
  2.      TreeMap<Long,String> orderedChildren = new TreeMap<Long,String>();  
  3.      List<String> childNames = null;  
  4.      try{  
  5.         // 返回对列节点下   所有的子znode 名称  
  6.          childNames = zookeeper.getChildren(dir, watcher);  
  7.      }catch (KeeperException.NoNodeException e){  
  8.          throw e;  
  9.      }  
  10.   
  11.       // 所有子节点  
  12.      for(String childName : childNames){  
  13.          try{  
  14.              //Check format  
  15.              if(!childName.regionMatches(0, prefix, 0, prefix.length())){  
  16.                  LOG.warn("Found child node with improper name: " + childName);  
  17.                  continue;  
  18.              }  
  19.              String suffix = childName.substring(prefix.length());  
  20.              // 顺序节点编号  
  21.              Long childId = new Long(suffix);  
  22.              orderedChildren.put(childId,childName);  
  23.          }catch(NumberFormatException e){  
  24.              LOG.warn("Found child node with improper format : " + childName + " " + e,e);  
  25.          }  
  26.      }  
  27.      return orderedChildren;  
  28.  }  

2.   remove  返回对列头部的第一个元素的值  并删除该znode

  1. public byte[] remove() throws NoSuchElementException, KeeperException, InterruptedException {  
  2.     TreeMap<Long,String> orderedChildren;  
  3.     while(true){  
  4.         try{  
  5.             // 查找所有子节点  用TreeMap排序  
  6.             orderedChildren = orderedChildren(null);  
  7.         }catch(KeeperException.NoNodeException e){  
  8.             throw new NoSuchElementException();  
  9.         }  
  10.         if(orderedChildren.size() == 0throw new NoSuchElementException();  
  11.   
  12.         for(String headNode : orderedChildren.values()){  
  13.             String path = dir +"/"+headNode;  
  14.             try{  
  15.                 // 对列头部 第一个节点对应的数据  
  16.                 byte[] data = zookeeper.getData(path, falsenull);  
  17.                 // 删除该节点  
  18.                 zookeeper.delete(path, -1);  
  19.                 return data;  
  20.             }catch(KeeperException.NoNodeException e){  
  21.                 // Another client deleted the node first.  
  22.             }  
  23.         }  
  24.     }  
  25. }  

3. take 检索并移除对列头部一个znode对应的值  如果对列为空  则一直等待
  1. public byte[] take() throws KeeperException, InterruptedException {  
  2.     TreeMap<Long,String> orderedChildren;  
  3.     // Same as for element.  Should refactor this.  
  4.     while(true){  
  5.         LatchChildWatcher childWatcher = new LatchChildWatcher();  
  6.         try{  
  7.             // 查找所有对列元素 并给对列主znode 设置监视器  
  8.             orderedChildren = orderedChildren(childWatcher);  
  9.         }catch(KeeperException.NoNodeException e){  
  10.             zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT);  
  11.             continue;  
  12.         }  
  13.         // 如果对列中不存在元素  
  14.         // 对列节点下不存在子节点,  线程将一直等待. 使用到CountDownLatch  
  15.         // 当客户端调用 offer方法 写入一个元素时  触发LatchChildWatcher监视器CountDownLatch 计数减1 为0时  当前线程获得运行机会  
  16.         if(orderedChildren.size() == 0){  
  17.             childWatcher.await();  
  18.             continue;  
  19.         }  
  20.   
  21.         for(String headNode : orderedChildren.values()){  
  22.             String path = dir +"/"+headNode;  
  23.             try{  
  24.                 // 返回对列头部第一个元素  
  25.                 byte[] data = zookeeper.getData(path, falsenull);  
  26.                 // 删除该元素  
  27.                 zookeeper.delete(path, -1);  
  28.                 return data;  
  29.             }catch(KeeperException.NoNodeException e){  
  30.                 // Another client deleted the node first.  
  31.             }  
  32.         }  
  33.     }  
  34. }  

  1. private class LatchChildWatcher implements Watcher {  
  2.   
  3.     CountDownLatch latch;  
  4.   
  5.     public LatchChildWatcher(){  
  6.         latch = new CountDownLatch(1);  
  7.     }  
  8.   
  9.     public void process(WatchedEvent event){  
  10.         LOG.debug("Watcher fired on path: " + event.getPath() + " state: " +   
  11.                 event.getState() + " type " + event.getType());  
  12.         latch.countDown();  
  13.     }  
  14.     public void await() throws InterruptedException {  
  15.         latch.await();  
  16.     }  
  17. }  

4. offer  写入队列尾部  使用永久顺序节点

  1. public boolean offer(byte[] data) throws KeeperException, InterruptedException{  
  2.     for(;;){  
  3.         try{  
  4.             // 写入对列    节点类型  永久顺序节点  
  5.             zookeeper.create(dir+"/"+prefix, data, acl, CreateMode.PERSISTENT_SEQUENTIAL);  
  6.             return true;  
  7.         }catch(KeeperException.NoNodeException e){  
  8.             zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT);  
  9.         }  
  10.     }  
  11. }  

5.peek  返回对列头部  第一个znode对应的值
  1. public byte[] peek() throws KeeperException, InterruptedException{  
  2.     try{  
  3.         // 返回对列头部第一个znode的值  
  4.         return element();  
  5.     }catch(NoSuchElementException e){  
  6.         return null;  
  7.     }  
  8. }  

6. 返回队列头部第一个znode对应的值  并删除该znode
  1. public byte[] poll() throws KeeperException, InterruptedException {  
  2.     try{  
  3.         // 返回对列znode下 第一个子节点值  并删除该节点  
  4.         return remove();  
  5.     }catch(NoSuchElementException e){  
  6.         return null;  
  7.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值