- java.lang.Object
-
- io.helidon.common.reactive.EmittingPublisher<T>
-
- Type Parameters:
T
- type of emitted item
- All Implemented Interfaces:
Flow.Publisher<T>
public class EmittingPublisher<T> extends Object implements Flow.Publisher<T>
Emitting publisher for manual publishing on the same thread.EmittingPublisher
doesn't have any buffering capability and propagates backpressure directly by returningfalse
fromemit(Object)
in case there is no demand, orcancel
signal has been received.For publishing with buffering in case of backpressure use
BufferedEmittingPublisher
.This publisher allows only a single subscriber.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
complete()
Properly complete the stream, set publisher to completed state and sendonComplete
signal downstream.static <T> EmittingPublisher<T>
create()
Create newEmittingPublisher
.boolean
emit(T item)
Emit one item to the stream, if there is enough requested and publisher is not cancelled, item is signaled to downstream asonNext
and method returns true.void
fail(Throwable throwable)
Properly fail the stream, set publisher to cancelled state and sendonError
signal downstream.Optional<Throwable>
failCause()
Return cause of fail, if publisher is in failed state.boolean
hasRequests()
Check if demand is higher than 0.boolean
isCancelled()
Check if publisher has been cancelled.boolean
isCompleted()
Check if publisher has been completed.boolean
isFailed()
Check if publisher has been failed.boolean
isUnbounded()
Check if downstream requested unbounded number of items, eg.void
onCancel(Runnable cancelCallback)
Executed when cancel signal from downstream arrive.void
onRequest(BiConsumer<Long,Long> requestCallback)
Callback executed when request signal from downstream arrive.void
subscribe(Flow.Subscriber<? super T> subscriber)
-
-
-
Method Detail
-
create
public static <T> EmittingPublisher<T> create()
Create newEmittingPublisher
.- Type Parameters:
T
- type of emitted item- Returns:
- brand new
EmittingPublisher
-
subscribe
public void subscribe(Flow.Subscriber<? super T> subscriber)
- Specified by:
subscribe
in interfaceFlow.Publisher<T>
-
fail
public void fail(Throwable throwable)
Properly fail the stream, set publisher to cancelled state and sendonError
signal downstream. SignalonError
is sent only once, any other call to this method is no-op.- Parameters:
throwable
- Sent asonError
signal
-
complete
public void complete()
Properly complete the stream, set publisher to completed state and sendonComplete
signal downstream. SignalonComplete
is sent only once, any other call to this method is no-op.
-
emit
public boolean emit(T item)
Emit one item to the stream, if there is enough requested and publisher is not cancelled, item is signaled to downstream asonNext
and method returns true. If there is requested less than 1, nothing is sent and method returns false.- Parameters:
item
- to be sent downstream- Returns:
- true if item successfully sent, false if canceled or no demand
- Throws:
IllegalStateException
- if publisher is completed
-
isCompleted
public boolean isCompleted()
Check if publisher has been completed.- Returns:
- true if so
-
isCancelled
public boolean isCancelled()
Check if publisher has been cancelled.- Returns:
- true if so
-
isFailed
public boolean isFailed()
Check if publisher has been failed.- 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 so
-
isUnbounded
public boolean isUnbounded()
Check if downstream requested unbounded number of items, eg. there is no backpressure.- Returns:
- true if so
-
failCause
public Optional<Throwable> failCause()
Return cause of fail, if publisher is in failed state.- Returns:
- optional cause of fail
-
onCancel
public void onCancel(Runnable cancelCallback)
Executed when cancel signal from downstream arrive.- Parameters:
cancelCallback
- to be executed
-
onRequest
public void onRequest(BiConsumer<Long,Long> requestCallback)
Callback executed when request signal from downstream arrive.- param
n
the requested count. - param
result
the 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
-
-