操作执行

Redisson 支持自动重新尝试的机制对于每一个操作,在每次尝试中重新发送命令。重新尝试通过retryAttempts (缺省是 3) and retryInterval (缺省值 1000 ms) 的配置进行控制。每一个重新尝试都会在retryInterval时间周期之后。

Redisson 对象可以通过同步或者是异步两种方式进行操作,通过使用RedissonClient 接口。 其他的Redisson对象包括Reactive Streams 方式可以通过使用RedissonReactiveClient 接口。

以下是使用RAtomicLong 对象的实例:

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. 异步方式

几乎每个Redisson对象实现了Asynchronous接口中的异步方法,这些方法模仿同步的方法名称。如下所示:

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

异步的方法返回的实现Future接口的对象拥有添加监听器到其中的能力。因此你可以完全使用无锁定的方式获取结果。

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. 反应式方法

Redisson 支持反应式的方法通过新的Reactive Streams 标准的实现基于著名的Reactor 项目。反应式对象可以通过独立的apiRedissonReactiveClient 接口进行使用 :

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

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

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

results matching ""

    No results matching ""