Redisson instances are fully thread-safe. Objects with synchronous/asynchronous
methods could be reached via RedissonClient interface.Reactive
and RxJava3
methods through RedissonReactiveClient and RedissonRxClient interfaces respectively.
Redisson implements auto-retry policy per operation. Retry policy is controlled by retryAttempts and retryInterval settings. These settings are applied to each Redisson object. timeout setting is applied when the Redis command was successfully sent.
Settings above can be overridden per Redisson object instance. These settings apply to each method of a given Redisson object instance.
Here is an example with RBucket
object:
RedissonClient client = Redisson.create(config);
RBucket<MyObject> bucket = client.getBucket('myObject');
// sync way
bucket.get();
// async way
RFuture<MyObject> result = bucket.getAsync();
// instance with overridden retryInterval and timeout parameters
RBucket<MyObject> bucket = client.getBucket(PlainOptions.name('myObject')
.timeout(Duration.ofSeconds(3))
.retryInterval(Duration.ofSeconds(5)));
3.1. Async way
Most Redisson objects extend asynchronous interface with asynchronous methods which mirror to synchronous methods. Like this:
// RAtomicLong extends RAtomicLongAsync
RAtomicLongAsync longObject = client.getAtomicLong("myLong");
RFuture<Boolean> future = longObject.compareAndSetAsync(1, 401);
Asynchronous methods return RFuture object which extends CompletionStage interface.
future.whenComplete((res, exception) -> {
// handle both result and exception
});
// or
future.thenAccept(res -> {
// handle result
}).exceptionally(exception -> {
// handle exception
});
Avoid to use blocking methods in future listeners. Listeners executed by netty-threads and delays in listeners may cause errors in Redis request/response processing. Use follow methods to execute blocking methods in listeners:
future.whenCompleteAsync((res, exception) -> {
// handle both result and exception
}, executor);
// or
future.thenAcceptAsync(res -> {
// handle result
}, executor).exceptionallyAsync(exception -> {
// handle exception
}, executor);
3.2. Reactive way
Redisson exposes Reactive Streams API for most objects and based on two implementations:
- Project Reactor based implementation. Code example: ```java RedissonReactiveClient client = redissonClient.reactive();
RAtomicLongReactive atomicLong = client.getAtomicLong(“myLong”);
Mono
get.doOnSuccess(res -> { // … }).subscribe();
2. [RxJava3](https://github.com/ReactiveX/RxJava) based implementation. Code example:
```java
RedissonRxClient client = redissonClient.rxJava();
RAtomicLongRx atomicLong = client.getAtomicLong("myLong");
Single<Boolean> cs = longObject.compareAndSet(10, 91);
Single<Long> get = longObject.get();
get.doOnSuccess(res -> {
// ...
}).subscribe();