操作执行

Redisson 支持对每个操作自动重试的策略并且在每次尝试期会尝试发送命令。 重试策略由设置项 retryAttempts (默认为 3) 和 retryInterval (默认为 1000 ms) 来控制。 每次尝试会在 retryInterval 时间间隔后执行。

Redisson 实例和 Redisson 对象都是完全线程安全的。

带有同步/异步方法的 Redisson 对象可通过 RedissonClient 接口获得。 替代的带有 Reactive Streams 方法的 Redisson 对象 可通过 RedissonReactiveClient 接口获得。

以下是 RAtomicLong 对象的示例:

  1. RedissonClient client = Redisson.create(config);
  2. RAtomicLong longObject = client.getAtomicLong('myLong');
  3. // sync way
  4. longObject.compareAndSet(3, 401);
  5. // async way
  6. longObject.compareAndSetAsync(3, 401);
  7. RedissonReactiveClient client = Redisson.createReactive(config);
  8. RAtomicLongReactive longObject = client.getAtomicLong('myLong');
  9. // reactive way
  10. longObject.compareAndSet(3, 401);

异步方式

几乎每个 Redisson 对象都扩展了具有和同步方法的镜像的异步方法的一个异步接口。如:

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

异步方法返回一个扩展的具有可添加监听器的 Future 对象。 这样你可以以完全非阻塞的方式来获取结果。

  1. future.addListener(new FutureListener<Boolean>() {
  2. @Override
  3. public void operationComplete(Future<Boolean> future) throws Exception {
  4. if (future.isSuccess()) {
  5. // get result
  6. Boolean result = future.getNow();
  7. // ...
  8. } else {
  9. // an error has occurred
  10. Throwable cause = future.cause();
  11. }
  12. }
  13. });

Reactive 方式

Redisson 通过对 Java 9 的 Reactive Streams 标准来支持 Reactive 方式。 基于著名的 Reactor 项目。 针对 Java 的 Reactive 对象可通过独立的 RedissonReactiveClient 接口来获取:

  1. RedissonReactiveClient client = Redisson.createReactive(config);
  2. RAtomicLongReactive longObject = client.getAtomicLong("myLong");
  3. Publisher<Boolean> csPublisher = longObject.compareAndSet(10, 91);
  4. Publisher<Long> getPublisher = longObject.get();