廊坊专业做网站,企业大全企业名录,网络策划方案,群晖wordpress7.1. 映射#xff08;Map#xff09;
基于Redis的Redisson的分布式映射结构的RMap Java对象实现了java.util.concurrent.ConcurrentMap接口和java.util.Map接口。与HashMap不同的是#xff0c;RMap保持了元素的插入顺序。该对象的最大容量受Redis限制#xff0c;最大元素数…7.1. 映射Map
基于Redis的Redisson的分布式映射结构的RMap Java对象实现了java.util.concurrent.ConcurrentMap接口和java.util.Map接口。与HashMap不同的是RMap保持了元素的插入顺序。该对象的最大容量受Redis限制最大元素数量是4 294 967 295个。
除了同步接口外还提供了异步Async、反射式Reactive和RxJava2标准的接口。如果你想用Redis Map来保存你的POJO的话可以考虑使用分布式实时对象Live Object服务。
在特定的场景下映射缓存Map上的高度频繁的读取操作使网络通信都被视为瓶颈时可以使用Redisson提供的带有本地缓存功能的映射。
RMapString, SomeObject map redisson.getMap(anyMap);
SomeObject prevObject map.put(123, new SomeObject());
SomeObject currentObject map.putIfAbsent(323, new SomeObject());
SomeObject obj map.remove(123);map.fastPut(321, new SomeObject());
map.fastRemove(321);RFutureSomeObject putAsyncFuture map.putAsync(321);
RFutureVoid fastPutAsyncFuture map.fastPutAsync(321);map.fastPutAsync(321, new SomeObject());
map.fastRemoveAsync(321);
映射的字段锁的用法
RMapMyKey, MyValue map redisson.getMap(anyMap);
MyKey k new MyKey();
RLock keyLock map.getLock(k);
keyLock.lock();
try {MyValue v map.get(k);// 其他业务逻辑
} finally {keyLock.unlock();
}RReadWriteLock rwLock map.getReadWriteLock(k);
rwLock.readLock().lock();
try {MyValue v map.get(k);// 其他业务逻辑
} finally {keyLock.readLock().unlock();
}7.1.1. 映射Map的元素淘汰Eviction本地缓存LocalCache和数据分片Sharding
Redisson提供了一系列的映射类型的数据结构这些结构按特性主要分为三大类
元素淘汰Eviction 类 -- 带有元素淘汰Eviction机制的映射类允许针对一个映射中每个元素单独设定 有效时间 和 最长闲置时间 。本地缓存LocalCache 类 -- 本地缓存Local Cache也叫就近缓存Near Cache。这类映射的使用主要用于在特定的场景下映射缓存MapCache上的高度频繁的读取操作使网络通信都被视为瓶颈的情况。Redisson与Redis通信的同时还将部分数据保存在本地内存里。这样的设计的好处是它能将读取速度提高最多 45倍 。 所有同名的本地缓存共用一个订阅发布话题所有更新和过期消息都将通过该话题共享。数据分片Sharding 类 -- 数据分片Sharding类仅适用于Redis集群环境下因此带有数据分片Sharding功能的映射也叫集群分布式映射。它利用分库的原理将单一一个映射结构切分为若干个小的映射并均匀的分布在集群中的各个槽里。这样的设计能使一个单一映射结构突破Redis自身的容量限制让其容量随集群的扩大而增长。在扩容的同时还能够使读写性能和元素淘汰处理能力随之成线性增长。
以下列表是Redisson提供的所有映射的名称及其特性 接口名称 中文名称 RedissonClient 对应的构造方法 本地缓存功能 Local Cache 数据分片功能 Sharding 元素淘汰功能 Eviction RMap 映射 getMap() No No No RMapCache 映射缓存 getMapCache() No No Yes RLocalCachedMap 本地缓存映射 getLocalCachedMap() Yes No No RLocalCachedMap Cache 本地缓存映射缓存仅限于Redisson PRO版本 getLocalCachedMapCache() Yes No Yes RClusteredMap 集群分布式映射存仅限于Redisson PRO版本 getClusteredMap() No Yes No RClusteredMapCache 集群分布式映射缓存存仅限于Redisson PRO版本 getClusteredMapCache() No Yes Yes RClusteredLocal CachedMap 集群分布式本地缓存映射存仅限于Redisson PRO版本 getClusteredLocal CachedMap() Yes Yes No RClusteredLocal CachedMapCache 集群分布式本地缓存映射缓存存仅限于Redisson PRO版本 getClusteredLocal CachedMapCache() Yes Yes Yes
除此以外Redisson还提供了Spring Cache和JCache的实现。
元素淘汰功能Eviction
Redisson的分布式的RMapCache Java对象在基于RMap的前提下实现了针对单个元素的淘汰机制。同时仍然保留了元素的插入顺序。由于RMapCache是基于RMap实现的使它同时继承了java.util.concurrent.ConcurrentMap接口和java.util.Map接口。Redisson提供的Spring Cache整合以及JCache正是基于这样的功能来实现的。
目前的Redis自身并不支持散列Hash当中的元素淘汰因此所有过期元素都是通过org.redisson.EvictionScheduler实例来实现定期清理的。为了保证资源的有效利用每次运行最多清理300个过期元素。任务的启动时间将根据上次实际清理数量自动调整间隔时间趋于1秒到1小时之间。比如该次清理时删除了300条元素那么下次执行清理的时间将在1秒以后最小间隔时间。一旦该次清理数量少于上次清理数量时间间隔将增加1.5倍。
RMapCacheString, SomeObject map redisson.getMapCache(anyMap);
// 有效时间 ttl 10分钟
map.put(key1, new SomeObject(), 10, TimeUnit.MINUTES);
// 有效时间 ttl 10分钟, 最长闲置时间 maxIdleTime 10秒钟
map.put(key1, new SomeObject(), 10, TimeUnit.MINUTES, 10, TimeUnit.SECONDS);// 有效时间 3 秒钟
map.putIfAbsent(key2, new SomeObject(), 3, TimeUnit.SECONDS);
// 有效时间 ttl 40秒钟, 最长闲置时间 maxIdleTime 10秒钟
map.putIfAbsent(key2, new SomeObject(), 40, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
本地缓存功能Local Cache
在特定的场景下映射Map上的高度频繁的读取操作使网络通信都被视为瓶颈时使用Redisson提供的带有本地缓存功能的分布式本地缓存映射RLocalCachedMapJava对象会是一个很好的选择。它同时实现了java.util.concurrent.ConcurrentMap和java.util.Map两个接口。本地缓存功能充分的利用了JVM的自身内存空间对部分常用的元素实行就地缓存这样的设计让读取操作的性能较分布式映射相比提高最多 45倍 。以下配置参数可以用来创建这个实例
LocalCachedMapOptions options LocalCachedMapOptions.defaults()// 用于淘汰清除本地缓存内的元素// 共有以下几种选择:// LFU - 统计元素的使用频率淘汰用得最少最不常用的。// LRU - 按元素使用时间排序比较淘汰最早最久远的。// SOFT - 元素用Java的WeakReference来保存缓存元素通过GC过程清除。// WEAK - 元素用Java的SoftReference来保存, 缓存元素通过GC过程清除。// NONE - 永不淘汰清除缓存元素。.evictionPolicy(EvictionPolicy.NONE)// 如果缓存容量值为0表示不限制本地缓存容量大小.cacheSize(1000)// 以下选项适用于断线原因造成了未收到本地缓存更新消息的情况。// 断线重连的策略有以下几种// CLEAR - 如果断线一段时间以后则在重新建立连接以后清空本地缓存// LOAD - 在服务端保存一份10分钟的作废日志// 如果10分钟内重新建立连接则按照作废日志内的记录清空本地缓存的元素// 如果断线时间超过了这个时间则将清空本地缓存中所有的内容// NONE - 默认值。断线重连时不做处理。.reconnectionStrategy(ReconnectionStrategy.NONE)// 以下选项适用于不同本地缓存之间相互保持同步的情况// 缓存同步策略有以下几种// INVALIDATE - 默认值。当本地缓存映射的某条元素发生变动时同时驱逐所有相同本地缓存映射内的该元素// UPDATE - 当本地缓存映射的某条元素发生变动时同时更新所有相同本地缓存映射内的该元素// NONE - 不做任何同步处理.syncStrategy(SyncStrategy.INVALIDATE)// 每个Map本地缓存里元素的有效时间默认毫秒为单位.timeToLive(10000)// 或者.timeToLive(10, TimeUnit.SECONDS)// 每个Map本地缓存里元素的最长闲置时间默认毫秒为单位.maxIdle(10000)// 或者.maxIdle(10, TimeUnit.SECONDS);
RLocalCachedMapString, Integer map redisson.getLocalCachedMap(test, options);String prevObject map.put(123, 1);
String currentObject map.putIfAbsent(323, 2);
String obj map.remove(123);// 在不需要旧值的情况下可以使用fast为前缀的类似方法
map.fastPut(a, 1);
map.fastPutIfAbsent(d, 32);
map.fastRemove(b);RFutureString putAsyncFuture map.putAsync(321);
RFutureVoid fastPutAsyncFuture map.fastPutAsync(321);map.fastPutAsync(321, new SomeObject());
map.fastRemoveAsync(321);
当不再使用Map本地缓存对象的时候应该手动销毁如果Redisson对象被关闭shutdown了则不用手动销毁。
RLocalCachedMapString, Integer map ...
map.destroy();
如何通过加载数据的方式来降低过期淘汰事件发布信息对网络的影响
代码范例:
public void loadData(String cacheName, MapString, String data) {RLocalCachedMapString, String clearMap redisson.getLocalCachedMap(cacheName, LocalCachedMapOptions.defaults().cacheSize(1).syncStrategy(SyncStrategy.INVALIDATE));RLocalCachedMapString, String loadMap redisson.getLocalCachedMap(cacheName, LocalCachedMapOptions.defaults().cacheSize(1).syncStrategy(SyncStrategy.NONE));loadMap.putAll(data);clearMap.clearLocalCache();
}
数据分片功能Sharding
Map数据分片是Redis集群模式下的一个功能。Redisson提供的分布式集群映射RClusteredMap Java对象也是基于RMap实现的。它同时实现了java.util.concurrent.ConcurrentMap和java.util.Map两个接口。在这里可以获取更多的内部信息。
RClusteredMapString, SomeObject map redisson.getClusteredMap(anyMap);SomeObject prevObject map.put(123, new SomeObject());
SomeObject currentObject map.putIfAbsent(323, new SomeObject());
SomeObject obj map.remove(123);map.fastPut(321, new SomeObject());
map.fastRemove(321);
7.1.2. 映射持久化方式缓存策略
Redisson供了将映射中的数据持久化到外部储存服务的功能。主要场景有一下几种
将Redisson的分布式映射类型作为业务和外部储存媒介之间的缓存。或是用来增加Redisson映射类型中数据的持久性或是用来增加已被驱逐的数据的寿命。或是用来缓存数据库Web服务或其他数据源的数据。
Read-through策略
通俗的讲如果一个被请求的数据不存在于Redisson的映射中的时候Redisson将通过预先配置好的MapLoader对象加载数据。
Write-through数据同步写入策略
在遇到映射中某条数据被更改时Redisson会首先通过预先配置好的MapWriter对象写入到外部储存系统然后再更新Redis内的数据。
Write-behind数据异步写入策略
对映射的数据的更改会首先写入到Redis然后再使用异步的方式通过MapWriter对象写入到外部储存系统。在并发环境下可以通过writeBehindThreads参数来控制写入线程的数量已达到对外部储存系统写入并发量的控制。
以上策略适用于所有实现了RMap、RMapCache、RLocalCachedMap和RLocalCachedMapCache接口的对象。
配置范例
MapOptionsK, V options MapOptions.K, Vdefaults().writer(myWriter).loader(myLoader);RMapK, V map redisson.getMap(test, options);
// 或
RMapCacheK, V map redisson.getMapCache(test, options);
// 或
RLocalCachedMapK, V map redisson.getLocalCachedMap(test, options);
// 或
RLocalCachedMapCacheK, V map redisson.getLocalCachedMapCache(test, options);
7.1.3. 映射监听器Map Listener
Redisson为所有实现了RMapCache或RLocalCachedMapCache接口的对象提供了监听以下事件的监听器
事件 | 监听器 元素 添加 事件 | org.redisson.api.map.event.EntryCreatedListener 元素 过期 事件 | org.redisson.api.map.event.EntryExpiredListener 元素 删除 事件 | org.redisson.api.map.event.EntryRemovedListener 元素 更新 事件 | org.redisson.api.map.event.EntryUpdatedListener
使用范例
RMapCacheString, Integer map redisson.getMapCache(myMap);
// 或
RLocalCachedMapCacheString, Integer map redisson.getLocalCachedMapCache(myMap, options);int updateListener map.addListener(new EntryUpdatedListenerInteger, Integer() {Overridepublic void onUpdated(EntryEventInteger, Integer event) {event.getKey(); // 字段名event.getValue() // 新值event.getOldValue() // 旧值// ...}
});int createListener map.addListener(new EntryCreatedListenerInteger, Integer() {Overridepublic void onCreated(EntryEventInteger, Integer event) {event.getKey(); // 字段名event.getValue() // 值// ...}
});int expireListener map.addListener(new EntryExpiredListenerInteger, Integer() {Overridepublic void onExpired(EntryEventInteger, Integer event) {event.getKey(); // 字段名event.getValue() // 值// ...}
});int removeListener map.addListener(new EntryRemovedListenerInteger, Integer() {Overridepublic void onRemoved(EntryEventInteger, Integer event) {event.getKey(); // 字段名event.getValue() // 值// ...}
});map.removeListener(updateListener);
map.removeListener(createListener);
map.removeListener(expireListener);
map.removeListener(removeListener);
7.1.4. LRU有界映射
Redisson提供了基于Redis的以LRU为驱逐策略的分布式LRU有界映射对象。顾名思义分布式LRU有界映射允许通过对其中元素按使用时间排序处理的方式主动移除超过规定容量限制的元素。
RMapCacheString, String map redisson.getMapCache(map);
// 尝试将该映射的最大容量限制设定为10
map.trySetMaxSize(10);// 将该映射的最大容量限制设定或更改为10
map.setMaxSize(10);map.put(1, 2);
map.put(3, 3, 1, TimeUnit.SECONDS);
7.2. 多值映射Multimap
基于Redis的Redisson的分布式RMultimap Java对象允许Map中的一个字段值包含多个元素。 字段总数受Redis限制每个Multimap最多允许有4 294 967 295个不同字段。
7.2.1. 基于集Set的多值映射Multimap
基于Set的Multimap不允许一个字段值包含有重复的元素。
RSetMultimapSimpleKey, SimpleValue map redisson.getSetMultimap(myMultimap);
map.put(new SimpleKey(0), new SimpleValue(1));
map.put(new SimpleKey(0), new SimpleValue(2));
map.put(new SimpleKey(3), new SimpleValue(4));SetSimpleValue allValues map.get(new SimpleKey(0));ListSimpleValue newValues Arrays.asList(new SimpleValue(7), new SimpleValue(6), new SimpleValue(5));
SetSimpleValue oldValues map.replaceValues(new SimpleKey(0), newValues);SetSimpleValue removedValues map.removeAll(new SimpleKey(0));
7.2.2. 基于列表List的多值映射Multimap
基于List的Multimap在保持插入顺序的同时允许一个字段下包含重复的元素。
RListMultimapSimpleKey, SimpleValue map redisson.getListMultimap(test1);
map.put(new SimpleKey(0), new SimpleValue(1));
map.put(new SimpleKey(0), new SimpleValue(2));
map.put(new SimpleKey(0), new SimpleValue(1));
map.put(new SimpleKey(3), new SimpleValue(4));ListSimpleValue allValues map.get(new SimpleKey(0));CollectionSimpleValue newValues Arrays.asList(new SimpleValue(7), new SimpleValue(6), new SimpleValue(5));
ListSimpleValue oldValues map.replaceValues(new SimpleKey(0), newValues);ListSimpleValue removedValues map.removeAll(new SimpleKey(0));
7.2.3. 多值映射Multimap淘汰机制Eviction
Multimap对象的淘汰机制是通过不同的接口来实现的。它们是RSetMultimapCache接口和RListMultimapCache接口分别对应的是Set和List的Multimaps。
所有过期元素都是通过org.redisson.EvictionScheduler实例来实现定期清理的。为了保证资源的有效利用每次运行最多清理100个过期元素。任务的启动时间将根据上次实际清理数量自动调整间隔时间趋于1秒到2小时之间。比如该次清理时删除了100条元素那么下次执行清理的时间将在1秒以后最小间隔时间。一旦该次清理数量少于上次清理数量时间间隔将增加1.5倍。
RSetMultimapCache的使用范例
RSetMultimapCacheString, String multimap redisson.getSetMultimapCache(myMultimap);
multimap.put(1, a);
multimap.put(1, b);
multimap.put(1, c);multimap.put(2, e);
multimap.put(2, f);multimap.expireKey(2, 10, TimeUnit.MINUTES);
7.3. 集Set
基于Redis的Redisson的分布式Set结构的RSet Java对象实现了java.util.Set接口。通过元素的相互状态比较保证了每个元素的唯一性。该对象的最大容量受Redis限制最大元素数量是4 294 967 295个。
RSetSomeObject set redisson.getSet(anySet);
set.add(new SomeObject());
set.remove(new SomeObject());
Redisson PRO版本中的Set对象还可以在Redis集群环境下支持单集合数据分片。
7.3.1. 集Set淘汰机制Eviction
基于Redis的Redisson的分布式RSetCache Java对象在基于RSet的前提下实现了针对单个元素的淘汰机制。由于RSetCache是基于RSet实现的使它还集成了java.util.Set接口。
目前的Redis自身并不支持Set当中的元素淘汰因此所有过期元素都是通过org.redisson.EvictionScheduler实例来实现定期清理的。为了保证资源的有效利用每次运行最多清理100个过期元素。任务的启动时间将根据上次实际清理数量自动调整间隔时间趋于1秒到2小时之间。比如该次清理时删除了100条元素那么下次执行清理的时间将在1秒以后最小间隔时间。一旦该次清理数量少于上次清理数量时间间隔将增加1.5倍。
RSetCacheSomeObject set redisson.getSetCache(anySet);
// ttl 10 seconds
set.add(new SomeObject(), 10, TimeUnit.SECONDS);
7.3.2. 集Set数据分片Sharding
Set数据分片是Redis集群模式下的一个功能。Redisson提供的分布式RClusteredSet Java对象也是基于RSet实现的。在这里可以获取更多的信息。
RClusteredSetSomeObject set redisson.getClusteredSet(anySet);
set.add(new SomeObject());
set.remove(new SomeObject());
除了RClusteredSet以外Redisson还提供了另一种集群模式下的分布式集Set它不仅提供了透明的数据分片功能还为每个元素提供了淘汰机制。RClusteredSetCache 类分别同时提供了RClusteredSet 和RSetCache 这两个接口的实现。当然这些都是基于java.util.Set的接口实现上的。
该功能仅限于Redisson PRO版本。
7.4. 有序集SortedSet
基于Redis的Redisson的分布式RSortedSet Java对象实现了java.util.SortedSet接口。在保证元素唯一性的前提下通过比较器Comparator接口实现了对元素的排序。
RSortedSetInteger set redisson.getSortedSet(anySet);
set.trySetComparator(new MyComparator()); // 配置元素比较器
set.add(3);
set.add(1);
set.add(2);set.removeAsync(0);
set.addAsync(5);
7.5. 计分排序集ScoredSortedSet
基于Redis的Redisson的分布式RScoredSortedSet Java对象是一个可以按插入时指定的元素评分排序的集合。它同时还保证了元素的唯一性。
RScoredSortedSetSomeObject set redisson.getScoredSortedSet(simple);set.add(0.13, new SomeObject(a, b));
set.addAsync(0.251, new SomeObject(c, d));
set.add(0.302, new SomeObject(g, d));set.pollFirst();
set.pollLast();int index set.rank(new SomeObject(g, d)); // 获取元素在集合中的位置
Double score set.getScore(new SomeObject(g, d)); // 获取元素的评分
7.6. 字典排序集LexSortedSet
基于Redis的Redisson的分布式RLexSortedSet Java对象在实现了java.util.SetString接口的同时将其中的所有字符串元素按照字典顺序排列。它公式还保证了字符串元素的唯一性。
RLexSortedSet set redisson.getLexSortedSet(simple);
set.add(d);
set.addAsync(e);
set.add(f);set.lexRangeTail(d, false);
set.lexCountHead(e);
set.lexRange(d, true, z, false);
7.7. 列表List
基于Redis的Redisson分布式列表List结构的RList Java对象在实现了java.util.List接口的同时确保了元素插入时的顺序。该对象的最大容量受Redis限制最大元素数量是4 294 967 295个。
RListSomeObject list redisson.getList(anyList);
list.add(new SomeObject());
list.get(0);
list.remove(new SomeObject());
7.8. 队列Queue
基于Redis的Redisson分布式无界队列Queue结构的RQueue Java对象实现了java.util.Queue接口。尽管RQueue对象无初始大小边界限制但对象的最大容量受Redis限制最大元素数量是4 294 967 295个。
RQueueSomeObject queue redisson.getQueue(anyQueue);
queue.add(new SomeObject());
SomeObject obj queue.peek();
SomeObject someObj queue.poll();
7.9. 双端队列Deque
基于Redis的Redisson分布式无界双端队列Deque结构的RDeque Java对象实现了java.util.Deque接口。尽管RDeque对象无初始大小边界限制但对象的最大容量受Redis限制最大元素数量是4 294 967 295个。
RDequeSomeObject queue redisson.getDeque(anyDeque);
queue.addFirst(new SomeObject());
queue.addLast(new SomeObject());
SomeObject obj queue.removeFirst();
SomeObject someObj queue.removeLast();
7.10. 阻塞队列Blocking Queue
基于Redis的Redisson分布式无界阻塞队列Blocking Queue结构的RBlockingQueue Java对象实现了java.util.concurrent.BlockingQueue接口。尽管RBlockingQueue对象无初始大小边界限制但对象的最大容量受Redis限制最大元素数量是4 294 967 295个。
RBlockingQueueSomeObject queue redisson.getBlockingQueue(anyQueue);
queue.offer(new SomeObject());SomeObject obj queue.peek();
SomeObject someObj queue.poll();
SomeObject ob queue.poll(10, TimeUnit.MINUTES);
poll, pollFromAny, pollLastAndOfferFirstTo和take方法内部采用话题订阅发布实现在Redis节点故障转移主从切换或断线重连以后内置的相关话题监听器将自动完成话题的重新订阅。
7.11. 有界阻塞队列Bounded Blocking Queue
基于Redis的Redisson分布式有界阻塞队列Bounded Blocking Queue结构的RBoundedBlockingQueue Java对象实现了java.util.concurrent.BlockingQueue接口。该对象的最大容量受Redis限制最大元素数量是4 294 967 295个。队列的初始容量边界必须在使用前设定好。
RBoundedBlockingQueueSomeObject 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节点故障转移主从切换或断线重连以后内置的相关话题监听器将自动完成话题的重新订阅。
7.12. 阻塞双端队列Blocking Deque
基于Redis的Redisson分布式无界阻塞双端队列Blocking Deque结构的RBlockingDeque Java对象实现了java.util.concurrent.BlockingDeque接口。尽管RBlockingDeque对象无初始大小边界限制但对象的最大容量受Redis限制最大元素数量是4 294 967 295个。
RBlockingDequeInteger deque redisson.getBlockingDeque(anyDeque);
deque.putFirst(1);
deque.putLast(2);
Integer firstValue queue.takeFirst();
Integer lastValue queue.takeLast();
Integer firstValue queue.pollFirst(10, TimeUnit.MINUTES);
Integer lastValue queue.pollLast(3, TimeUnit.MINUTES);
poll, pollFromAny, pollLastAndOfferFirstTo和take方法内部采用话题订阅发布实现在Redis节点故障转移主从切换或断线重连以后内置的相关话题监听器将自动完成话题的重新订阅。
7.13. 阻塞公平队列Blocking Fair Queue
基于Redis的Redisson分布式无界阻塞公平队列Blocking Fair Queue结构的RBlockingFairQueue Java对象在实现Redisson分布式无界阻塞队列Blocking Queue结构RBlockingQueue接口的基础上解决了多个队列消息的处理者在复杂的网络环境下网络延时的影响使“较远”的客户端最终收到消息数量低于“较近”的客户端的问题。从而解决了这种现象引发的个别处理节点过载的情况。
以分布式无界阻塞队列为基础采用公平获取消息的机制不仅保证了poll、pollFromAny、pollLastAndOfferFirstTo和take方法获取消息的先入顺序还能让队列里的消息被均匀的发布到处在复杂分布式环境中的各个处理节点里。
RBlockingFairQueue queue redisson.getBlockingFairQueue(myQueue);
queue.offer(new SomeObject());SomeObject obj queue.peek();
SomeObject someObj queue.poll();
SomeObject ob queue.poll(10, TimeUnit.MINUTES);
该功能仅限于Redisson PRO版本。
7.14. 阻塞公平双端队列Blocking Fair Deque
基于Redis的Redisson分布式无界阻塞公平双端队列Blocking Fair Deque结构的RBlockingFairDeque Java对象在实现Redisson分布式无界阻塞双端队列Blocking Deque结构RBlockingDeque接口的基础上解决了多个队列消息的处理者在复杂的网络环境下网络延时的影响使“较远”的客户端最终收到消息数量低于“较近”的客户端的问题。从而解决了这种现象引发的个别处理节点过载的情况。
以分布式无界阻塞双端队列为基础采用公平获取消息的机制不仅保证了poll、take、pollFirst、takeFirst、pollLast和takeLast方法获取消息的先入顺序还能让队列里的消息被均匀的发布到处在复杂分布式环境中的各个处理节点里。
RBlockingFairDeque deque redisson.getBlockingFairDeque(myDeque);
deque.offer(new SomeObject());SomeObject firstElement queue.peekFirst();
SomeObject firstElement queue.pollFirst();
SomeObject firstElement queue.pollFirst(10, TimeUnit.MINUTES);
SomeObject firstElement queue.takeFirst();SomeObject lastElement queue.peekLast();
SomeObject lastElement queue.pollLast();
SomeObject lastElement queue.pollLast(10, TimeUnit.MINUTES);
SomeObject lastElement queue.takeLast();
该功能仅限于Redisson PRO版本。
7.15. 延迟队列Delayed Queue
基于Redis的Redisson分布式延迟队列Delayed Queue结构的RDelayedQueue Java对象在实现了RQueue接口的基础上提供了向队列按要求延迟添加项目的功能。该功能可以用来实现消息传送延迟按几何增长或几何衰减的发送策略。
RQueueString distinationQueue ...
RDelayedQueueString delayedQueue getDelayedQueue(distinationQueue);
// 10秒钟以后将消息发送到指定队列
delayedQueue.offer(msg1, 10, TimeUnit.SECONDS);
// 一分钟以后将消息发送到指定队列
delayedQueue.offer(msg2, 1, TimeUnit.MINUTES);
在该对象不再需要的情况下应该主动销毁。仅在相关的Redisson对象也需要关闭的时候可以不用主动销毁。
RDelayedQueueString delayedQueue ...
delayedQueue.destroy();
7.16. 优先队列Priority Queue
基于Redis的Redisson分布式优先队列Priority QueueJava对象实现了java.util.Queue的接口。可以通过比较器Comparator接口来对元素排序。
RPriorityQueueInteger queue redisson.getPriorityQueue(anyQueue);
queue.trySetComparator(new MyComparator()); // 指定对象比较器
queue.add(3);
queue.add(1);
queue.add(2);queue.removeAsync(0);
queue.addAsync(5);queue.poll();
7.17. 优先双端队列Priority Deque
基于Redis的Redisson分布式优先双端队列Priority DequeJava对象实现了java.util.Deque的接口。可以通过比较器Comparator接口来对元素排序。
RPriorityDequeInteger queue redisson.getPriorityDeque(anyQueue);
queue.trySetComparator(new MyComparator()); // 指定对象比较器
queue.addLast(3);
queue.addFirst(1);
queue.add(2);queue.removeAsync(0);
queue.addAsync(5);queue.pollFirst();
queue.pollLast();
7.18. 优先阻塞队列Priority Blocking Queue
基于Redis的分布式无界优先阻塞队列Priority Blocking QueueJava对象的结构与java.util.concurrent.PriorityBlockingQueue类似。可以通过比较器Comparator接口来对元素排序。PriorityBlockingQueue的最大容量是4 294 967 295个元素。
RPriorityBlockingQueueInteger queue redisson.getPriorityBlockingQueue(anyQueue);
queue.trySetComparator(new MyComparator()); // 指定对象比较器
queue.add(3);
queue.add(1);
queue.add(2);queue.removeAsync(0);
queue.addAsync(5);queue.take();
当Redis服务端断线重连以后或Redis服务端发生主从切换并重新建立连接后断线时正在使用pollpollLastAndOfferFirstTo或take方法的对象Redisson将自动再次为其订阅相关的话题。
7.19. 优先阻塞双端队列Priority Blocking Deque
基于Redis的分布式无界优先阻塞双端队列Priority Blocking DequeJava对象实现了java.util.Deque的接口。addLast、 addFirst、push方法不能再这个对里使用。PriorityBlockingDeque的最大容量是4 294 967 295个元素。
RPriorityBlockingDequeInteger queue redisson.getPriorityBlockingDeque(anyQueue);
queue.trySetComparator(new MyComparator()); // 指定对象比较器
queue.add(2);queue.removeAsync(0);
queue.addAsync(5);queue.pollFirst();
queue.pollLast();
queue.takeFirst();
queue.takeLast();
当Redis服务端断线重连以后或Redis服务端发生主从切换并重新建立连接后断线时正在使用pollpollLastAndOfferFirstTo或take方法的对象Redisson将自动再次为其订阅相关的话题。
文章转载自: http://www.morning.bpmtg.cn.gov.cn.bpmtg.cn http://www.morning.jphxt.cn.gov.cn.jphxt.cn http://www.morning.jhzct.cn.gov.cn.jhzct.cn http://www.morning.zrgdd.cn.gov.cn.zrgdd.cn http://www.morning.xfyjn.cn.gov.cn.xfyjn.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.dpplr.cn.gov.cn.dpplr.cn http://www.morning.lndongguan.com.gov.cn.lndongguan.com http://www.morning.chrbp.cn.gov.cn.chrbp.cn http://www.morning.dmzzt.cn.gov.cn.dmzzt.cn http://www.morning.pdynk.cn.gov.cn.pdynk.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.dfkby.cn.gov.cn.dfkby.cn http://www.morning.jjwt.cn.gov.cn.jjwt.cn http://www.morning.rltsx.cn.gov.cn.rltsx.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.nbqwr.cn.gov.cn.nbqwr.cn http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn http://www.morning.gbsfs.com.gov.cn.gbsfs.com http://www.morning.fydsr.cn.gov.cn.fydsr.cn http://www.morning.ptlwt.cn.gov.cn.ptlwt.cn http://www.morning.blznh.cn.gov.cn.blznh.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.mytmx.cn.gov.cn.mytmx.cn http://www.morning.zlgr.cn.gov.cn.zlgr.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.xsqbx.cn.gov.cn.xsqbx.cn http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.fldrg.cn.gov.cn.fldrg.cn http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.tkcz.cn.gov.cn.tkcz.cn http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.zlces.com.gov.cn.zlces.com http://www.morning.nsjpz.cn.gov.cn.nsjpz.cn http://www.morning.pqktp.cn.gov.cn.pqktp.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.glncb.cn.gov.cn.glncb.cn http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.xhxsr.cn.gov.cn.xhxsr.cn http://www.morning.xqltq.cn.gov.cn.xqltq.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.fkmqg.cn.gov.cn.fkmqg.cn http://www.morning.gtylt.cn.gov.cn.gtylt.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.gjzwj.cn.gov.cn.gjzwj.cn http://www.morning.rqxtb.cn.gov.cn.rqxtb.cn http://www.morning.ptslx.cn.gov.cn.ptslx.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.lzwfg.cn.gov.cn.lzwfg.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.fktlr.cn.gov.cn.fktlr.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.mrckk.cn.gov.cn.mrckk.cn http://www.morning.jrlgz.cn.gov.cn.jrlgz.cn http://www.morning.dpbgw.cn.gov.cn.dpbgw.cn http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.jnrry.cn.gov.cn.jnrry.cn http://www.morning.trtdg.cn.gov.cn.trtdg.cn http://www.morning.bzlgb.cn.gov.cn.bzlgb.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.shuangxizhongxin.cn.gov.cn.shuangxizhongxin.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.tftw.cn.gov.cn.tftw.cn