Mastering Redisson Distributed Collections in Spring Boot 2.7
This article introduces Redisson as a Redis Java client and demonstrates how to use its distributed collection implementations—RMap, RSet, RList, RQueue, and various blocking queues—in Spring Boot 2.7, covering synchronous, fast, and asynchronous operations, lock binding, local caching, and event listeners.
Environment: SpringBoot 2.7.18 + Redis 6.2.6 + Redisson 3.18.0
1. Introduction
Redisson is a Redis Java client and real‑time data platform that simplifies using Redis. It provides distributed implementations of Java collection interfaces such as RList, RSet, RMap, allowing developers to work with data across multiple JVMs as if they were local collections.
2. Practical Examples
2.1 Map Collection
Redisson’s RMap implements java.util.concurrent.ConcurrentMap and is fully thread‑safe.
public interface RMap<K, V> extends ConcurrentMap<K, V>, ... {}All operations are performed through a RedissonClient instance.
@Resource
private RedissonClient redisson;Synchronous operations
RMap<String, User> map = redisson.getMap("user-list");
User preValue = map.put("1", new User(2L, "ZhangSan2", 22));
User value = map.putIfAbsent("2", new User(2L, "LiSi", 33));Fast operations (no previous value returned)
RMap<String, User> map = redisson.getMap("user-list");
map.fastPut("1", new User(2L, "ZhangSan2", 22));
map.fastPutIfAbsent("2", new User(2L, "LiSi", 33));
map.fastRemove("1");Asynchronous operations
RFuture<User> f1 = map.putAsync("1", new User(2L, "ZhangSan2", 22));
RFuture<Boolean> f2 = map.fastPutAsync("2", new User(2L, "LiSi", 33));
RFuture<Long> f3 = map.fastRemoveAsync("2");Each operation maps to a Redis HASH structure.
2.2 Set Collection
RSet implements java.util.Set, guaranteeing element uniqueness and supporting up to 4,294,967,295 elements.
public interface RSet<V> extends Set<V>, ... {} RSet<User> set = redisson.getSet("user-set");
set.add(new User(1L, "ZhangSan", 33));
set.add(new User(2L, "LiSi", 55));Underlying Redis structure is a SET.
RSet also supports asynchronous methods and can bind a lock to a specific element.
RSet<User> set = redisson.getSet("user-set");
RLock lock = set.getLock(new User(1L, "ZhangSan", 33));
lock.lock();
try {
// business logic
} finally {
lock.unlock();
}2.3 List Collection
RList implements java.util.List and preserves insertion order.
public interface RList<V> extends List<V>, ... {} RList<User> list = redisson.getList("user-list");
User user = new User(1L, "ZhangSan", 10);
list.add(user);
User ret = list.get(0);
list.remove(user);Listeners can be added for events such as put, remove, or expiration.
list.addListener(new ExpiredObjectListener() {
@Override
public void onExpired(String name) {
// handle expiration
}
});2.4 Queue
RQueue implements java.util.Queue and is fully thread‑safe.
public interface RQueue<V> extends Queue<V>, ... {} RQueue<User> queue = redisson.getQueue("user-queue");
queue.add(new User());
User u1 = queue.peek(); // read without removing
User u2 = queue.poll(); // read and removeCorresponding Redis structure is a LIST.
2.5 BlockingQueue
RBlockingQueue implements java.util.concurrent.BlockingQueue, supporting blocking take operations.
public interface RBlockingQueue<V> extends BlockingQueue<V>, ... {} RBlockingQueue<User> queue = redisson.getBlockingQueue("user-blockqueue");
queue.offer(new User(1L, "HaHa", 22));
User u1 = queue.peek();
User u2 = queue.poll();
User u3 = queue.poll(10, TimeUnit.SECONDS); // blocks up to 10 s2.6 BoundedBlockingQueue
RBoundedBlockingQueue adds a capacity limit to the blocking queue.
RBoundedBlockingQueue<SomeObject> queue = redisson.getBoundedBlockingQueue("user-capacity-queue");
queue.trySetCapacity(2);
queue.offer(new User(1L, "ZhangSan", 20));
queue.offer(new User(2L, "LiSi", 10));Redisson also provides other distributed queue types such as deque and priority queue.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
