Class Source$
- java.lang.Object
-
- akka.stream.javadsl.Source$
-
public class Source$ extends java.lang.Object
Java API
-
-
Constructor Summary
Constructors Constructor Description Source$()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <T> Source<T,ActorRef>
actorRef(int bufferSize, OverflowStrategy overflowStrategy)
Deprecated.Use variant accepting completion and failure matchers.<T> Source<T,ActorRef>
actorRef(Function<java.lang.Object,java.util.Optional<CompletionStrategy>> completionMatcher, Function<java.lang.Object,java.util.Optional<java.lang.Throwable>> failureMatcher, int bufferSize, OverflowStrategy overflowStrategy)
Creates aSource
that is materialized as anActorRef
.<T> Source<T,ActorRef>
actorRefWithAck(java.lang.Object ackMessage)
Deprecated.Use actorRefWithBackpressure accepting completion and failure matchers.<T> Source<T,ActorRef>
actorRefWithAck(java.lang.Object ackMessage, Function<java.lang.Object,java.util.Optional<CompletionStrategy>> completionMatcher, Function<java.lang.Object,java.util.Optional<java.lang.Throwable>> failureMatcher)
Deprecated.Use actorRefWithBackpressure instead<T> Source<T,ActorRef>
actorRefWithBackpressure(java.lang.Object ackMessage, Function<java.lang.Object,java.util.Optional<CompletionStrategy>> completionMatcher, Function<java.lang.Object,java.util.Optional<java.lang.Throwable>> failureMatcher)
Creates aSource
that is materialized as anActorRef
.<T> Source<T,org.reactivestreams.Subscriber<T>>
asSubscriber()
Creates aSource
that is materialized as aSubscriber
<T,U>
Source<U,NotUsed>combine(Source<T,?> first, Source<T,?> second, java.util.List<Source<T,?>> rest, Function<java.lang.Integer,? extends Graph<UniformFanInShape<T,U>,NotUsed>> fanInStrategy)
<T,U,M>
Source<U,java.util.List<M>>combine(java.util.List<? extends Graph<SourceShape<T>,M>> sources, Function<java.lang.Integer,Graph<UniformFanInShape<T,U>,NotUsed>> fanInStrategy)
<T,U,M1,M2,M>
Source<U,M>combineMat(Source<T,M1> first, Source<T,M2> second, Function<java.lang.Integer,? extends Graph<UniformFanInShape<T,U>,NotUsed>> fanInStrategy, Function2<M1,M2,M> matF)
Combines two sources with fan-in strategy likeMerge
orConcat
and returnsSource
with a materialized value.<T> Source<T,NotUsed>
completionStage(java.util.concurrent.CompletionStage<T> completionStage)
Emits a single value when the givenCompletionStage
is successfully completed and then completes the stream.<T,M>
Source<T,java.util.concurrent.CompletionStage<M>>completionStageSource(java.util.concurrent.CompletionStage<Source<T,M>> completionStageSource)
Turn aCompletionStage[Source]
into a source that will emit the values of the source when the future completes successfully.<O> Source<O,NotUsed>
cycle(Creator<java.util.Iterator<O>> f)
Helper to create a 'cycled'Source
that will continually produce elements in the order they are provided.<O> Source<O,NotUsed>
empty()
Create aSource
with no elements, i.e.<T> Source<T,NotUsed>
empty(java.lang.Class<T> clazz)
Create aSource
with no elements.<T> Source<T,NotUsed>
failed(java.lang.Throwable cause)
Create aSource
that immediately ends the stream with thecause
failure to every connectedSink
.<O> Source<O,NotUsed>
from(java.lang.Iterable<O> iterable)
Helper to createSource
fromIterable
.<O> Source<O,NotUsed>
fromCompletionStage(java.util.concurrent.CompletionStage<O> future)
Deprecated.Use 'Source.completionStage' instead.<O> Source<O,NotUsed>
fromFuture(scala.concurrent.Future<O> future)
Deprecated.Use 'Source.future' instead.<T,M>
Source<T,scala.concurrent.Future<M>>fromFutureSource(scala.concurrent.Future<? extends Graph<SourceShape<T>,M>> future)
Deprecated.Use 'Source.futureSource' (potentially together with `Source.fromGraph`) instead.<T,M>
Source<T,M>fromGraph(Graph<SourceShape<T>,M> g)
A graph with the shape of a source logically is a source, this method makes it so also in type.<O> Source<O,NotUsed>
fromIterator(Creator<java.util.Iterator<O>> f)
Helper to create aSource
from anIterator
.<O,S extends java.util.stream.BaseStream<O,S>>
Source<O,NotUsed>fromJavaStream(Creator<java.util.stream.BaseStream<O,S>> stream)
Creates a source that wraps a Java 8Stream
.
<T,M>
Source<T,java.util.concurrent.CompletionStage<M>>fromMaterializer(java.util.function.BiFunction<Materializer,Attributes,Source<T,M>> factory)
Defers the creation of aSource
until materialization.<O> Source<O,NotUsed>
fromPublisher(org.reactivestreams.Publisher<O> publisher)
Helper to createSource
fromPublisher
.<T,M>
Source<T,java.util.concurrent.CompletionStage<M>>fromSourceCompletionStage(java.util.concurrent.CompletionStage<? extends Graph<SourceShape<T>,M>> completion)
Deprecated.Use 'Source.completionStageSource' (potentially together with `Source.fromGraph`) instead.<T> Source<T,NotUsed>
future(scala.concurrent.Future<T> futureElement)
Emits a single value when the given ScalaFuture
is successfully completed and then completes the stream.<T,M>
Source<T,java.util.concurrent.CompletionStage<M>>lazily(Creator<Source<T,M>> create)
Deprecated.Use 'Source.lazySource' instead.<T> Source<T,scala.concurrent.Future<NotUsed>>
lazilyAsync(Creator<java.util.concurrent.CompletionStage<T>> create)
Deprecated.Use 'Source.lazyCompletionStage' instead.<T> Source<T,NotUsed>
lazyCompletionStage(Creator<java.util.concurrent.CompletionStage<T>> create)
Defers invoking thecreate
function to create a future element until there is downstream demand.<T,M>
Source<T,java.util.concurrent.CompletionStage<M>>lazyCompletionStageSource(Creator<java.util.concurrent.CompletionStage<Source<T,M>>> create)
Defers invoking thecreate
function to create a future source until there is downstream demand.<T> Source<T,NotUsed>
lazySingle(Creator<T> create)
Defers invoking thecreate
function to create a single element until there is downstream demand.<T,M>
Source<T,java.util.concurrent.CompletionStage<M>>lazySource(Creator<Source<T,M>> create)
Defers invoking thecreate
function to create a future source until there is downstream demand.<T> Source<T,java.util.concurrent.CompletableFuture<java.util.Optional<T>>>
maybe()
Create aSource
which materializes aCompletableFuture
which controls what element will be emitted by the Source.<T> Source<T,NotUsed>
mergePrioritizedN(java.util.List<Pair<Source<T,?>,java.lang.Integer>> sourcesAndPriorities, boolean eagerComplete)
Merge multipleSource
s.<T> Source<T,NotUsed>
never()
Never emits any elements, never completes and never fails.<T> Source<T,BoundedSourceQueue<T>>
queue(int bufferSize)
Creates aSource
that is materialized as anBoundedSourceQueue
.<T> Source<T,SourceQueueWithComplete<T>>
queue(int bufferSize, OverflowStrategy overflowStrategy)
Creates aSource
that is materialized as anSourceQueueWithComplete
.<T> Source<T,SourceQueueWithComplete<T>>
queue(int bufferSize, OverflowStrategy overflowStrategy, int maxConcurrentOffers)
Creates aSource
that is materialized as anSourceQueueWithComplete
.Source<java.lang.Integer,NotUsed>
range(int start, int end)
CreatesSource
that represents integer values in range ''[start;end]'', step equals to 1.Source<java.lang.Integer,NotUsed>
range(int start, int end, int step)
CreatesSource
that represents integer values in range ''[start;end]'', with the given step.<T> Source<T,NotUsed>
repeat(T element)
Create aSource
that will continually emit the given element.<T,M>
Source<T,java.util.concurrent.CompletionStage<M>>setup(java.util.function.BiFunction<ActorMaterializer,Attributes,Source<T,M>> factory)
Deprecated.Use 'fromMaterializer' instead.<T> Source<T,NotUsed>
single(T element)
Create aSource
with one element.<O> Source<O,Cancellable>
tick(java.time.Duration initialDelay, java.time.Duration interval, O tick)
Elements are emitted periodically with the specified interval.<S,E>
Source<E,NotUsed>unfold(S s, Function<S,java.util.Optional<Pair<S,E>>> f)
Create aSource
that will unfold a value of typeS
into a pair of the next stateS
and output elements of typeE
.<S,E>
Source<E,NotUsed>unfoldAsync(S s, Function<S,java.util.concurrent.CompletionStage<java.util.Optional<Pair<S,E>>>> f)
Same as<S,E>unfold(S,akka.japi.function.Function<S,java.util.Optional<akka.japi.Pair<S,E>>>)
, but uses an async function to generate the next state-element tuple.<T,S>
Source<T,NotUsed>unfoldResource(Creator<S> create, Function<S,java.util.Optional<T>> read, Procedure<S> close)
Start a newSource
from some resource which can be opened, read and closed.<T,S>
Source<T,NotUsed>unfoldResourceAsync(Creator<java.util.concurrent.CompletionStage<S>> create, Function<S,java.util.concurrent.CompletionStage<java.util.Optional<T>>> read, Function<S,java.util.concurrent.CompletionStage<Done>> close)
Start a newSource
from some resource which can be opened, read and closed.<SuperOut,Out extends SuperOut,Mat>
Source<SuperOut,Mat>upcast(Source<Out,Mat> source)
Upcast a stream of elements to a stream of supertypes of that element.<T> Source<java.util.List<T>,NotUsed>
zipN(java.util.List<Source<T,?>> sources)
Combine the elements of multiple streams into a stream of lists.<T,O>
Source<O,NotUsed>zipWithN(Function<java.util.List<T>,O> zipper, java.util.List<Source<T,?>> sources)
-
-
-
Field Detail
-
MODULE$
public static final Source$ MODULE$
Static reference to the singleton instance of this Scala object.
-
-
Method Detail
-
empty
public <O> Source<O,NotUsed> empty()
Create aSource
with no elements, i.e. an empty stream that is completed immediately for every connectedSink
.
-
empty
public <T> Source<T,NotUsed> empty(java.lang.Class<T> clazz)
Create aSource
with no elements. The result is the same as callingSource.
empty()
-
maybe
public <T> Source<T,java.util.concurrent.CompletableFuture<java.util.Optional<T>>> maybe()
Create aSource
which materializes aCompletableFuture
which controls what element will be emitted by the Source. If the materialized promise is completed with a filled Optional, that value will be produced downstream, followed by completion. If the materialized promise is completed with an empty Optional, no value will be produced downstream and completion will be signalled immediately. If the materialized promise is completed with a failure, then the source will fail with that error. If the downstream of this source cancels or fails before the promise has been completed, then the promise will be completed with an empty Optional.
-
fromPublisher
public <O> Source<O,NotUsed> fromPublisher(org.reactivestreams.Publisher<O> publisher)
Helper to createSource
fromPublisher
.Construct a transformation starting with given publisher. The transformation steps are executed by a series of
Processor
instances that mediate the flow of elements downstream and the propagation of back-pressure upstream.
-
fromIterator
public <O> Source<O,NotUsed> fromIterator(Creator<java.util.Iterator<O>> f)
Helper to create aSource
from anIterator
. Example usage:List<Integer> data = new ArrayList<Integer>(); data.add(1); data.add(2); data.add(3); Source.from(() -> data.iterator());
Start a new
Source
from the given Iterator. The produced stream of elements will continue until the iterator runs empty or fails during evaluation of thenext()
method. Elements are pulled out of the iterator in accordance with the demand coming from the downstream transformation steps.
-
fromJavaStream
public <O,S extends java.util.stream.BaseStream<O,S>> Source<O,NotUsed> fromJavaStream(Creator<java.util.stream.BaseStream<O,S>> stream)
Creates a source that wraps a Java 8Stream
.
Source
uses a stream iterator to get all its elements and send them downstream on demand.
You can use
Source.async
to create asynchronous boundaries between synchronous java stream and the rest of flow.
-
cycle
public <O> Source<O,NotUsed> cycle(Creator<java.util.Iterator<O>> f)
Helper to create a 'cycled'Source
that will continually produce elements in the order they are provided.Example usage:
Source.cycle(() -> Arrays.asList(1, 2, 3).iterator());
The function
f
is invoked to obtain an iterator and elements are emitted into the stream as provided by that iterator. If the iterator is finite, the functionf
invoked again, as necessary, when the elements from the previous iteration are exhausted. If every call to functionf
returns an iterator that produces the same elements in the same order, then theSource
will effectively be cyclic. However,f
is not required to behave that way, in which case theSource
will not be cyclic.The
Source
fails iff
returns an empty iterator.
-
from
public <O> Source<O,NotUsed> from(java.lang.Iterable<O> iterable)
Helper to createSource
fromIterable
. Example usage:List<Integer> data = new ArrayList<Integer>(); data.add(1); data.add(2); data.add(3); Source.from(data);
Starts a new
Source
from the givenIterable
. This is like starting from an Iterator, but every Subscriber directly attached to the Publisher of this stream will see an individual flow of elements (always starting from the beginning) regardless of when they subscribed.Make sure that the
Iterable
is immutable or at least not modified after being used as aSource
. Otherwise the stream may fail withConcurrentModificationException
or other more subtle errors may occur.
-
range
public Source<java.lang.Integer,NotUsed> range(int start, int end)
CreatesSource
that represents integer values in range ''[start;end]'', step equals to 1. It allows to createSource
out of range as simply as on ScalaSource(1 to N)
Uses {@link scala.collection.immutable.Range.inclusive(Int, Int)} internally
-
range
public Source<java.lang.Integer,NotUsed> range(int start, int end, int step)
CreatesSource
that represents integer values in range ''[start;end]'', with the given step. It allows to createSource
out of range as simply as on ScalaSource(1 to N)
Uses {@link scala.collection.immutable.Range.inclusive(Int, Int, Int)} internally
-
fromFuture
public <O> Source<O,NotUsed> fromFuture(scala.concurrent.Future<O> future)
Deprecated.Use 'Source.future' instead. Since 2.6.0.Start a newSource
from the givenFuture
. The stream will consist of one element when theFuture
is completed with a successful value, which may happen before or after materializing theFlow
. The stream terminates with a failure if theFuture
is completed with a failure.
-
fromCompletionStage
public <O> Source<O,NotUsed> fromCompletionStage(java.util.concurrent.CompletionStage<O> future)
Deprecated.Use 'Source.completionStage' instead. Since 2.6.0.Starts a newSource
from the givenCompletionStage
. The stream will consist of one element when theCompletionStage
is completed with a successful value, which may happen before or after materializing theFlow
. The stream terminates with a failure if theCompletionStage
is completed with a failure.
-
fromFutureSource
public <T,M> Source<T,scala.concurrent.Future<M>> fromFutureSource(scala.concurrent.Future<? extends Graph<SourceShape<T>,M>> future)
Deprecated.Use 'Source.futureSource' (potentially together with `Source.fromGraph`) instead. Since 2.6.0.Streams the elements of the given future source once it successfully completes. If theFuture
fails the stream is failed with the exception from the future. If downstream cancels before the stream completes the materializedFuture
will be failed with aStreamDetachedException
.
-
fromSourceCompletionStage
public <T,M> Source<T,java.util.concurrent.CompletionStage<M>> fromSourceCompletionStage(java.util.concurrent.CompletionStage<? extends Graph<SourceShape<T>,M>> completion)
Deprecated.Use 'Source.completionStageSource' (potentially together with `Source.fromGraph`) instead. Since 2.6.0.Streams the elements of an asynchronous source once its givenCompletionStage
completes. If theCompletionStage
fails the stream is failed with the exception from the future. If downstream cancels before the stream completes the materializedCompletionStage
will be failed with aStreamDetachedException
-
tick
public <O> Source<O,Cancellable> tick(java.time.Duration initialDelay, java.time.Duration interval, O tick)
Elements are emitted periodically with the specified interval. The tick element will be delivered to downstream consumers that has requested any elements. If a consumer has not requested any elements at the point in time when the tick element is produced it will not receive that tick element later. It will receive new tick elements as soon as it has requested more elements.
-
single
public <T> Source<T,NotUsed> single(T element)
Create aSource
with one element. Every connectedSink
of this stream will see an individual stream consisting of one element.
-
repeat
public <T> Source<T,NotUsed> repeat(T element)
Create aSource
that will continually emit the given element.
-
unfold
public <S,E> Source<E,NotUsed> unfold(S s, Function<S,java.util.Optional<Pair<S,E>>> f)
Create aSource
that will unfold a value of typeS
into a pair of the next stateS
and output elements of typeE
.
-
unfoldAsync
public <S,E> Source<E,NotUsed> unfoldAsync(S s, Function<S,java.util.concurrent.CompletionStage<java.util.Optional<Pair<S,E>>>> f)
Same as<S,E>unfold(S,akka.japi.function.Function<S,java.util.Optional<akka.japi.Pair<S,E>>>)
, but uses an async function to generate the next state-element tuple.
-
failed
public <T> Source<T,NotUsed> failed(java.lang.Throwable cause)
Create aSource
that immediately ends the stream with thecause
failure to every connectedSink
.
-
lazily
public <T,M> Source<T,java.util.concurrent.CompletionStage<M>> lazily(Creator<Source<T,M>> create)
Deprecated.Use 'Source.lazySource' instead. Since 2.6.0.Creates aSource
that is not materialized until there is downstream demand, when the source gets materialized the materialized future is completed with its value, if downstream cancels or fails without any demand thecreate
factory is never called and the materializedCompletionStage
is failed.
-
lazilyAsync
public <T> Source<T,scala.concurrent.Future<NotUsed>> lazilyAsync(Creator<java.util.concurrent.CompletionStage<T>> create)
Deprecated.Use 'Source.lazyCompletionStage' instead. Since 2.6.0.Creates aSource
from supplied future factory that is not called until downstream demand. When source gets materialized the materialized future is completed with the value from the factory. If downstream cancels or fails without any demand the create factory is never called and the materializedFuture
is failed.- See Also:
Source.lazily
-
future
public <T> Source<T,NotUsed> future(scala.concurrent.Future<T> futureElement)
Emits a single value when the given ScalaFuture
is successfully completed and then completes the stream. The stream fails if theFuture
is completed with a failure.Here for Java interoperability, the normal use from Java should be
Source.completionStage
-
never
public <T> Source<T,NotUsed> never()
Never emits any elements, never completes and never fails. This stream could be useful in tests.
-
completionStage
public <T> Source<T,NotUsed> completionStage(java.util.concurrent.CompletionStage<T> completionStage)
Emits a single value when the givenCompletionStage
is successfully completed and then completes the stream. If theCompletionStage
is completed with a failure the stream is failed.
-
completionStageSource
public <T,M> Source<T,java.util.concurrent.CompletionStage<M>> completionStageSource(java.util.concurrent.CompletionStage<Source<T,M>> completionStageSource)
Turn aCompletionStage[Source]
into a source that will emit the values of the source when the future completes successfully. If theCompletionStage
is completed with a failure the stream is failed.
-
lazySingle
public <T> Source<T,NotUsed> lazySingle(Creator<T> create)
Defers invoking thecreate
function to create a single element until there is downstream demand.If the
create
function fails when invoked the stream is failed.Note that asynchronous boundaries (and other operators) in the stream may do pre-fetching which counter acts the laziness and will trigger the factory immediately.
The materialized future
Done
value is completed when thecreate
function has successfully been invoked, if the function throws the future materialized value is failed with that exception. If downstream cancels or fails before the function is invoked the materialized value is failed with aNeverMaterializedException
-
lazyCompletionStage
public <T> Source<T,NotUsed> lazyCompletionStage(Creator<java.util.concurrent.CompletionStage<T>> create)
Defers invoking thecreate
function to create a future element until there is downstream demand.The returned future element will be emitted downstream when it completes, or fail the stream if the future is failed or the
create
function itself fails.Note that asynchronous boundaries (and other operators) in the stream may do pre-fetching which counter acts the laziness and will trigger the factory immediately.
The materialized future
Done
value is completed when thecreate
function has successfully been invoked and the future completes, if the function throws or the future fails the future materialized value is failed with that exception. If downstream cancels or fails before the function is invoked the materialized value is failed with aNeverMaterializedException
-
lazySource
public <T,M> Source<T,java.util.concurrent.CompletionStage<M>> lazySource(Creator<Source<T,M>> create)
Defers invoking thecreate
function to create a future source until there is downstream demand.The returned source will emit downstream and behave just like it was the outer source. Downstream completes when the created source completes and fails when the created source fails.
Note that asynchronous boundaries (and other operators) in the stream may do pre-fetching which counter acts the laziness and will trigger the factory immediately.
The materialized future value is completed with the materialized value of the created source when it has been materialized. If the function throws or the source materialization fails the future materialized value is failed with the thrown exception.
If downstream cancels or fails before the function is invoked the materialized value is failed with a
NeverMaterializedException
-
lazyCompletionStageSource
public <T,M> Source<T,java.util.concurrent.CompletionStage<M>> lazyCompletionStageSource(Creator<java.util.concurrent.CompletionStage<Source<T,M>>> create)
Defers invoking thecreate
function to create a future source until there is downstream demand.The returned future source will emit downstream and behave just like it was the outer source when the
CompletionStage
completes successfully. Downstream completes when the created source completes and fails when the created source fails. If theCompletionStage
or thecreate
function fails the stream is failed.Note that asynchronous boundaries (and other operators) in the stream may do pre-fetching which counter acts the laziness and triggers the factory immediately.
The materialized
CompletionStage
value is completed with the materialized value of the created source when it has been materialized. If the function throws or the source materialization fails the future materialized value is failed with the thrown exception.If downstream cancels or fails before the function is invoked the materialized value is failed with a
NeverMaterializedException
-
asSubscriber
public <T> Source<T,org.reactivestreams.Subscriber<T>> asSubscriber()
Creates aSource
that is materialized as aSubscriber
-
actorRef
public <T> Source<T,ActorRef> actorRef(Function<java.lang.Object,java.util.Optional<CompletionStrategy>> completionMatcher, Function<java.lang.Object,java.util.Optional<java.lang.Throwable>> failureMatcher, int bufferSize, OverflowStrategy overflowStrategy)
Creates aSource
that is materialized as anActorRef
. Messages sent to this actor will be emitted to the stream if there is demand from downstream, otherwise they will be buffered until request for demand is received.Depending on the defined
OverflowStrategy
it might drop elements if there is no space available in the buffer.The strategy
akka.stream.OverflowStrategy.backpressure
is not supported, and an IllegalArgument("Backpressure overflowStrategy not supported") will be thrown if it is passed as argument.The buffer can be disabled by using
bufferSize
of 0 and then received messages are dropped if there is no demand from downstream. WhenbufferSize
is 0 theoverflowStrategy
does not matter.The stream can be completed successfully by sending the actor reference a message that is matched by
completionMatcher
in which case already buffered elements will be signaled before signaling completion.The stream can be completed with failure by sending a message that is matched by
failureMatcher
. The extractedThrowable
will be used to fail the stream. In case the Actor is still draining its internal buffer (after having received a message matched bycompletionMatcher
) before signaling completion and it receives a message matched byfailureMatcher
, the failure will be signaled downstream immediately (instead of the completion signal).Note that terminating the actor without first completing it, either with a success or a failure, will prevent the actor triggering downstream completion and the stream will continue to run even though the source actor is dead. Therefore you should **not** attempt to manually terminate the actor such as with a
PoisonPill
.The actor will be stopped when the stream is completed, failed or canceled from downstream, i.e. you can watch it to get notified when that happens.
See also
akka.stream.scaladsl.Source.queue
.- Parameters:
completionMatcher
- catches the completion message to end the streamfailureMatcher
- catches the failure message to fail the streambufferSize
- The size of the buffer in element countoverflowStrategy
- Strategy that is used when incoming elements cannot fit inside the buffer
-
actorRef
public <T> Source<T,ActorRef> actorRef(int bufferSize, OverflowStrategy overflowStrategy)
Deprecated.Use variant accepting completion and failure matchers. Since 2.6.0.Creates aSource
that is materialized as anActorRef
. Messages sent to this actor will be emitted to the stream if there is demand from downstream, otherwise they will be buffered until request for demand is received.Depending on the defined
OverflowStrategy
it might drop elements if there is no space available in the buffer.The strategy
akka.stream.OverflowStrategy.backpressure
is not supported, and an IllegalArgument("Backpressure overflowStrategy not supported") will be thrown if it is passed as argument.The buffer can be disabled by using
bufferSize
of 0 and then received messages are dropped if there is no demand from downstream. WhenbufferSize
is 0 theoverflowStrategy
does not matter.The stream can be completed successfully by sending the actor reference a
Status.Success
(whose content will be ignored) in which case already buffered elements will be signaled before signaling completion.The stream can be completed successfully by sending the actor reference a
Status.Success
. If the content isakka.stream.CompletionStrategy.immediately
the completion will be signaled immediately, otherwise if the content isakka.stream.CompletionStrategy.draining
(or anything else) already buffered elements will be signaled before signaling completion. SendingPoisonPill
will signal completion immediately but this behavior is deprecated and scheduled to be removed.The stream can be completed with failure by sending a
Status.Failure
to the actor reference. In case the Actor is still draining its internal buffer (after having received aStatus.Success
) before signaling completion and it receives aStatus.Failure
, the failure will be signaled downstream immediately (instead of the completion signal).Note that terminating the actor without first completing it, either with a success or a failure, will prevent the actor triggering downstream completion and the stream will continue to run even though the source actor is dead. Therefore you should **not** attempt to manually terminate the actor such as with a
PoisonPill
.The actor will be stopped when the stream is completed, failed or canceled from downstream, i.e. you can watch it to get notified when that happens.
See also
akka.stream.javadsl.Source.queue
.- Parameters:
bufferSize
- The size of the buffer in element countoverflowStrategy
- Strategy that is used when incoming elements cannot fit inside the buffer
-
actorRefWithBackpressure
public <T> Source<T,ActorRef> actorRefWithBackpressure(java.lang.Object ackMessage, Function<java.lang.Object,java.util.Optional<CompletionStrategy>> completionMatcher, Function<java.lang.Object,java.util.Optional<java.lang.Throwable>> failureMatcher)
Creates aSource
that is materialized as anActorRef
. Messages sent to this actor will be emitted to the stream if there is demand from downstream, and a new message will only be accepted after the previous messages has been consumed and acknowledged back. The stream will complete with failure if a message is sent before the acknowledgement has been replied back.The stream can be completed with failure by sending a message that is matched by
failureMatcher
. The extractedThrowable
will be used to fail the stream. In case the Actor is still draining its internal buffer (after having received a message matched bycompletionMatcher
) before signaling completion and it receives a message matched byfailureMatcher
, the failure will be signaled downstream immediately (instead of the completion signal).The actor will be stopped when the stream is completed, failed or canceled from downstream, i.e. you can watch it to get notified when that happens.
-
actorRefWithAck
public <T> Source<T,ActorRef> actorRefWithAck(java.lang.Object ackMessage, Function<java.lang.Object,java.util.Optional<CompletionStrategy>> completionMatcher, Function<java.lang.Object,java.util.Optional<java.lang.Throwable>> failureMatcher)
Deprecated.Use actorRefWithBackpressure insteadCreates aSource
that is materialized as anActorRef
. Messages sent to this actor will be emitted to the stream if there is demand from downstream, and a new message will only be accepted after the previous messages has been consumed and acknowledged back. The stream will complete with failure if a message is sent before the acknowledgement has been replied back.The stream can be completed with failure by sending a message that is matched by
failureMatcher
. The extractedThrowable
will be used to fail the stream. In case the Actor is still draining its internal buffer (after having received a message matched bycompletionMatcher
) before signaling completion and it receives a message matched byfailureMatcher
, the failure will be signaled downstream immediately (instead of the completion signal).The actor will be stopped when the stream is completed, failed or canceled from downstream, i.e. you can watch it to get notified when that happens.
-
actorRefWithAck
public <T> Source<T,ActorRef> actorRefWithAck(java.lang.Object ackMessage)
Deprecated.Use actorRefWithBackpressure accepting completion and failure matchers. Since 2.6.0.Creates aSource
that is materialized as anActorRef
. Messages sent to this actor will be emitted to the stream if there is demand from downstream, and a new message will only be accepted after the previous messages has been consumed and acknowledged back. The stream will complete with failure if a message is sent before the acknowledgement has been replied back.The stream can be completed successfully by sending the actor reference a
Status.Success
. If the content isakka.stream.CompletionStrategy.immediately
the completion will be signaled immediately, otherwise if the content isakka.stream.CompletionStrategy.draining
(or anything else) already buffered element will be signaled before signaling completion.The stream can be completed with failure by sending a
Status.Failure
to the actor reference. In case the Actor is still draining its internal buffer (after having received aStatus.Success
) before signaling completion and it receives aStatus.Failure
, the failure will be signaled downstream immediately (instead of the completion signal).The actor will be stopped when the stream is completed, failed or canceled from downstream, i.e. you can watch it to get notified when that happens.
-
fromGraph
public <T,M> Source<T,M> fromGraph(Graph<SourceShape<T>,M> g)
A graph with the shape of a source logically is a source, this method makes it so also in type.
-
fromMaterializer
public <T,M> Source<T,java.util.concurrent.CompletionStage<M>> fromMaterializer(java.util.function.BiFunction<Materializer,Attributes,Source<T,M>> factory)
-
setup
public <T,M> Source<T,java.util.concurrent.CompletionStage<M>> setup(java.util.function.BiFunction<ActorMaterializer,Attributes,Source<T,M>> factory)
Deprecated.Use 'fromMaterializer' instead. Since 2.6.0.
-
combine
public <T,U> Source<U,NotUsed> combine(Source<T,?> first, Source<T,?> second, java.util.List<Source<T,?>> rest, Function<java.lang.Integer,? extends Graph<UniformFanInShape<T,U>,NotUsed>> fanInStrategy)
-
combineMat
public <T,U,M1,M2,M> Source<U,M> combineMat(Source<T,M1> first, Source<T,M2> second, Function<java.lang.Integer,? extends Graph<UniformFanInShape<T,U>,NotUsed>> fanInStrategy, Function2<M1,M2,M> matF)
Combines two sources with fan-in strategy likeMerge
orConcat
and returnsSource
with a materialized value.
-
combine
public <T,U,M> Source<U,java.util.List<M>> combine(java.util.List<? extends Graph<SourceShape<T>,M>> sources, Function<java.lang.Integer,Graph<UniformFanInShape<T,U>,NotUsed>> fanInStrategy)
-
zipN
public <T> Source<java.util.List<T>,NotUsed> zipN(java.util.List<Source<T,?>> sources)
Combine the elements of multiple streams into a stream of lists.
-
zipWithN
public <T,O> Source<O,NotUsed> zipWithN(Function<java.util.List<T>,O> zipper, java.util.List<Source<T,?>> sources)
-
queue
public <T> Source<T,BoundedSourceQueue<T>> queue(int bufferSize)
Creates aSource
that is materialized as anBoundedSourceQueue
. You can push elements to the queue and they will be emitted to the stream if there is demand from downstream, otherwise they will be buffered until request for demand is received. The buffer size is passed in as a parameter. Elements in the buffer will be discarded if downstream is terminated.Pushed elements may be dropped if there is no space available in the buffer. Elements will also be dropped if the queue is failed through the materialized
BoundedQueueSource
or theSource
is cancelled by the downstream. An element that was reported to beenqueued
is not guaranteed to be processed by the rest of the stream. If the queue is failed by callingBoundedQueueSource.fail
or the downstream cancels the stream, elements in the buffer are discarded.Acknowledgement of pushed elements is immediate.
akka.stream.BoundedSourceQueue.offer
returnsQueueOfferResult
which is implemented as:QueueOfferResult.enqueued()
element was added to buffer, but may still be discarded later when the queue is failed or cancelledQueueOfferResult.dropped()
element was droppedQueueOfferResult.QueueClosed
the queue was completed withakka.stream.BoundedSourceQueue.complete
QueueOfferResult.Failure
the queue was failed withakka.stream.BoundedSourceQueue.fail
or if the stream failed- Parameters:
bufferSize
- size of the buffer in number of elements
-
queue
public <T> Source<T,SourceQueueWithComplete<T>> queue(int bufferSize, OverflowStrategy overflowStrategy)
Creates aSource
that is materialized as anSourceQueueWithComplete
. You can push elements to the queue and they will be emitted to the stream if there is demand from downstream, otherwise they will be buffered until request for demand is received. Elements in the buffer will be discarded if downstream is terminated.Depending on the defined
OverflowStrategy
it might drop elements if there is no space available in the buffer.Acknowledgement mechanism is available.
akka.stream.javadsl.SourceQueueWithComplete.offer
returnsCompletionStage
which completes withQueueOfferResult.enqueued()
if element was added to buffer or sent downstream. It completes withQueueOfferResult.dropped()
if element was dropped. Can also complete withQueueOfferResult.Failure
- when stream failed orQueueOfferResult.QueueClosed
when downstream is completed.The strategy
akka.stream.OverflowStrategy.backpressure
will not complete lastoffer():CompletionStage
call when buffer is full.Instead of using the strategy
akka.stream.OverflowStrategy.dropNew
it's recommended to useSource.queue(bufferSize)
instead which returns aQueueOfferResult
synchronously.You can watch accessibility of stream with
akka.stream.javadsl.SourceQueueWithComplete.watchCompletion
. It returns a future that completes with success when this operator is completed or fails when stream is failed.The buffer can be disabled by using
bufferSize
of 0 and then received message will wait for downstream demand unless there is another message waiting for downstream demand, in that case offer result will be completed according to the overflow strategy.The materialized SourceQueue may only be used from a single producer.
- Parameters:
bufferSize
- size of buffer in element countoverflowStrategy
- Strategy that is used when incoming elements cannot fit inside the buffer
-
queue
public <T> Source<T,SourceQueueWithComplete<T>> queue(int bufferSize, OverflowStrategy overflowStrategy, int maxConcurrentOffers)
Creates aSource
that is materialized as anSourceQueueWithComplete
. You can push elements to the queue and they will be emitted to the stream if there is demand from downstream, otherwise they will be buffered until request for demand is received. Elements in the buffer will be discarded if downstream is terminated.Depending on the defined
OverflowStrategy
it might drop elements if there is no space available in the buffer.Acknowledgement mechanism is available.
akka.stream.javadsl.SourceQueueWithComplete.offer
returnsCompletionStage
which completes withQueueOfferResult.enqueued()
if element was added to buffer or sent downstream. It completes withQueueOfferResult.dropped()
if element was dropped. Can also complete withQueueOfferResult.Failure
- when stream failed orQueueOfferResult.QueueClosed
when downstream is completed.The strategy
akka.stream.OverflowStrategy.backpressure
will not completemaxConcurrentOffers
number ofoffer():CompletionStage
call when buffer is full.Instead of using the strategy
akka.stream.OverflowStrategy.dropNew
it's recommended to useSource.queue(bufferSize)
instead which returns aQueueOfferResult
synchronously.You can watch accessibility of stream with
akka.stream.javadsl.SourceQueueWithComplete.watchCompletion
. It returns a future that completes with success when this operator is completed or fails when stream is failed.The buffer can be disabled by using
bufferSize
of 0 and then received message will wait for downstream demand unless there is another message waiting for downstream demand, in that case offer result will be completed according to the overflow strategy.The materialized SourceQueue may be used by up to maxConcurrentOffers concurrent producers.
- Parameters:
bufferSize
- size of buffer in element countoverflowStrategy
- Strategy that is used when incoming elements cannot fit inside the buffermaxConcurrentOffers
- maximum number of pending offers when buffer is full, should be greater than 0, not applicable whenOverflowStrategy.dropNew
is used
-
unfoldResource
public <T,S> Source<T,NotUsed> unfoldResource(Creator<S> create, Function<S,java.util.Optional<T>> read, Procedure<S> close)
Start a newSource
from some resource which can be opened, read and closed. Interaction with resource happens in a blocking way.Example:
Source.unfoldResource( () -> new BufferedReader(new FileReader("...")), reader -> reader.readLine(), reader -> reader.close())
You can use the supervision strategy to handle exceptions for
read
function. All exceptions thrown bycreate
orclose
will fail the stream.Restart
supervision strategy will close and create blocking IO again. Default strategy isStop
which means that stream will be terminated on error inread
function by default.You can configure the default dispatcher for this Source by changing the
akka.stream.materializer.blocking-io-dispatcher
or set it for a given Source by usingActorAttributes
.Adheres to the
ActorAttributes.SupervisionStrategy
attribute.- Parameters:
create
- - function that is called on stream start and creates/opens resource.read
- - function that reads data from opened resource. It is called each time backpressure signal is received. Stream calls close and completes whenread
returns an empty Optional.close
- - function that closes resource
-
unfoldResourceAsync
public <T,S> Source<T,NotUsed> unfoldResourceAsync(Creator<java.util.concurrent.CompletionStage<S>> create, Function<S,java.util.concurrent.CompletionStage<java.util.Optional<T>>> read, Function<S,java.util.concurrent.CompletionStage<Done>> close)
Start a newSource
from some resource which can be opened, read and closed. It's similar tounfoldResource
but takes functions that returnCompletionStage
instead of plain values.You can use the supervision strategy to handle exceptions for
read
function or failures of producedFutures
. All exceptions thrown bycreate
orclose
as well as fails of returned futures will fail the stream.Restart
supervision strategy will close and create resource. Default strategy isStop
which means that stream will be terminated on error inread
function (or future) by default.You can configure the default dispatcher for this Source by changing the
akka.stream.materializer.blocking-io-dispatcher
or set it for a given Source by usingActorAttributes
.Adheres to the
ActorAttributes.SupervisionStrategy
attribute.- Parameters:
create
- - function that is called on stream start and creates/opens resource.read
- - function that reads data from opened resource. It is called each time backpressure signal is received. Stream calls close and completes whenCompletionStage
from read function returns an empty Optional.close
- - function that closes resource
-
upcast
public <SuperOut,Out extends SuperOut,Mat> Source<SuperOut,Mat> upcast(Source<Out,Mat> source)
Upcast a stream of elements to a stream of supertypes of that element. Useful in combination with fan-in operators where you do not want to pay the cost of casting each element in amap
.Example:
Source<Apple, NotUsed> apples = Source.single(new Apple()); Source<Orange, NotUsed> oranges = Source.single(new Orange()); Source<Fruit, NotUsed> appleFruits = Source.upcast(apples); Source<Fruit, NotUsed> orangeFruits = Source.upcast(oranges); Source<Fruit, NotUsed> fruits = appleFruits.merge(orangeFruits);
- Returns:
- A source with the supertype as elements
-
mergePrioritizedN
public <T> Source<T,NotUsed> mergePrioritizedN(java.util.List<Pair<Source<T,?>,java.lang.Integer>> sourcesAndPriorities, boolean eagerComplete)
Merge multipleSource
s. Prefer the sources depending on the 'priority' parameters. The provided sources and priorities must have the same size and order.'''emits''' when one of the inputs has an element available, preferring inputs based on the 'priority' parameters if both have elements available
'''backpressures''' when downstream backpressures
'''completes''' when both upstreams complete (This behavior is changeable to completing when any upstream completes by setting
eagerComplete=true
.)'''Cancels when''' downstream cancels
-
-