Skip to content

3. Operations execution

Nikita Koksharov edited this page Mar 15, 2019 · 24 revisions

RedissonClient, RedissonReactiveClient and RedissonRxClient instances as well as Redisson objects are fully-thread safe.

Redisson supports auto-retry policy for each operation and tries to send command during each attempt. Retry policy controlled by retryAttempts (default is 3) and retryInterval (default is 1000 ms) settings. Each attempt executed after retryInterval time interval.

Redisson objects with synchronous/asynchronous methods could be reached via RedissonClient interface. Reactive and RxJava2 methods through RedissonReactiveClient and RedissonRxClient interfaces respectively.

Here is an example for RAtomicLong object:

RedissonClient client = Redisson.create(config);
RAtomicLong longObject = client.getAtomicLong('myLong');
// sync way
longObject.compareAndSet(3, 401);
// async way
RFuture<Boolean> result = longObject.compareAndSetAsync(3, 401);

RedissonReactiveClient client = Redisson.createReactive(config);
RAtomicLongReactive longObject = client.getAtomicLong('myLong');
// reactive way
Mono<Boolean> result = longObject.compareAndSet(3, 401);

RedissonRxClient client = Redisson.createRx(config);
RAtomicLongRx longObject= client.getAtomicLong("myLong");
// RxJava2 way
Flowable<Boolean result = longObject.compareAndSet(3, 401);

3.1. Async way

Almost every Redisson object extends an Asynchronous interface with asynchronous methods which mirrors synchronous methods. Like this:

// RAtomicLong extends RAtomicLongAsync
RAtomicLongAsync longObject = client.getAtomicLong("myLong");
RFuture<Boolean> future = longObject.compareAndSetAsync(1, 401);

Asynchronous method returns extended RFuture object extending both java.util.concurrent.CompletionStage and java.util.concurrent.Future interfaces.

future.whenComplete((res, exception) -> {
    // ...
});
// or
future.thenAccept(res -> {
    // handle result
}).exceptionally(exception -> {
    // handle exception
});

3.2. Reactive way

Redisson exposes Reactive Streams API for most objects and based on two implementations:

  1. Project Reactor based implementation. Code example:
RedissonReactiveClient client = Redisson.createReactive(config);

RAtomicLongReactive atomicLong = client.getAtomicLong("myLong");
Mono<Boolean> cs = longObject.compareAndSet(10, 91);
Mono<Long> get = longObject.get();
  1. RxJava2 based implementation. Code example:
RedissonRxClient client = Redisson.createRx(config);

RAtomicLongRx atomicLong = client.getAtomicLong("myLong");
Single<Boolean> cs = longObject.compareAndSet(10, 91);
Single<Long> get = longObject.get();
You can’t perform that action at this time.