Class ReactiveLatch
java.lang.Object
dev.sympho.modular_commands.utils.ReactiveLatch
public class ReactiveLatch
extends Object
implements Function<Object,org.reactivestreams.Publisher<Void>>
An equivalent to Java's
CountDownLatch that uses Reactive Streams for non-blocking
waiting. It can be used by either directly subscribing to await() or by using an
instance of this class with Mono.delayUntil(Function) (or the Flux equivalent).
The latch can also be failed, propagating an error to those waiting on it instead of a completion.
- Since:
- 1.0
- Version:
- 1.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new latch that opens aftercountDown()is called once.ReactiveLatch(long needed) Creates a new latch that opens after counting down a given number of times. -
Method Summary
Modifier and TypeMethodDescriptionorg.reactivestreams.Publisher<Void>Creates a publisher that completes once this latch opens.await()Creates a Mono that emits a completion signal when this latch enters the open state.voidCounts down the latch.voidFails the latch, issuing an error signal to those waiting on it.
-
Constructor Details
-
ReactiveLatch
Creates a new latch that opens after counting down a given number of times.- Parameters:
needed- How many timescountDown()needs to be called before the latch opens.- Throws:
IllegalArgumentException- if the given value is not positive (>0).
-
ReactiveLatch
Creates a new latch that opens aftercountDown()is called once. This is effectively a binary latch or a switch.
-
-
Method Details
-
apply
Creates a publisher that completes once this latch opens. -
await
Creates a Mono that emits a completion signal when this latch enters the open state.Once the latch is open, subscribing to the returned Mono will complete instantly.
- Returns:
- The Mono to wait on.
-
countDown
Counts down the latch. If the internal counter reaches zero, the latch opens.Has no effect if the latch is already opened or failed.
- Throws:
IllegalStateException- if emitting the completion signal failed.
-
fail
Fails the latch, issuing an error signal to those waiting on it.Has no effect if the latch is already opened or failed.
- Parameters:
error- The error to issue.- Throws:
IllegalStateException- if emitting the error signal failed.
-