3. Operations execution

Nikita Koksharov edited this page Oct 18, 2016 · 11 revisions

Redisson supports auto-retry policy for each operation and try 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 instance and Redisson objects are fully-thread safe.

Redisson object with synchronous/asynchronous methods could be reached via RedissonClient interface. Alternative Redisson object with Reactive Streams approach could be reached via RedissonReactiveClient interface.

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
longObject.compareAndSetAsync(3, 401);

RedissonReactiveClient client = Redisson.createReactive(config);
RAtomicLongReactive longObject = client.getAtomicLong('myLong');
// reactive way
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 with ability to add listeners to it. Thus you can get results in fully non-blocking way.

// JDK 1.8+ compatible
future.handle((conn, exception) -> {
    // ...
});
// JDK 1.6+ compatible
future.addListener(new FutureListener<Boolean>() {
    @Override
    public void operationComplete(Future<Boolean> future) throws Exception {
         if (future.isSuccess()) {
            // get result
            Boolean result = future.getNow();
            // ...
         } else {
            // an error has occurred
            Throwable cause = future.cause();
         }
    }
});

3.2. Reactive way

Redisson supports Reactive approach via new Reactive Streams Standard for Java 9. Based on famous Reactor project. Reactive objects for Java are available through RedissonReactiveClient interface:

RedissonReactiveClient client = Redisson.createReactive(config);
RAtomicLongReactive longObject = client.getAtomicLong("myLong");

Publisher<Boolean> csPublisher = longObject.compareAndSet(10, 91);

Publisher<Long> getPublisher = longObject.get();