4 ms·
I have tested a lot of async frameworks in a lot of languages and I like ReactiveX for Java (RxJava, Reactor) the most. // process1 does nothing and takes
by lmilcin 5y ago
I have tested a lot of async frameworks in a lot of languages and I like ReactiveX for Java (RxJava, Reactor) the most.
// process1 does nothing and takes 20s to complete
// if cancelled, it will print a message
private Mono<Void> process1() {
return Mono
.just(new Object())
.delayElement(Duration.ofSeconds(20))
.then()
.doOnCancel(() -> log.info("I have just been canceled"));
}
// assume it does the same as process1
private Mono<Void> process2() {
...
}
private Mono<Object> cancellationSignal() {
return Mono
.just(new Object())
.delayElement(Duration.ofSeconds(10))
.doOnNext(n -> log.info("oops, sending cancellation"));
}
// executes process1, then process2
// if cancellationSignal happens before they finish they will be safely cancelled, you will only receive message from process1
private Mono<Void> compositeSequential() {
return process1()
.then(process2())
// timeout() is just one way to cause cancellation (in this case on an arbitrary signal)
.timeout(cancellationSignal());
}
// executes both processes concurrently
// you will receive messages from both processes as they are both getting the cancellation
private Mono<Void> compositeConcurrent() {
return Flux
.just(process1(), process2())
.flatMap(identity())
.then()
.timeout(cancellationSignal());
}
- azth 5y agoCheck out structured concurrency proposal in Loom: * https://cr.openjdk.java.net/~rpressler/loom/loom/sol1_part2.html https://cr.openjdk.java.net/~rpressler/loom/loom/sol1_part2.... * https://wiki.openjdk.java.net/display/loom/Getting+started https://wiki.openjdk.java.net/display/loom/Getting+started * https://wiki.openjdk.java.net/display/loom/Structured+Concurrency https://wiki.openjdk.java.net/display/loom/Structured+Concur...
- vips7L 5y agoHaving used Rx extensively in a desktop app, I’m very looking forward to reactive programming dying. It’s done nothing but add technical debt and increase maintenance costs at this point.
- deleted 5y ago[deleted]