[ZooKeeper]纠正官网的Queue示例
来源:互联网 发布:美工军工基金指数 编辑:程序博客网 时间:2024/05/17 03:56
Queue接口
public interface Queue<E> {boolean produce(E e) throws InterruptedException, QueueException;E consume() throws QueueException, InterruptedException;}
单机版的队列用BlockingDeque或BlockingQueue就能实现
import java.util.concurrent.BlockingDeque;import java.util.concurrent.LinkedBlockingDeque;/** * 单机版的队列 * */public class StandaloneQueue<E> {private BlockingDeque<E> deque;public StandaloneQueue() {deque = new LinkedBlockingDeque<>();}public boolean produce(E e) {return deque.offerLast(e);}public E consume() throws InterruptedException {return deque.takeFirst();}}
分布式版的Queue,对于getData和delete时产生的NONODE异常可以放过
import java.io.IOException;import java.nio.ByteBuffer;import java.util.List;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.KeeperException.Code;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.data.Stat;/** * 分布式的队列 */public class DistributedQueue implements Queue<Integer>, Watcher {private String root;private String queueName;private ZooKeeper zooKeeper; private Lock lock; private Condition nodeChildrenChange; private volatile boolean expired;public DistributedQueue(String address, String root, String queueName) throws QueueException, InterruptedException {this.root = root;this.queueName = queueName;lock = new ReentrantLock(); nodeChildrenChange = lock.newCondition(); try { zooKeeper = new ZooKeeper(address, 3000, this); } catch (IOException e) { throw new QueueException(e); } try { Stat stat = zooKeeper.exists(root, false); if (stat == null) { zooKeeper.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } catch (KeeperException e) { if (e.code() != Code.NODEEXISTS) { throw new QueueException(e); } } }@Overridepublic boolean produce(Integer i) throws InterruptedException, QueueException {ByteBuffer b = ByteBuffer.allocate(4);b.putInt(i);byte[] value = b.array();try {zooKeeper.create(root + "/" + queueName, value, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);return true;} catch (KeeperException e) {throw new QueueException(e);}}@Overridepublic Integer consume() throws QueueException, InterruptedException {while (!Thread.interrupted() && !expired) {List<String> list;try {list = zooKeeper.getChildren(root, true);} catch (KeeperException e) {throw new QueueException(e);}if (list.isEmpty()) {lock.lock(); try { nodeChildrenChange.await(); } finally { lock.unlock(); }} else {Integer min = new Integer(list.get(0).substring(7)); for(String s : list){ Integer tempValue = new Integer(s.substring(7)); if(tempValue < min) min = tempValue; } String path = root + "/" + queueName + min; try {byte[] value = zooKeeper.getData(path, false, null);zooKeeper.delete(path, 0);ByteBuffer buffer = ByteBuffer.wrap(value);return buffer.getInt();} catch (KeeperException e) {if (e.code() != Code.NONODE) {throw new QueueException(e);}// 数据已经被别人取走}}}throw new QueueException("interruped or expired");}@Overridepublic void process(WatchedEvent event) {if (event.getType() == Watcher.Event.EventType.None) { if (event.getState() == Watcher.Event.KeeperState.Expired) { expired = true; try { zooKeeper.close(); } catch (InterruptedException e) { // do nothing; } } } else if (event.getType() == Watcher.Event.EventType.NodeChildrenChanged) { lock.lock(); try { nodeChildrenChange.signalAll(); } finally { lock.unlock(); } }}}
0 0
- [ZooKeeper]纠正官网的Queue示例
- [ZooKeeper]纠正官网的Barrier的示例
- 基于zookeeper的分布式Queue
- ZooKeeper的示例代码
- zookeeper的barriers和queue简单案例
- zookeeper的barriers和queue简单案例
- ZooKeeper 基本的操作示例
- activemq中queue的简单负载示例
- 用自己的话理解理解zookeeper(自认为纠正了网上的一些说法)
- Queue的使用示例 与 Stack的使用示例
- ZooKeeper示例
- zookeeper和dubbo的结合示例
- 煮粥放碱的纠正
- 关于px的纠正
- 算法导论示例-Queue
- IPC-Message-queue示例
- ZooKeeper实现分布式队列Queue
- ZooKeeper实现分布式队列Queue
- weblogic 与jms事例配置
- ExtJs6 checkboxgroup组件根据数据库数据默认选中设置方法
- early_printk使用方法
- 写在前面
- 析构函数virtual与非virtual的区别
- [ZooKeeper]纠正官网的Queue示例
- IntelliJ IDEA15如何配置tomcat
- Linux常用命令:more命令
- 第十三周【项目3-形状类族的中的纯虚函数】
- Django笔记 使用编辑器django-ckeditor
- Android 滤镜推流
- 史上最全的maven pom.xml文件教程详解
- 常量指针和指针常量
- windows bat脚本编写