10.Curator队列
- ZK有1MB 的传输限制。实践中ZNode必须相对较小,而队列包含成千上万的消息,非常的大
- 如果有很多节点,ZK启动时相当的慢。而使用queue会导致好多ZNode。你需要显著增大 initLimit 和 syncLimit
- ZNode很大的时候很难清理。Netflix不得不创建了一个专门的程序做这事
- 当很大量的包含成千上万的子节点的ZNode时,ZK的性能变得不好
- ZK的数据库完全放在内存中。大量的Queue意味着会占用很多的内存空间
1.DistributedQueue
- QueueBuilder - 创建队列使用QueueBuilder,它也是其它队列的创建类
- QueueConsumer - 队列中的消息消费者接口
- QueueSerializer - 队列消息序列化和反序列化接口,提供了对队列中的对象的序列化和反序列化
- DistributedQueue - 队列实现类
public class DistributedQueueExample{private static final String PATH = "/example/queue";public static void main(String[] args) throws Exception{CuratorFramework clientA = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));clientA.start();CuratorFramework clientB = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));clientB.start();DistributedQueue<String> queueA = null;QueueBuilder<String> builderA = QueueBuilder.builder(clientA, createQueueConsumer("A"), createQueueSerializer(), PATH);queueA = builderA.buildQueue();queueA.start();DistributedQueue<String> queueB = null;QueueBuilder<String> builderB = QueueBuilder.builder(clientB, createQueueConsumer("B"), createQueueSerializer(), PATH);queueB = builderB.buildQueue();queueB.start();for (int i = 0; i < 100; i++){queueA.put(" test-A-" + i);Thread.sleep(10);queueB.put(" test-B-" + i);}Thread.sleep(1000 * 10);// 等待消息消费完成queueB.close();queueA.close();clientB.close();clientA.close();System.out.println("OK!");}/** 队列消息序列化实现类 */private static QueueSerializer<String> createQueueSerializer(){return new QueueSerializer<String>(){@Overridepublic byte[] serialize(String item){return item.getBytes();}@Overridepublic String deserialize(byte[] bytes){return new String(bytes);}};}/** 定义队列消费者 */private static QueueConsumer<String> createQueueConsumer(final String name){return new QueueConsumer<String>(){@Overridepublic void stateChanged(CuratorFramework client, ConnectionState newState){System.out.println("连接状态改变: " + newState.name());}@Overridepublic void consumeMessage(String message) throws Exception{System.out.println("消费消息(" + name + "): " + message);}};}}
消费消息(A): test-A-0消费消息(A): test-B-0......消费消息(B): test-A-51消费消息(B): test-B-51消费消息(B): test-A-52消费消息(B): test-B-52消费消息(B): test-A-53消费消息(B): test-B-54消费消息(B): test-A-55......消费消息(A): test-A-99消费消息(A): test-B-99OK!

2.DistributedIdQueue
- 通过下面方法创建:builder.buildIdQueue()
- 放入元素时:queue.put(aMessage, messageId);
- 移除元素时:int numberRemoved = queue.remove(messageId);
public class DistributedIdQueueExample{private static final String PATH = "/example/queue";public static void main(String[] args) throws Exception{CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));client.start();DistributedIdQueue<String> queue = null;QueueConsumer<String> consumer = createQueueConsumer("A");QueueBuilder<String> builder = QueueBuilder.builder(client, consumer, createQueueSerializer(), PATH);queue = builder.buildIdQueue();queue.start();for (int i = 0; i < 10; i++){queue.put(" test-" + i, "Id" + i);Thread.sleep((long) (50 * Math.random()));queue.remove("Id" + i);}Thread.sleep(1000 * 3);queue.close();client.close();System.out.println("OK!");}......}
消费消息(A): test-2消费消息(A): test-3消费消息(A): test-4消费消息(A): test-7OK!
3.DistributedPriorityQueue
public class DistributedPriorityQueueExample{private static final String PATH = "/example/queue";public static void main(String[] args) throws Exception{CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));client.start();DistributedPriorityQueue<String> queue = null;QueueConsumer<String> consumer = createQueueConsumer("A");QueueBuilder<String> builder = QueueBuilder.builder(client, consumer, createQueueSerializer(), PATH);queue = builder.buildPriorityQueue(0);queue.start();for (int i = 0; i < 5; i++){int priority = (int) (Math.random() * 100);System.out.println("test-" + i + " 优先级:" + priority);queue.put("test-" + i, priority);Thread.sleep((long) (50 * Math.random()));}Thread.sleep(1000 * 2);queue.close();client.close();}......}
test-0 优先级:34test-1 优先级:51test-2 优先级:63test-3 优先级:45test-4 优先级:36消费消息(A): test-0消费消息(A): test-4消费消息(A): test-3消费消息(A): test-1消费消息(A): test-2OK!
4.DistributedDelayQueue
public class DistributedDelayQueueExample{private static final String PATH = "/example/queue";public static void main(String[] args) throws Exception{CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));client.start();DistributedDelayQueue<String> queue = null;QueueConsumer<String> consumer = createQueueConsumer("A");QueueBuilder<String> builder = QueueBuilder.builder(client, consumer, createQueueSerializer(), PATH);queue = builder.buildDelayQueue();queue.start();for (int i = 0; i < 10; i++){queue.put("test-" + i, System.currentTimeMillis() + 3000);}System.out.println("put 完成!");Thread.sleep(1000 * 5);queue.close();client.close();System.out.println("OK!");}......}
put 完成!消费消息(A): test-0消费消息(A): test-3消费消息(A): test-1消费消息(A): test-2消费消息(A): test-6消费消息(A): test-4消费消息(A): test-5消费消息(A): test-7消费消息(A): test-8消费消息(A): test-9OK!
5.SimpleDistributedQueue
// 创建public SimpleDistributedQueue(CuratorFramework client, String path)// 增加元素public boolean offer(byte[] data) throws Exception// 删除元素public byte[] take() throws Exception// 另外还提供了其它方法public byte[] peek() throws Exceptionpublic byte[] poll(long timeout, TimeUnit unit) throws Exceptionpublic byte[] poll() throws Exceptionpublic byte[] remove() throws Exceptionpublic byte[] element() throws Exception
-------------------------------------------------------------------------------------------------------------------------------
10.Curator队列的更多相关文章
- 10 阻塞队列 & 生产者-消费者模式
原文:http://www.cnblogs.com/dolphin0520/p/3932906.html 在前面我们接触的队列都是非阻塞队列,比如PriorityQueue.LinkedList(Li ...
- java数据结构-10循环队列
一.概念: 循环队列就是将队列存储空间的最后一个位置绕到第一个位置,形成逻辑上的环状空间,供队列循环使用 二.代码实现: @SuppressWarnings("unchecked" ...
- JUC 并发编程--10, 阻塞队列之--LinkedBlockingDeque 工作窃取, 代码演示
直接上代码 class LinkedBlockingDequeDemo { // 循环是否结束的开关 private static volatile boolean flag1 = true; pri ...
- java多线程系列10 阻塞队列模拟
接下来的几篇博客会介绍下juc包下的相关数据结构 包含queue,list,map等 这篇文章主要模拟下阻塞队列. 下面是代码 import java.util.LinkedList; import ...
- Nodejs事件引擎libuv源码剖析之:高效队列(queue)的实现
声明:本文为原创博文,转载请注明出处. 在libuv中,有一个只使用简单的宏封装成的高效队列(queue),现在我们就来看一下它是怎么实现的. 首先,看一下queue中最基本的几个宏: typede ...
- javascript数据结构与算法---队列
javascript数据结构与算法---队列 队列是一种列表,不同的是队列只能在队尾插入元素,在队首删除元素.队列用于存储按顺序排列的数据,先进先出,这点和栈不一样(后入先出).在栈中,最后入栈的元素 ...
- Java数据结构之队列的实现以及队列的应用之----简单生产者消费者应用
Java数据结构之---Queue队列 队列(简称作队,Queue)也是一种特殊的线性表,队列的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置插入和删除,而队列只允许在 ...
- java中使用队列:java.util.Queue (转)
Queue接口与List.Set同一级别,都是继承了Collection接口.LinkedList实现了Queue接 口.Queue接口窄化了对LinkedList的方法的访问权限(即在方法中的参数类 ...
- java 队列基础操作
http://www.cnblogs.com/fuck1/p/5996116.html 队列(简称作队,Queue)也是一种特殊的线性表,队列的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别 ...
随机推荐
- thinkphp 使用原生mysql语句 联合查询
<?php class DelAction extends Action { public function ml(){ // 实例化一个空模型,没有对应任何数据表 $Dao = M(); // ...
- 远程桌面能连接到服务器,但PING不通
解决方法:
- 使用ffmpeg的av_read_frame,如何控制连接超时
最近使用ffmpeg来做一个rtsp的客户端,这过程也遇到不少问题,不过相应都比较好,一路走下来.不过到项目结尾时,且遇到一个比较纠结的问题.那就是客户端在使用的过程中,把rtsp服务器的网断了.这时 ...
- nodejs基础 -- 回调函数
Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了. 回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,Node 所有 API 都 ...
- erlang二进制的难理解的地方,有点神奇
40> <<A:16>> = <<1,2>>.<<1,2>>41> <<B:16/bits>> ...
- php HTML安全过滤
/*HTML安全过滤*/ function _htmtocode($content) { $content = str_replace('%','%',$content); $content = s ...
- dos中执行cd命令切换不到对应的盘解决方法
可以使用cd命令,不过需要加参数 /d,如: cd /d e:
- Sqrt算法
转自原文:http://www.cnblogs.com/pkuoliver/archive/2010/10/06/sotry-about-sqrt.html 一个Sqrt函数引发的血案 2010-10 ...
- WinSock1.1和WinSock2.0
网络编程很重要,说到网络编程就不得不提Socket编程. Windows提供了Windows Socket API(简称WSA),WinSock,目前有两个版本:WinSock1.1 and WinS ...
- android webView不简单
手机屏幕大小非常伤程序猿 励志成为一名Javaproject师的我.真的被它伤到了,不仅由于webView的强大.并且这个内容适合各样屏幕大小问题. 想当年苹果project师嘲笑安卓project师 ...