返回介绍

3. Operations execution

发布于 2025-02-21 21:07:12 字数 4575 浏览 0 评论 0 收藏 0

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:

RedissonReactiveClient client = redissonClient.reactive();

RAtomicLongReactive atomicLong = client.getAtomicLong("myLong"); 
Mono cs = longObject.compareAndSet(10, 91);
Mono get = longObject.get();

get.doOnSuccess(res -> { // ... }).subscribe();

RxJava3 based implementation. Code example:

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文