Package io.activej.csp
Interface ChannelConsumer<T>
- All Superinterfaces:
io.activej.async.process.AsyncCloseable
- All Known Implementing Classes:
AbstractChannelConsumer,ChannelFileWriter,RecyclingChannelConsumer
public interface ChannelConsumer<T>
extends io.activej.async.process.AsyncCloseable
This interface represents consumer of data items that should be used serially
(each consecutive
accept(Object) operation should be called only after
previous accept(Object) operation finishes)
After consumer is closed, all subsequent calls to accept(Object) will
return a completed exceptionally promise.
If any exception is caught while consuming data items, AsyncCloseable.closeEx(Exception)
method should be called. All resources should be freed and the caught exception
should be propagated to all related processes.
If accept(Object) takes null as argument, it represents end-of-stream
and means that no additional data should be consumed.
-
Field Summary
Fields inherited from interface io.activej.async.process.AsyncCloseable
STATIC -
Method Summary
Modifier and TypeMethodDescription@NotNull io.activej.promise.Promise<Void>Consumes a provided value and returns aPromiseas a marker of success.default @NotNull io.activej.promise.Promise<Void>default io.activej.promise.Promise<Void>default @NotNull io.activej.promise.Promise<Void>Accepts provided items and returnsPromiseas a marker of completion.default io.activej.promise.Promise<Void>default ChannelConsumer<T>async()default ChannelConsumer<T>Creates a wrapper ChannelConsumer - when itsaccept(T value)is called, current ChannelConsumer will accept the value only of it passesPredicatetest.default <V> ChannelConsumer<V>Creates a wrapper ChannelConsumer - when itsaccept(T value)is called,fnwill be applied to the providedvaluefirst and the result of thefnwill be accepted by current ChannelConsumer.default <V> ChannelConsumer<V>Creates a wrapper ChannelConsumer - when itsaccept(T value)is called,fnwill be applied to the providedvaluefirst and the result of thefnwill be accepted by current ChannelConsumer asynchronously.static <T> ChannelConsumer<T>of(@NotNull io.activej.async.function.AsyncConsumer<T> consumer) WrapsAsyncConsumerinChannelConsumer.static <T> ChannelConsumer<T>of(@NotNull io.activej.async.function.AsyncConsumer<T> consumer, @Nullable io.activej.async.process.AsyncCloseable closeable) WrapsAsyncConsumerinChannelConsumer.static <T> ChannelConsumer<T>ofAnotherEventloop(@NotNull io.activej.eventloop.Eventloop anotherEventloop, @NotNull ChannelConsumer<T> anotherEventloopConsumer) static <T> ChannelConsumer<T>ofConsumer(@NotNull io.activej.common.function.ConsumerEx<T> consumer) Wraps aConsumerExinChannelConsumer.static <T> ChannelConsumer<T>Creates a consumer which always returns Promise of exception when accepts values.static <T> ChannelConsumer<T>ofLazyProvider(Supplier<? extends ChannelConsumer<T>> provider) Returns aChannelConsumerwrapped inSupplierand calls itsaccept()whenaccept()method is called.static <T> ChannelConsumer<T>ofPromise(io.activej.promise.Promise<? extends ChannelConsumer<T>> promise) UnwrapsChannelConsumerof providedPromise.static ChannelConsumer<io.activej.bytebuf.ByteBuf>ofSocket(AsyncTcpSocket socket) WrapsAsyncTcpSocket.write(ByteBuf)operation intoChannelConsumer.static <T> ChannelConsumer<T>ofSupplier(io.activej.async.function.AsyncConsumer<ChannelSupplier<T>> supplierConsumer) static <T> ChannelConsumer<T>ofSupplier(io.activej.async.function.AsyncConsumer<ChannelSupplier<T>> supplierConsumer, ChannelQueue<T> queue) default ChannelConsumer<T>Creates a wrapper ChannelConsumer - when itsaccept(T value)is called, if providedvaluedoesn't equalnull, it will be accepted by the providedfnfirst and then by this ChannelConsumer.default <R> RTransforms currentChannelConsumerwith providedChannelConsumerTransformer.default ChannelConsumer<T>withAcknowledgement(UnaryOperator<io.activej.promise.Promise<Void>> fn) Creates a wrapper ChannelConsumer - after itsaccept(T value)is called and completed, an acknowledgement is returned.default ChannelConsumer<T>withExecutor(io.activej.async.process.AsyncExecutor asyncExecutor) Creates a wrapper ChannelConsumer which executes current ChannelConsumer'saccept(T value)within theasyncExecutor.Methods inherited from interface io.activej.async.process.AsyncCloseable
close, closeEx
-
Method Details
-
accept
Consumes a provided value and returns aPromiseas a marker of success. -
acceptEndOfStream
-
acceptAll
Accepts provided items and returnsPromiseas a marker of completion. If one of the items was accepted with an error, subsequent items will be recycled and aPromiseof exception will be returned. -
acceptAll
-
acceptAll
- See Also:
-
of
static <T> ChannelConsumer<T> of(@NotNull @NotNull io.activej.async.function.AsyncConsumer<T> consumer) WrapsAsyncConsumerinChannelConsumer.- See Also:
-
of
static <T> ChannelConsumer<T> of(@NotNull @NotNull io.activej.async.function.AsyncConsumer<T> consumer, @Nullable @Nullable io.activej.async.process.AsyncCloseable closeable) WrapsAsyncConsumerinChannelConsumer.- Type Parameters:
T- type of data to be consumed- Parameters:
consumer- AsyncConsumer to be wrappedcloseable- a Cancellable, which will be set to the returned ChannelConsumer- Returns:
- AbstractChannelConsumer which wraps AsyncConsumer
-
ofConsumer
static <T> ChannelConsumer<T> ofConsumer(@NotNull @NotNull io.activej.common.function.ConsumerEx<T> consumer) Wraps aConsumerExinChannelConsumer. -
ofException
Creates a consumer which always returns Promise of exception when accepts values.- Type Parameters:
T- type of data to be consumed- Parameters:
e- an exception which is wrapped in returned Promise whenaccept()is called- Returns:
- an AbstractChannelConsumer which always returns Promise of exception when accepts values
-
ofSupplier
static <T> ChannelConsumer<T> ofSupplier(io.activej.async.function.AsyncConsumer<ChannelSupplier<T>> supplierConsumer) - See Also:
-
ofSupplier
static <T> ChannelConsumer<T> ofSupplier(io.activej.async.function.AsyncConsumer<ChannelSupplier<T>> supplierConsumer, ChannelQueue<T> queue) -
ofPromise
static <T> ChannelConsumer<T> ofPromise(io.activej.promise.Promise<? extends ChannelConsumer<T>> promise) UnwrapsChannelConsumerof providedPromise. If provided Promise is already successfully completed, its result will be returned, otherwise anAbstractChannelConsumeris created, which waits for the Promise to be completed before accepting any value. A Promise of Exception will be returned if Promise was completed with an exception.- Type Parameters:
T- type of data to be consumed- Parameters:
promise- Promise ofChannelConsumer- Returns:
- ChannelConsumer b
-
ofAnotherEventloop
static <T> ChannelConsumer<T> ofAnotherEventloop(@NotNull @NotNull io.activej.eventloop.Eventloop anotherEventloop, @NotNull @NotNull ChannelConsumer<T> anotherEventloopConsumer) -
ofLazyProvider
Returns aChannelConsumerwrapped inSupplierand calls itsaccept()whenaccept()method is called.- Parameters:
provider- provider of theChannelConsumer- Returns:
- a
ChannelConsumerwhich was wrapped in theprovider
-
ofSocket
WrapsAsyncTcpSocket.write(ByteBuf)operation intoChannelConsumer.- Returns:
ChannelConsumerof ByteBufs that will be sent to network
-
transformWith
Transforms currentChannelConsumerwith providedChannelConsumerTransformer.- Type Parameters:
R- result value after transformation- Parameters:
fn- transformer of theChannelConsumer- Returns:
- result of transformation applied to the current
ChannelConsumer
-
async
-
withExecutor
Creates a wrapper ChannelConsumer which executes current ChannelConsumer'saccept(T value)within theasyncExecutor.- Parameters:
asyncExecutor- executes ChannelConsumer- Returns:
- a wrapper of current ChannelConsumer which executes
in provided
asyncExecutor
-
peek
Creates a wrapper ChannelConsumer - when itsaccept(T value)is called, if providedvaluedoesn't equalnull, it will be accepted by the providedfnfirst and then by this ChannelConsumer.- Parameters:
fn-Consumerwhich accepts the value passed byapply(T value)- Returns:
- a wrapper ChannelConsumer
-
map
Creates a wrapper ChannelConsumer - when itsaccept(T value)is called,fnwill be applied to the providedvaluefirst and the result of thefnwill be accepted by current ChannelConsumer. If providevalueisnull,fnwon't be applied.- Type Parameters:
V- type of data accepted and returned by thefnand accepted by ChannelConsumer- Parameters:
fn-Functionto be applied to the value ofapply(T value)- Returns:
- a wrapper ChannelConsumer
-
mapAsync
Creates a wrapper ChannelConsumer - when itsaccept(T value)is called,fnwill be applied to the providedvaluefirst and the result of thefnwill be accepted by current ChannelConsumer asynchronously. If providedvalueisnull,fnwon't be applied.- Type Parameters:
V- type of data accepted by thefnand ChannelConsumer- Parameters:
fn-Functionto be applied to the value ofapply(T value)- Returns:
- a wrapper ChannelConsumer
-
filter
Creates a wrapper ChannelConsumer - when itsaccept(T value)is called, current ChannelConsumer will accept the value only of it passesPredicatetest.- Parameters:
predicate-Predicatewhich is used to filter accepted value- Returns:
- a wrapper ChannelConsumer
-
withAcknowledgement
Creates a wrapper ChannelConsumer - after itsaccept(T value)is called and completed, an acknowledgement is returned. An acknowledgement is aSettablePromisewhich is accepted by the providedfnand then materialized.- Parameters:
fn- a function applied to theSettablePromisewhich is then materialized and returned- Returns:
- a wrapper ChannelConsumer
-