- 7.11. 有界阻塞队列(Bounded Blocking Queue)
7.11. 有界阻塞队列(Bounded Blocking Queue)
基于Redis的Redisson分布式有界阻塞队列(Bounded Blocking Queue)结构的RBoundedBlockingQueue Java对象实现了java.util.concurrent.BlockingQueue接口。该对象的最大容量受Redis限制,最大元素数量是4 294 967 295个。队列的初始容量(边界)必须在使用前设定好。
RBoundedBlockingQueue<SomeObject> queue = redisson.getBoundedBlockingQueue("anyQueue");// 如果初始容量(边界)设定成功则返回`真(true)`,// 如果初始容量(边界)已近存在则返回`假(false)`。queue.trySetCapacity(2);queue.offer(new SomeObject(1));queue.offer(new SomeObject(2));// 此时容量已满,下面代码将会被阻塞,直到有空闲为止。queue.put(new SomeObject());SomeObject obj = queue.peek();SomeObject someObj = queue.poll();SomeObject ob = queue.poll(10, TimeUnit.MINUTES);
poll, pollFromAny, pollLastAndOfferFirstTo和take方法内部采用话题订阅发布实现,在Redis节点故障转移(主从切换)或断线重连以后,内置的相关话题监听器将自动完成话题的重新订阅。
