Creates a Source
that is materialized as an akka.actor.ActorRef.
Creates a Source
that is materialized as an akka.actor.ActorRef.
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 akka.stream.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. When bufferSize
is 0 the overflowStrategy
does not matter. An async boundary is added after
this Source; as such, it is never safe to assume the downstream will always generate demand.
The stream can be completed successfully by sending the actor reference a akka.actor.Status.Success (whose content will be ignored) in which case already buffered elements will be signaled before signaling completion, or by sending akka.actor.PoisonPill in which case completion will be signaled immediately.
The stream can be completed with failure by sending a akka.actor.Status.Failure to the actor reference. In case the Actor is still draining its internal buffer (after having received a akka.actor.Status.Success) before signaling completion and it receives a akka.actor.Status.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.
See also akka.stream.scaladsl.Source.queue.
The size of the buffer in element count
Strategy that is used when incoming elements cannot fit inside the buffer
Helper to create Source from Iterable
.
Helper to create Source from Iterable
.
Example usage: Source(Seq(1,2,3))
Starts a new Source
from the given Iterable
. 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.
Creates a Source
that is materialized as a org.reactivestreams.Subscriber
Combines several sources with fan-in strategy like Merge
or Concat
and returns Source
.
Combines two sources with fan-in strategy like Merge
or Concat
and returns Source
with a materialized value.
Creates Source that will continually produce given elements in specified order.
Creates Source that will continually produce given elements in specified order.
Starts a new 'cycled' Source
from the given elements. The producer stream of elements
will continue infinitely by repeating the sequence of elements provided by function parameter.
A Source
with no elements, i.e.
A Source
with no elements, i.e. an empty stream that is completed immediately for every connected Sink
.
Create a Source
that immediately ends the stream with the cause
error to every connected Sink
.
Starts a new Source
from the given Future
.
Starts a new Source
from the given Future
. The stream will consist of
one element when the Future
is completed with a successful value, which
may happen before or after materializing the Flow
.
The stream terminates with a failure if the Future
is completed with a failure.
Starts a new Source
from the given Future
.
Starts a new Source
from the given Future
. The stream will consist of
one element when the Future
is completed with a successful value, which
may happen before or after materializing the Flow
.
The stream terminates with a failure if the Future
is completed with a failure.
Streams the elements of the given future source once it successfully completes.
Streams the elements of the given future source once it successfully completes.
If the Future fails the stream is failed with the exception from the future. If downstream cancels before the
stream completes the materialized Future
will be failed with a StreamDetachedException
A graph with the shape of a source logically is a source, this method makes it so also in type.
Helper to create Source from Iterator
.
Helper to create Source from Iterator
.
Example usage: Source.fromIterator(() => Iterator.from(0))
Start a new Source
from the given function that produces anIterator.
The produced stream of elements will continue until the iterator runs empty
or fails during evaluation of the next()
method.
Elements are pulled out of the iterator in accordance with the demand coming
from the downstream transformation steps.
Helper to create Source from Publisher
.
Helper to create Source from Publisher
.
Construct a transformation starting with given publisher. The transformation steps are executed by a series of org.reactivestreams.Processor instances that mediate the flow of elements downstream and the propagation of back-pressure upstream.
Streams the elements of an asynchronous source once its given completion
operator completes.
Streams the elements of an asynchronous source once its given completion
operator completes.
If the CompletionStage fails the stream is failed with the exception from the future.
If downstream cancels before the stream completes the materialized Future
will be failed
with a StreamDetachedException
Creates a Source
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 the
create factory is never called and the materialized Future
is failed.
Creates a Source
from supplied future factory that is not called until downstream demand.
Creates a Source
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 materialized Future
is failed.
Create a Source
which materializes a scala.concurrent.Promise which controls what element
will be emitted by the Source.
Create a Source
which materializes a scala.concurrent.Promise which controls what element
will be emitted by the Source.
If the materialized promise is completed with a Some, that value will be produced downstream,
followed by completion.
If the materialized promise is completed with a None, no value will be produced downstream and completion will
be signalled immediately.
If the materialized promise is completed with a failure, then the returned source will terminate with that error.
If the downstream of this source cancels before the promise has been completed, then the promise will be completed
with None.
Creates a Source
that is materialized as an akka.stream.scaladsl.SourceQueue.
Creates a Source
that is materialized as an akka.stream.scaladsl.SourceQueue.
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 akka.stream.OverflowStrategy it might drop elements if there is no space available in the buffer.
Acknowledgement mechanism is available.
akka.stream.scaladsl.SourceQueue.offer returns Future[QueueOfferResult]
which completes with
QueueOfferResult.Enqueued
if element was added to buffer or sent downstream. It completes with
QueueOfferResult.Dropped
if element was dropped. Can also complete with QueueOfferResult.Failure
-
when stream failed or QueueOfferResult.QueueClosed
when downstream is completed.
The strategy akka.stream.OverflowStrategy.backpressure will not complete last offer():Future
call when buffer is full.
You can watch accessibility of stream with akka.stream.scaladsl.SourceQueue.watchCompletion. It returns future that completes with success when stream is completed or fail 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.
size of buffer in element count
Strategy that is used when incoming elements cannot fit inside the buffer
Create a Source
that will continually emit the given element.
INTERNAL API
Create a Source
with one element.
Create a Source
with one element.
Every connected Sink
of this stream will see an individual stream consisting of one element.
Elements are emitted periodically with the specified interval.
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.
Create a Source
that will unfold a value of type S
into
a pair of the next state S
and output elements of type E
.
Create a Source
that will unfold a value of type S
into
a pair of the next state S
and output elements of type E
.
For example, all the Fibonacci numbers under 10M:
Source.unfold(0 → 1) { case (a, _) if a > 10000000 ⇒ None case (a, b) ⇒ Some((b → (a + b)) → a) }
Same as unfold, but uses an async function to generate the next state-element tuple.
Same as unfold, but uses an async function to generate the next state-element tuple.
async fibonacci example:
Source.unfoldAsync(0 → 1) { case (a, _) if a > 10000000 ⇒ Future.successful(None) case (a, b) ⇒ Future{ Thread.sleep(1000) Some((b → (a + b)) → a) } }
Start a new Source
from some resource which can be opened, read and closed.
Start a new Source
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 => Option(reader.readLine()), reader => reader.close())
You can use the supervision strategy to handle exceptions for read
function. All exceptions thrown by create
or close
will fail the stream.
Restart
supervision strategy will close and create blocking IO again. Default strategy is Stop
which means
that stream will be terminated on error in read
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 using ActorAttributes.
Adheres to the ActorAttributes.SupervisionStrategy attribute.
- function that is called on stream start and creates/opens resource.
- function that reads data from opened resource. It is called each time backpressure signal
is received. Stream calls close and completes when read
returns None.
- function that closes resource
Start a new Source
from some resource which can be opened, read and closed.
Start a new Source
from some resource which can be opened, read and closed.
It's similar to unfoldResource
but takes functions that return Futures
instead of plain values.
You can use the supervision strategy to handle exceptions for read
function or failures of produced Futures
.
All exceptions thrown by create
or close
as well as fails of returned futures will fail the stream.
Restart
supervision strategy will close and create resource. Default strategy is Stop
which means
that stream will be terminated on error in read
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 using ActorAttributes.
Adheres to the ActorAttributes.SupervisionStrategy attribute.
- function that is called on stream start and creates/opens resource.
- function that reads data from opened resource. It is called each time backpressure signal
is received. Stream calls close and completes when Future
from read function returns None.
- function that closes resource
Combine the elements of multiple streams into a stream of sequences.
Creates a Source
that is materialized to an akka.actor.ActorRef which points to an Actor
created according to the passed in akka.actor.Props.
Creates a Source
that is materialized to an akka.actor.ActorRef which points to an Actor
created according to the passed in akka.actor.Props. Actor created by the props
must
be akka.stream.actor.ActorPublisher.
(Since version 2.5.0) Use akka.stream.stage.GraphStage
and fromGraph
instead, it allows for all operations an Actor would and is more type-safe as well as guaranteed to be ReactiveStreams compliant.