reactor-lock

This library provides the way to perform any computations wrapped with reactor.core.publisher.Mono with exclusive locking in the reactive manner.

License

License

Categories

Categories

React User Interface Web Frameworks Reactor Container Microservices Reactive libraries
GroupId

GroupId

com.github.alex-pumpkin
ArtifactId

ArtifactId

reactor-lock-core
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

reactor-lock
This library provides the way to perform any computations wrapped with reactor.core.publisher.Mono with exclusive locking in the reactive manner.
Project URL

Project URL

https://github.com/alex-pumpkin/reactor-lock
Source Code Management

Source Code Management

http://github.com/alex-pumpkin/reactor-lock/tree/master

Download reactor-lock-core

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.alex-pumpkin/reactor-lock-core/ -->
<dependency>
    <groupId>com.github.alex-pumpkin</groupId>
    <artifactId>reactor-lock-core</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.alex-pumpkin/reactor-lock-core/
implementation 'com.github.alex-pumpkin:reactor-lock-core:0.2.0'
// https://jarcasting.com/artifacts/com.github.alex-pumpkin/reactor-lock-core/
implementation ("com.github.alex-pumpkin:reactor-lock-core:0.2.0")
'com.github.alex-pumpkin:reactor-lock-core:jar:0.2.0'
<dependency org="com.github.alex-pumpkin" name="reactor-lock-core" rev="0.2.0">
  <artifact name="reactor-lock-core" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.alex-pumpkin', module='reactor-lock-core', version='0.2.0')
)
libraryDependencies += "com.github.alex-pumpkin" % "reactor-lock-core" % "0.2.0"
[com.github.alex-pumpkin/reactor-lock-core "0.2.0"]

Dependencies

runtime (3)

Group / Artifact Type Version
io.projectreactor : reactor-core jar 3.4.5
io.projectreactor.addons : reactor-extra jar 3.4.1
io.vavr : vavr jar 0.10.0

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.awaitility : awaitility jar 3.1.6

Project Modules

There are no modules declared in this project.

Exclusive locking for Mono and CacheMono in the reactive style

This library provides the way to perform any computations wrapped with Mono with exclusive locking in the reactive manner.

In most cases you should avoid to use it. But if you have the requirement to minimize concurrent calls of some services with or without caching, reactor-lock may be useful.

Usage

Dependencies

Maven

<dependency>
  <groupId>com.github.alex-pumpkin</groupId>
  <artifactId>reactor-lock-core</artifactId>
  <version>0.2.0</version>
</dependency>

Gradle

implementation 'com.github.alex-pumpkin:reactor-lock-core:0.2.0'

Exclusive subscription by key

public <K, V> Mono<V> callServiceByKeyWithLock(K key) {
    return LockMono.key(key)
            .lock(callServiceByKey(key));
}

private <K, V> Mono<V> callServiceByKey(K key) {
    return Mono.fromCallable(() -> {...});
}

Caching with exclusive subscription to the original Mono

Cache doesn’t implement Map interface

public <K, V> Mono<V> callServiceByKeyWithCache(K key) {
    return LockCacheMono.create(LockMono.key(key).build())
            .lookup(k -> Mono.justOrEmpty(CACHE.get(k)).map(Signal::next))
            .onCacheMissResume(() -> callServiceByKey(key))
            .andWriteWith((k, signal) -> Mono.fromRunnable(() -> Optional.ofNullable(signal.get())
                    .ifPresent(v -> CACHE.put(k, v))));
}

private <K, V> Mono<V> callServiceByKey(K key) {
    return Mono.fromCallable(() -> {...});
}

Cache implements Map interface

Map<K, Signal<V>> MAP_CACHE = ...;

public <K, V> Mono<V> callServiceByKeyWithCache(K key) {
    return LockCacheMono.create(LockMono.key(key).build())
            .lookup(MAP_CACHE)
            .onCacheMissResume(() -> callServiceByKey(key));
}

private <K, V> Mono<V> callServiceByKey(K key) {
    return Mono.fromCallable(() -> {...});
}

License

Copyright 2019 Alexander Pankin

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Versions

Version
0.2.0
0.1.0