Module io.helidon.common.reactive
Package io.helidon.common.reactive
Class BufferedEmittingPublisher<T>
- java.lang.Object
-
- io.helidon.common.reactive.BufferedEmittingPublisher<T>
-
- Type Parameters:
T- type of emitted item
- All Implemented Interfaces:
Flow.Publisher<T>
public class BufferedEmittingPublisher<T> extends Object implements Flow.Publisher<T>
Emitting publisher for manual publishing with built-in buffer for handling backpressure.This publisher allows only a single subscriber.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedBufferedEmittingPublisher()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intbufferSize()Estimated size of the buffer.voidclearBuffer(Consumer<T> consumer)Clear whole buffer, invoke consumer for each item before discarding it.voidcomplete()Drain the buffer, in case of not sufficient demands wait for more requests, then sendonCompletesignal to downstream.voidcompleteNow()SendonCompletesignal downstream immediately, regardless of the buffer content.static <T> BufferedEmittingPublisher<T>create()Create newBufferedEmittingPublisher.intemit(T item)Emit item to the stream, if there is no immediate demand from downstream, buffer item for sending when demand is signaled.voidfail(Throwable throwable)SendonErrorsignal downstream, regardless of the buffer content.booleanhasRequests()Check if demand is higher than 0.booleanisCancelled()Check if publisher is in terminal state CANCELLED.booleanisCompleted()Check if publisher sentonCompletesignal downstream.booleanisUnbounded()Check if downstream requested unbounded number of items eg.voidonEmit(Consumer<T> onEmitCallback)Callback executed right afteronNextis actually sent.voidonRequest(BiConsumer<Long,Long> requestCallback)Callback executed when request signal from downstream arrive.voidsubscribe(Flow.Subscriber<? super T> subscriber)
-
-
-
Method Detail
-
create
public static <T> BufferedEmittingPublisher<T> create()
Create newBufferedEmittingPublisher.- Type Parameters:
T- type of emitted item- Returns:
- new instance of BufferedEmittingPublisher
-
subscribe
public void subscribe(Flow.Subscriber<? super T> subscriber)
- Specified by:
subscribein interfaceFlow.Publisher<T>
-
onRequest
public void onRequest(BiConsumer<Long,Long> requestCallback)
Callback executed when request signal from downstream arrive.- param
nthe requested count. - param
resultthe current total cumulative requested count, ranges between [0,Long.MAX_VALUE] where the max indicates that this publisher is unbounded.
- Parameters:
requestCallback- to be executed
- param
-
onEmit
public void onEmit(Consumer<T> onEmitCallback)
Callback executed right afteronNextis actually sent.- param
isent item
- Parameters:
onEmitCallback- to be executed
- param
-
emit
public int emit(T item)
Emit item to the stream, if there is no immediate demand from downstream, buffer item for sending when demand is signaled.- Parameters:
item- to be emitted- Returns:
- actual size of the buffer, value should be used as informative and can change asynchronously
- Throws:
IllegalStateException- if cancelled, completed of failed
-
fail
public void fail(Throwable throwable)
SendonErrorsignal downstream, regardless of the buffer content. Nothing else can be sent downstream after calling fail.emit(Object)throwsIllegalStateExceptionafter calling fail.- Parameters:
throwable- Throwable to be sent downstream as onError signal.
-
complete
public void complete()
Drain the buffer, in case of not sufficient demands wait for more requests, then sendonCompletesignal to downstream.emit(Object)throwsIllegalStateExceptionafter calling complete.
-
completeNow
public void completeNow()
SendonCompletesignal downstream immediately, regardless of the buffer content. Nothing else can be sent downstream after callingcompleteNow().emit(Object)throwsIllegalStateExceptionafter calling completeNow.
-
clearBuffer
public void clearBuffer(Consumer<T> consumer)
Clear whole buffer, invoke consumer for each item before discarding it.- Parameters:
consumer- to be invoked for each item
-
isUnbounded
public boolean isUnbounded()
Check if downstream requested unbounded number of items eg.Long.MAX_VALUE.- Returns:
- true if so
-
hasRequests
public boolean hasRequests()
Check if demand is higher than 0. Returned value should be used as informative and can change asynchronously.- Returns:
- true if demand is higher than 0
-
isCompleted
public boolean isCompleted()
Check if publisher sentonCompletesignal downstream. Returnstrueright after callingcompleteNow()but after callingcomplete()returnsfalseuntil whole buffer has been drained.- Returns:
- true if so
-
isCancelled
public boolean isCancelled()
Check if publisher is in terminal state CANCELLED.- Returns:
- true if so
-
bufferSize
public int bufferSize()
Estimated size of the buffer. Returned value should be used as informative and can change asynchronously.- Returns:
- estimated size of the buffer
-
-