Class Sink<In,Mat>
- java.lang.Object
-
- akka.stream.javadsl.Sink<In,Mat>
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <In> Sink<In,NotUsed>actorRef(ActorRef ref, java.lang.Object onCompleteMessage)Sends the elements of the stream to the givenActorRef.static <In> Sink<In,NotUsed>actorRefWithAck(ActorRef ref, java.lang.Object onInitMessage, java.lang.Object ackMessage, java.lang.Object onCompleteMessage, Function<java.lang.Throwable,java.lang.Object> onFailureMessage)Sends the elements of the stream to the givenActorRefthat sends back back-pressure signal.static <T> Sink<T,ActorRef>actorSubscriber(Props props)Deprecated.Useakka.stream.stage.GraphStageandfromGraphinstead, it allows for all operations an Actor would and is more type-safe as well as guaranteed to be ReactiveStreams compliant.Sink<In,Mat>addAttributes(Attributes attr)Add the given attributes to thisSink.static <T> Sink<T,org.reactivestreams.Publisher<T>>asPublisher(AsPublisher fanout)ASinkthat materializes into aPublisher.Sink<In,Mat>asScala()Converts this Sink to its Scala DSL counterpart.Sink<In,Mat>async()Put an asynchronous boundary around thisSinkSink<In,Mat>async(java.lang.String dispatcher)Put an asynchronous boundary around thisSinkSink<In,Mat>async(java.lang.String dispatcher, int inputBufferSize)Put an asynchronous boundary around thisSinkstatic <T> Sink<T,NotUsed>cancelled()ASinkthat immediately cancels its upstream after materialization.static <T,U>
Sink<T,NotUsed>combine(Sink<U,?> output1, Sink<U,?> output2, java.util.List<Sink<U,?>> rest, Function<java.lang.Integer,Graph<UniformFanOutShape<T,U>,NotUsed>> strategy)Combine several sinks with fan-out strategy likeBroadcastorBalanceand returnsSink.<In2> Sink<In2,Mat>contramap(Function<In2,In> f)Transform this Sink by applying a function to each *incoming* upstream element before it is passed to theSinkstatic <U,In>
Sink<In,java.util.concurrent.CompletionStage<U>>fold(U zero, Function2<U,In,U> f)static <U,In>
Sink<In,java.util.concurrent.CompletionStage<U>>foldAsync(U zero, Function2<U,In,java.util.concurrent.CompletionStage<U>> f)ASinkthat will invoke the given asynchronous function for every received element, giving it its previous output (or the givenzerovalue) and the element as input.static <T> Sink<T,java.util.concurrent.CompletionStage<Done>>foreach(Procedure<T> f)ASinkthat will invoke the given procedure for each received element.static <T> Sink<T,java.util.concurrent.CompletionStage<Done>>foreachAsync(int parallelism, Function<T,java.util.concurrent.CompletionStage<java.lang.Void>> f)ASinkthat will invoke the given procedure asynchronously for each received element.static <T> Sink<T,java.util.concurrent.CompletionStage<Done>>foreachParallel(int parallel, Procedure<T> f, scala.concurrent.ExecutionContext ec)Deprecated.Use `foreachAsync` instead, it allows you to choose how to run the procedure, by calling some other API returning a CompletionStage or using CompletableFuture.supplyAsync.static <T,M>
Sink<T,M>fromGraph(Graph<SinkShape<T>,M> g)A graph with the shape of a sink logically is a sink, this method makes it so also in type.static <In> Sink<In,NotUsed>fromSubscriber(org.reactivestreams.Subscriber<In> subs)Helper to createSinkfromSubscriber.static <In> Sink<In,java.util.concurrent.CompletionStage<In>>head()ASinkthat materializes into aCompletionStageof the first value received.static <In> Sink<In,java.util.concurrent.CompletionStage<java.util.Optional<In>>>headOption()ASinkthat materializes into aCompletionStageof the optional first value received.static <T> Sink<T,java.util.concurrent.CompletionStage<Done>>ignore()ASinkthat will consume the stream and discard the elements.static <In> Sink<In,java.util.concurrent.CompletionStage<In>>last()ASinkthat materializes into aCompletionStageof the last value received.static <In> Sink<In,java.util.concurrent.CompletionStage<java.util.Optional<In>>>lastOption()ASinkthat materializes into aCompletionStageof the optional last value received.static <T,M>
Sink<T,java.util.concurrent.CompletionStage<M>>lazyInit(Function<T,java.util.concurrent.CompletionStage<Sink<T,M>>> sinkFactory, Creator<M> fallback)Deprecated.Use lazyInitAsync instead.static <T,M>
Sink<T,java.util.concurrent.CompletionStage<java.util.Optional<M>>>lazyInitAsync(Creator<java.util.concurrent.CompletionStage<Sink<T,M>>> sinkFactory)Creates a realSinkupon receiving the first element.<Mat2> Sink<In,Mat2>mapMaterializedValue(Function<Mat,Mat2> f)Transform only the materialized value of this Sink, leaving all other properties as they were.Sink<In,Mat>named(java.lang.String name)Add anameattribute to this Sink.static <In> Sink<In,NotUsed>onComplete(Procedure<scala.util.Try<Done>> callback)ASinkthat when the flow is completed, either through a failure or normal completion, apply the provided function withSuccessorFailure.Pair<Mat,Sink<In,NotUsed>>preMaterialize(Materializer materializer)Materializes this Sink, immediately returning (1) its materialized value, and (2) a new Sink that can be consume elements 'into' the pre-materialized one.static <T> Sink<T,SinkQueueWithCancel<T>>queue()Creates aSinkthat is materialized as anSinkQueue.static <In> Sink<In,java.util.concurrent.CompletionStage<In>>reduce(Function2<In,In,In> f)ASinkthat will invoke the given function for every received element, giving it its previous output (from the second element) and the element as input.<M> MrunWith(Graph<SourceShape<In>,M> source, Materializer materializer)Connect thisSinkto aSourceand run it.static <In> Sink<In,java.util.concurrent.CompletionStage<java.util.List<In>>>seq()ASinkthat keeps on collecting incoming elements until upstream terminates.static <T,M>
Sink<T,java.util.concurrent.CompletionStage<M>>setup(java.util.function.BiFunction<ActorMaterializer,Attributes,Sink<T,M>> factory)Defers the creation of aSinkuntil materialization.SinkShape<In>shape()The shape of a graph is all that is externally visible: its inlets and outlets.static <In> Sink<In,java.util.concurrent.CompletionStage<java.util.List<In>>>takeLast(int n)ASinkthat materializes into a aCompletionStageofListcontaining the lastncollected elements.java.lang.StringtoString()LinearTraversalBuildertraversalBuilder()INTERNAL API.Sink<In,Mat>withAttributes(Attributes attr)Replace the attributes of thisSinkwith the given ones.
-
-
-
Method Detail
-
fold
public static <U,In> Sink<In,java.util.concurrent.CompletionStage<U>> fold(U zero, Function2<U,In,U> f)
-
foldAsync
public static <U,In> Sink<In,java.util.concurrent.CompletionStage<U>> foldAsync(U zero, Function2<U,In,java.util.concurrent.CompletionStage<U>> f)
ASinkthat will invoke the given asynchronous function for every received element, giving it its previous output (or the givenzerovalue) and the element as input. The returnedCompletionStagewill be completed with value of the final function evaluation when the input stream ends, or completed withFailureif there is a failure is signaled in the stream.- Parameters:
zero- (undocumented)f- (undocumented)- Returns:
- (undocumented)
-
reduce
public static <In> Sink<In,java.util.concurrent.CompletionStage<In>> reduce(Function2<In,In,In> f)
ASinkthat will invoke the given function for every received element, giving it its previous output (from the second element) and the element as input. The returnedCompletionStagewill be completed with value of the final function evaluation when the input stream ends, or completed withFailureif there is a failure signaled in the stream.If the stream is empty (i.e. completes before signalling any elements), the reduce operator will fail its downstream with a
NoSuchElementException, which is semantically in-line with that Scala's standard library collections do in such situations.- Parameters:
f- (undocumented)- Returns:
- (undocumented)
-
fromSubscriber
public static <In> Sink<In,NotUsed> fromSubscriber(org.reactivestreams.Subscriber<In> subs)
Helper to createSinkfromSubscriber.- Parameters:
subs- (undocumented)- Returns:
- (undocumented)
-
cancelled
public static <T> Sink<T,NotUsed> cancelled()
ASinkthat immediately cancels its upstream after materialization.- Returns:
- (undocumented)
-
ignore
public static <T> Sink<T,java.util.concurrent.CompletionStage<Done>> ignore()
ASinkthat will consume the stream and discard the elements.- Returns:
- (undocumented)
-
asPublisher
public static <T> Sink<T,org.reactivestreams.Publisher<T>> asPublisher(AsPublisher fanout)
ASinkthat materializes into aPublisher.If
fanoutistrue, the materializedPublisherwill support multipleSubscribers and the size of theinputBufferconfigured for this operator becomes the maximum number of elements that the fastestSubscribercan be ahead of the slowest one before slowing the processing down due to back pressure.If
fanoutisfalsethen the materializedPublisherwill only support a singleSubscriberand reject any additionalSubscribers.- Parameters:
fanout- (undocumented)- Returns:
- (undocumented)
-
foreach
public static <T> Sink<T,java.util.concurrent.CompletionStage<Done>> foreach(Procedure<T> f)
ASinkthat will invoke the given procedure for each received element. The sink is materialized into aCompletionStagewhich will be completed withSuccesswhen reaching the normal end of the stream, or completed withFailureif there is a failure signaled in the stream.- Parameters:
f- (undocumented)- Returns:
- (undocumented)
-
foreachAsync
public static <T> Sink<T,java.util.concurrent.CompletionStage<Done>> foreachAsync(int parallelism, Function<T,java.util.concurrent.CompletionStage<java.lang.Void>> f)
ASinkthat will invoke the given procedure asynchronously for each received element. The sink is materialized into aCompletionStagewhich will be completed withSuccesswhen reaching the normal end of the stream, or completed withFailureif there is a failure signaled in the stream.- Parameters:
parallelism- (undocumented)f- (undocumented)- Returns:
- (undocumented)
-
foreachParallel
public static <T> Sink<T,java.util.concurrent.CompletionStage<Done>> foreachParallel(int parallel, Procedure<T> f, scala.concurrent.ExecutionContext ec)
Deprecated.Use `foreachAsync` instead, it allows you to choose how to run the procedure, by calling some other API returning a CompletionStage or using CompletableFuture.supplyAsync. Since 2.5.17.ASinkthat will invoke the given procedure for each received element in parallel. The sink is materialized into aCompletionStage.If
fthrows an exception and the supervision decision isakka.stream.Supervision.StoptheCompletionStagewill be completed with failure.If
fthrows an exception and the supervision decision isakka.stream.Supervision.Resumeorakka.stream.Supervision.Restartthe element is dropped and the stream continues.- Parameters:
parallel- (undocumented)f- (undocumented)ec- (undocumented)- Returns:
- (undocumented)
-
onComplete
public static <In> Sink<In,NotUsed> onComplete(Procedure<scala.util.Try<Done>> callback)
ASinkthat when the flow is completed, either through a failure or normal completion, apply the provided function withSuccessorFailure.- Parameters:
callback- (undocumented)- Returns:
- (undocumented)
-
head
public static <In> Sink<In,java.util.concurrent.CompletionStage<In>> head()
ASinkthat materializes into aCompletionStageof the first value received. If the stream completes before signaling at least a single element, the CompletionStage will be failed with aNoSuchElementException. If the stream signals an error before signaling at least a single element, the CompletionStage will be failed with the streams exception.See also
<In>headOption().- Returns:
- (undocumented)
-
headOption
public static <In> Sink<In,java.util.concurrent.CompletionStage<java.util.Optional<In>>> headOption()
ASinkthat materializes into aCompletionStageof the optional first value received. If the stream completes before signaling at least a single element, the value of the CompletionStage will be an emptyOptional. If the stream signals an error errors before signaling at least a single element, the CompletionStage will be failed with the streams exception.See also
<In>head().- Returns:
- (undocumented)
-
last
public static <In> Sink<In,java.util.concurrent.CompletionStage<In>> last()
ASinkthat materializes into aCompletionStageof the last value received. If the stream completes before signaling at least a single element, the CompletionStage will be failed with aNoSuchElementException. If the stream signals an error errors before signaling at least a single element, the CompletionStage will be failed with the streams exception.See also
<In>lastOption(),<In>takeLast(int).- Returns:
- (undocumented)
-
lastOption
public static <In> Sink<In,java.util.concurrent.CompletionStage<java.util.Optional<In>>> lastOption()
ASinkthat materializes into aCompletionStageof the optional last value received. If the stream completes before signaling at least a single element, the value of the CompletionStage will be an emptyOptional. If the stream signals an error errors before signaling at least a single element, the CompletionStage will be failed with the streams exception.See also
<In>head(),<In>takeLast(int).- Returns:
- (undocumented)
-
takeLast
public static <In> Sink<In,java.util.concurrent.CompletionStage<java.util.List<In>>> takeLast(int n)
ASinkthat materializes into a aCompletionStageofListcontaining the lastncollected elements.If the stream completes before signaling at least n elements, the
CompletionStagewill complete with all elements seen so far. If the stream never completes theCompletionStagewill never complete. If there is a failure signaled in the stream theCompletionStagewill be completed with failure.- Parameters:
n- (undocumented)- Returns:
- (undocumented)
-
seq
public static <In> Sink<In,java.util.concurrent.CompletionStage<java.util.List<In>>> seq()
ASinkthat keeps on collecting incoming elements until upstream terminates. As upstream may be unbounded,Flow[T].takeor the stricterFlow[T].limit(and their variants) may be used to ensure boundedness. Materializes into aCompletionStageofSeq[T]containing all the collected elements.Listis limited toInteger.MAX_VALUEelements, this Sink will cancel the stream after having received that many elements.See also
Flow.limit,Flow.limitWeighted,Flow.take,Flow.takeWithin,Flow.takeWhile- Returns:
- (undocumented)
-
actorRef
public static <In> Sink<In,NotUsed> actorRef(ActorRef ref, java.lang.Object onCompleteMessage)
Sends the elements of the stream to the givenActorRef. If the target actor terminates the stream will be canceled. When the stream is completed successfully the givenonCompleteMessagewill be sent to the destination actor. When the stream is completed with failure aStatus.Failuremessage will be sent to the destination actor.It will request at most
maxInputBufferSizenumber of elements from upstream, but there is no back-pressure signal from the destination actor, i.e. if the actor is not consuming the messages fast enough the mailbox of the actor will grow. For potentially slow consumer actors it is recommended to use a bounded mailbox with zeromailbox-push-timeout-timeor use a rate limiting operator in front of thisSink.- Parameters:
ref- (undocumented)onCompleteMessage- (undocumented)- Returns:
- (undocumented)
-
actorRefWithAck
public static <In> Sink<In,NotUsed> actorRefWithAck(ActorRef ref, java.lang.Object onInitMessage, java.lang.Object ackMessage, java.lang.Object onCompleteMessage, Function<java.lang.Throwable,java.lang.Object> onFailureMessage)
Sends the elements of the stream to the givenActorRefthat sends back back-pressure signal. First element is alwaysonInitMessage, then stream is waiting for acknowledgement messageackMessagefrom the given actor which means that it is ready to process elements. It also requiresackMessagemessage after each stream element to make backpressure work.If the target actor terminates the stream will be canceled. When the stream is completed successfully the given
onCompleteMessagewill be sent to the destination actor. When the stream is completed with failure - result ofonFailureMessage(throwable)message will be sent to the destination actor.- Parameters:
ref- (undocumented)onInitMessage- (undocumented)ackMessage- (undocumented)onCompleteMessage- (undocumented)onFailureMessage- (undocumented)- Returns:
- (undocumented)
-
actorSubscriber
public static <T> Sink<T,ActorRef> actorSubscriber(Props props)
Deprecated.Useakka.stream.stage.GraphStageandfromGraphinstead, it allows for all operations an Actor would and is more type-safe as well as guaranteed to be ReactiveStreams compliant.Creates aSinkthat is materialized to anActorRefwhich points to an Actor created according to the passed inProps. Actor created by thepropsshould beActorSubscriber.- Parameters:
props- (undocumented)- Returns:
- (undocumented)
-
fromGraph
public static <T,M> Sink<T,M> fromGraph(Graph<SinkShape<T>,M> g)
A graph with the shape of a sink logically is a sink, this method makes it so also in type.- Parameters:
g- (undocumented)- Returns:
- (undocumented)
-
setup
public static <T,M> Sink<T,java.util.concurrent.CompletionStage<M>> setup(java.util.function.BiFunction<ActorMaterializer,Attributes,Sink<T,M>> factory)
Defers the creation of aSinkuntil materialization. Thefactoryfunction exposesActorMaterializerwhich is going to be used during materialization andAttributesof theSinkreturned by this method.- Parameters:
factory- (undocumented)- Returns:
- (undocumented)
-
combine
public static <T,U> Sink<T,NotUsed> combine(Sink<U,?> output1, Sink<U,?> output2, java.util.List<Sink<U,?>> rest, Function<java.lang.Integer,Graph<UniformFanOutShape<T,U>,NotUsed>> strategy)
Combine several sinks with fan-out strategy likeBroadcastorBalanceand returnsSink.- Parameters:
output1- (undocumented)output2- (undocumented)rest- (undocumented)strategy- (undocumented)- Returns:
- (undocumented)
-
queue
public static <T> Sink<T,SinkQueueWithCancel<T>> queue()
Creates aSinkthat is materialized as anSinkQueue.akka.stream.javadsl.SinkQueue.pullmethod is pulling element from the stream and returnsCompletionStage[Option[T}.CompletionStagecompletes when element is available.Before calling pull method second time you need to wait until previous CompletionStage completes. Pull returns Failed future with ''IllegalStateException'' if previous future has not yet completed.
Sinkwill request at most number of elements equal to size ofinputBufferfrom upstream and then stop back pressure. You can configure size of input buffer by usingSink.withAttributesmethod.For stream completion you need to pull all elements from
SinkQueueincluding last None as completion marker- Returns:
- (undocumented)
- See Also:
SinkQueueWithCancel
-
lazyInit
public static <T,M> Sink<T,java.util.concurrent.CompletionStage<M>> lazyInit(Function<T,java.util.concurrent.CompletionStage<Sink<T,M>>> sinkFactory, Creator<M> fallback)
Deprecated.Use lazyInitAsync instead. (lazyInitAsync no more needs a fallback function and the materialized value more clearly indicates if the internal sink was materialized or not.). Since 2.5.11.Creates a realSinkupon receiving the first element. InternalSinkwill not be created if there are no elements, because of completion or error.If upstream completes before an element was received then the
Futureis completed with the value created by fallback. If upstream fails before an element was received,sinkFactorythrows an exception, or materialization of the internal sink fails then theFutureis completed with the exception. Otherwise theFutureis completed with the materialized value of the internal sink.- Parameters:
sinkFactory- (undocumented)fallback- (undocumented)- Returns:
- (undocumented)
-
lazyInitAsync
public static <T,M> Sink<T,java.util.concurrent.CompletionStage<java.util.Optional<M>>> lazyInitAsync(Creator<java.util.concurrent.CompletionStage<Sink<T,M>>> sinkFactory)
Creates a realSinkupon receiving the first element. InternalSinkwill not be created if there are no elements, because of completion or error.If upstream completes before an element was received then the
Futureis completed withNone. If upstream fails before an element was received,sinkFactorythrows an exception, or materialization of the internal sink fails then theFutureis completed with the exception. Otherwise theFutureis completed with the materialized value of the internal sink.- Parameters:
sinkFactory- (undocumented)- Returns:
- (undocumented)
-
shape
public SinkShape<In> shape()
Description copied from interface:GraphThe shape of a graph is all that is externally visible: its inlets and outlets.
-
traversalBuilder
public LinearTraversalBuilder traversalBuilder()
Description copied from interface:GraphINTERNAL API.Every materializable element must be backed by a stream layout module
- Specified by:
traversalBuilderin interfaceGraph<In,Mat>- Returns:
- (undocumented)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
asScala
public Sink<In,Mat> asScala()
Converts this Sink to its Scala DSL counterpart.- Returns:
- (undocumented)
-
runWith
public <M> M runWith(Graph<SourceShape<In>,M> source, Materializer materializer)
Connect thisSinkto aSourceand run it.- Parameters:
source- (undocumented)materializer- (undocumented)- Returns:
- (undocumented)
-
contramap
public <In2> Sink<In2,Mat> contramap(Function<In2,In> f)
Transform this Sink by applying a function to each *incoming* upstream element before it is passed to theSink'''Backpressures when''' original
Sinkbackpressures'''Cancels when''' original
Sinkbackpressures- Parameters:
f- (undocumented)- Returns:
- (undocumented)
-
mapMaterializedValue
public <Mat2> Sink<In,Mat2> mapMaterializedValue(Function<Mat,Mat2> f)
Transform only the materialized value of this Sink, leaving all other properties as they were.- Parameters:
f- (undocumented)- Returns:
- (undocumented)
-
preMaterialize
public Pair<Mat,Sink<In,NotUsed>> preMaterialize(Materializer materializer)
Materializes this Sink, immediately returning (1) its materialized value, and (2) a new Sink that can be consume elements 'into' the pre-materialized one.Useful for when you need a materialized value of a Sink when handing it out to someone to materialize it for you.
- Parameters:
materializer- (undocumented)- Returns:
- (undocumented)
-
withAttributes
public Sink<In,Mat> withAttributes(Attributes attr)
Replace the attributes of thisSinkwith the given ones. If this Sink is a composite of multiple graphs, new attributes on the composite will be less specific than attributes set directly on the individual graphs of the composite.- Specified by:
withAttributesin interfaceGraph<In,Mat>- Parameters:
attr- (undocumented)- Returns:
- (undocumented)
-
addAttributes
public Sink<In,Mat> addAttributes(Attributes attr)
Add the given attributes to thisSink. If the specific attribute was already present on this graph this means the added attribute will be more specific than the existing one. If this Sink is a composite of multiple graphs, new attributes on the composite will be less specific than attributes set directly on the individual graphs of the composite.- Specified by:
addAttributesin interfaceGraph<In,Mat>- Parameters:
attr- (undocumented)- Returns:
- (undocumented)
-
async
public Sink<In,Mat> async(java.lang.String dispatcher)
Put an asynchronous boundary around thisSink
-
-