Class Source$

java.lang.Object
org.apache.pekko.stream.scaladsl.Source$

public class Source$ extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Source$
    Static reference to the singleton instance of this Scala object.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    actorRef(scala.PartialFunction<Object,CompletionStrategy> completionMatcher, scala.PartialFunction<Object,Throwable> failureMatcher, int bufferSize, OverflowStrategy overflowStrategy)
    Creates a Source that is materialized as an pekko.actor.ActorRef.
    actorRefWithBackpressure(Object ackMessage, scala.PartialFunction<Object,CompletionStrategy> completionMatcher, scala.PartialFunction<Object,Throwable> failureMatcher)
    Creates a Source that is materialized as an pekko.actor.ActorRef.
    <T> Source<T,NotUsed>
    apply(Object array)
    Creates a Source from an array, if the array is empty, the stream is completed immediately, otherwise, every element of the array will be emitted sequentially.
    <T> Source<T,NotUsed>
    apply(scala.collection.immutable.Iterable<T> iterable)
    Helper to create Source from Iterable.
    <T> Source<T,NotUsed>
    apply(scala.collection.immutable.Seq<T> seq)
    Helper to create Source from Seq.
    <T> Source<T,org.reactivestreams.Subscriber<T>>
    Creates a Source that is materialized as a Subscriber
    <T, U> Source<U,NotUsed>
    combine(Source<T,?> first, Source<T,?> second, scala.collection.immutable.Seq<Source<T,?>> rest, scala.Function1<Object,Graph<UniformFanInShape<T,U>,NotUsed>> fanInStrategy)
    Combines several sources with fan-in strategy like Merge or Concat into a single Source.
    <T, U, M> Source<U,scala.collection.immutable.Seq<M>>
    combine(scala.collection.immutable.Seq<Graph<SourceShape<T>,M>> sources, scala.Function1<Object,Graph<UniformFanInShape<T,U>,NotUsed>> fanInStrategy)
    Combines several sources with fan-in strategy like Merge or Concat into a single Source.
    <T, U, M1, M2, M>
    Source<U,M>
    combineMat(Source<T,M1> first, Source<T,M2> second, scala.Function1<Object,Graph<UniformFanInShape<T,U>,NotUsed>> fanInStrategy, scala.Function2<M1,M2,M> matF)
    Combines several sources with fan-in strategy like Merge or Concat into a single Source with a materialized value.
    <T> Source<T,NotUsed>
    completionStage(CompletionStage<T> completionStage)
    Emits a single value when the given CompletionStage is successfully completed and then completes the stream.
    <T> Source<T,NotUsed>
    create(int bufferSize, scala.Function1<BoundedSourceQueue<T>,scala.runtime.BoxedUnit> producer)
    Creates a Source that will immediately execute the provided function producer with a BoundedSourceQueue when materialized.
    <T> Source<T,NotUsed>
    cycle(scala.Function0<scala.collection.Iterator<T>> f)
    Creates Source that will continually produce given elements in specified order.
    <T> Source<T,NotUsed>
    A Source with no elements, i.e.
    <T> Source<T,NotUsed>
    Create a Source that immediately ends the stream with the cause error to every connected Sink.
    <T, M> Source<T,M>
    A graph with the shape of a source logically is a source, this method makes it so also in type.
    <T> Source<T,NotUsed>
    fromIterator(scala.Function0<scala.collection.Iterator<T>> f)
    Helper to create Source from Iterator.
    <T, S extends BaseStream<T, S>>
    Source<T,NotUsed>
    fromJavaStream(scala.Function0<BaseStream<T,S>> stream)
    Creates a source that wraps a Java 8 Stream.
    <T, M> Source<T,scala.concurrent.Future<M>>
    fromMaterializer(scala.Function2<Materializer,Attributes,Source<T,M>> factory)
    Defers the creation of a Source until materialization.
    <T> Source<T,NotUsed>
    fromOption(scala.Option<T> option)
    Create a Source from an Option value, emitting the value if it is defined.
    <T> Source<T,NotUsed>
    fromPublisher(org.reactivestreams.Publisher<T> publisher)
    Helper to create Source from Publisher.
    <T> Source<T,NotUsed>
    future(scala.concurrent.Future<T> futureElement)
    Emits a single value when the given Future is successfully completed and then completes the stream.
    <T, M> Source<T,scala.concurrent.Future<M>>
    futureSource(scala.concurrent.Future<Source<T,M>> futureSource)
    Turn a Future[Source] into a source that will emit the values of the source when the future completes successfully.
    <T> Source<T,NotUsed>
    items(Object... items)
    Create a Source from the given elements.
    <T> Source<T,NotUsed>
    items(scala.collection.immutable.Seq<T> items)
    Create a Source from the given elements.
    <T> Source<T,NotUsed>
    iterate(T seed, scala.Function1<T,Object> p, scala.Function1<T,T> f)
    Creates a sequential Source by iterating with the given predicate and function, starting with the given seed value.
    <T> Source<T,NotUsed>
    lazyFuture(scala.Function0<scala.concurrent.Future<T>> create)
    Defers invoking the create function to create a future element until there is downstream demand.
    <T, M> Source<T,scala.concurrent.Future<M>>
    lazyFutureSource(scala.Function0<scala.concurrent.Future<Source<T,M>>> create)
    Defers invoking the create function to create a future source until there is downstream demand.
    <T> Source<T,NotUsed>
    lazySingle(scala.Function0<T> create)
    Defers invoking the create function to create a single element until there is downstream demand.
    <T, M> Source<T,scala.concurrent.Future<M>>
    lazySource(scala.Function0<Source<T,M>> create)
    Defers invoking the create function to create a future source until there is downstream demand.
    <T> Source<T,scala.concurrent.Promise<scala.Option<T>>>
    Create a Source which materializes a Promise which controls what element will be emitted by the Source.
    <T> Source<T,NotUsed>
    mergePrioritizedN(scala.collection.immutable.Seq<scala.Tuple2<Source<T,?>,Object>> sourcesAndPriorities, boolean eagerComplete)
    Merge multiple Sources.
    <T> Source<T,NotUsed>
    Never emits any elements, never completes and never fails.
    <SOut, FOut, SMat, FMat, Mat>
    Source<scala.Option<FOut>,Mat>
    optionalVia(Source<scala.Option<SOut>,SMat> source, Flow<SOut,FOut,FMat> viaFlow, scala.Function2<SMat,FMat,Mat> combine)
    Creates a Source from an existing base Source outputting an optional element and applying an additional viaFlow only if the element in the stream is defined.
    queue(int bufferSize)
    Creates a Source that is materialized as an pekko.stream.BoundedSourceQueue.
    queue(int bufferSize, OverflowStrategy overflowStrategy)
    Creates a Source that is materialized as an pekko.stream.scaladsl.SourceQueueWithComplete.
    queue(int bufferSize, OverflowStrategy overflowStrategy, int maxConcurrentOffers)
    Creates a Source that is materialized as an pekko.stream.scaladsl.SourceQueueWithComplete.
    <T> Source<T,NotUsed>
    repeat(T element)
    Create a Source that will continually emit the given element.
    <T> SourceShape<T>
    shape(String name)
    INTERNAL API
    <T> Source<T,NotUsed>
    single(T element)
    Create a Source with one element.
    tick(scala.concurrent.duration.FiniteDuration initialDelay, scala.concurrent.duration.FiniteDuration interval, T tick)
    Elements are emitted periodically with the specified interval.
    <S, E> Source<E,NotUsed>
    unfold(S s, scala.Function1<S,scala.Option<scala.Tuple2<S,E>>> f)
    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.
    <S, E> Source<E,NotUsed>
    unfoldAsync(S s, scala.Function1<S,scala.concurrent.Future<scala.Option<scala.Tuple2<S,E>>>> f)
    Same as <S,E>unfold(S,scala.Function1<S,scala.Option<scala.Tuple2<S,E>>>), but uses an async function to generate the next state-element tuple.
    <T, R> Source<T,NotUsed>
    unfoldResource(scala.Function0<R> create, scala.Function1<R,scala.Option<T>> read, scala.Function1<R,scala.runtime.BoxedUnit> close)
    Start a new Source from some resource which can be opened, read and closed.
    <T, R> Source<T,NotUsed>
    unfoldResourceAsync(scala.Function0<scala.concurrent.Future<R>> create, scala.Function1<R,scala.concurrent.Future<scala.Option<T>>> read, scala.Function1<R,scala.concurrent.Future<Done>> close)
    Start a new Source from some resource which can be opened, read and closed.
    <T> Source<scala.collection.immutable.Seq<T>,NotUsed>
    zipN(scala.collection.immutable.Seq<Source<T,?>> sources)
    Combine the elements of multiple streams into a stream of sequences.
    <T, O> Source<O,NotUsed>
    zipWithN(scala.Function1<scala.collection.immutable.Seq<T>,O> zipper, scala.collection.immutable.Seq<Source<T,?>> sources)
    Combine the elements of multiple streams into a stream of sequences using a combiner function.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MODULE$

      public static final Source$ MODULE$
      Static reference to the singleton instance of this Scala object.
  • Constructor Details

    • Source$

      public Source$()
  • Method Details

    • items

      public <T> Source<T,NotUsed> items(Object... items)
      Create a Source from the given elements.

      Since:
      1.3.0
    • shape

      public <T> SourceShape<T> shape(String name)
      INTERNAL API
    • fromPublisher

      public <T> Source<T,NotUsed> fromPublisher(org.reactivestreams.Publisher<T> publisher)
      Helper to create Source from Publisher.

      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 <T> Source<T,NotUsed> fromIterator(scala.Function0<scala.collection.Iterator<T>> f)
      Helper to create Source from Iterator. Example usage: Source.fromIterator(() => Iterator.from(0))

      Start a new Source from the given function that produces an Iterator. 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.

    • fromJavaStream

      public <T, S extends BaseStream<T, S>> Source<T,NotUsed> fromJavaStream(scala.Function0<BaseStream<T,S>> stream)
      Creates a source that wraps a Java 8 Stream. 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 <T> Source<T,NotUsed> cycle(scala.Function0<scala.collection.Iterator<T>> f)
      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.

    • optionalVia

      public <SOut, FOut, SMat, FMat, Mat> Source<scala.Option<FOut>,Mat> optionalVia(Source<scala.Option<SOut>,SMat> source, Flow<SOut,FOut,FMat> viaFlow, scala.Function2<SMat,FMat,Mat> combine)
      Creates a Source from an existing base Source outputting an optional element and applying an additional viaFlow only if the element in the stream is defined.

      '''Emits when''' the provided viaFlow runs with defined elements

      '''Backpressures when''' the viaFlow runs for the defined elements and downstream backpressures

      '''Completes when''' upstream completes

      '''Cancels when''' downstream cancels

      Parameters:
      source - The base source that outputs an optional element
      viaFlow - The flow that gets used if the optional element in is defined.
      combine - How to combine the materialized values of source and viaFlow
      Returns:
      a Source with the viaFlow applied onto defined elements of the flow. The output value is contained within an Option which indicates whether the original source's element had viaFlow applied.
      Since:
      1.1.0
    • 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,scala.concurrent.Future<M>> fromMaterializer(scala.Function2<Materializer,Attributes,Source<T,M>> factory)
      Defers the creation of a Source until materialization. The factory function exposes Materializer which is going to be used during materialization and Attributes of the Source returned by this method.
    • apply

      public <T> Source<T,NotUsed> apply(scala.collection.immutable.Iterable<T> iterable)
      Helper to create Source from Iterable. Example usage: Source(Set(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.

      See Also:
    • apply

      public <T> Source<T,NotUsed> apply(scala.collection.immutable.Seq<T> seq)
      Helper to create Source from Seq. Example usage: Source(Seq(1,2,3))

      Starts a new Source from the given Seq. 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.

      Since:
      2.0.0
      See Also:
    • apply

      public <T> Source<T,NotUsed> apply(Object array)
      Creates a Source from an array, if the array is empty, the stream is completed immediately, otherwise, every element of the array will be emitted sequentially.

      Since:
      1.3.0
    • tick

      public <T> Source<T,Cancellable> tick(scala.concurrent.duration.FiniteDuration initialDelay, scala.concurrent.duration.FiniteDuration interval, T 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 a Source with one element. Every connected Sink of this stream will see an individual stream consisting of one element.
    • items

      public <T> Source<T,NotUsed> items(scala.collection.immutable.Seq<T> items)
      Create a Source from the given elements.

      Since:
      1.3.0
    • fromOption

      public <T> Source<T,NotUsed> fromOption(scala.Option<T> option)
      Create a Source from an Option value, emitting the value if it is defined.

      Since:
      1.3.0
    • repeat

      public <T> Source<T,NotUsed> repeat(T element)
      Create a Source that will continually emit the given element.
    • unfold

      public <S, E> Source<E,NotUsed> unfold(S s, scala.Function1<S,scala.Option<scala.Tuple2<S,E>>> f)
      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)
         }
       
    • unfoldAsync

      public <S, E> Source<E,NotUsed> unfoldAsync(S s, scala.Function1<S,scala.concurrent.Future<scala.Option<scala.Tuple2<S,E>>>> f)
      Same as <S,E>unfold(S,scala.Function1<S,scala.Option<scala.Tuple2<S,E>>>), 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)
          }
         }
       
    • iterate

      public <T> Source<T,NotUsed> iterate(T seed, scala.Function1<T,Object> p, scala.Function1<T,T> f)
      Creates a sequential Source by iterating with the given predicate and function, starting with the given seed value. If the predicate returns false for the seed, the Source completes with empty.

      Since:
      1.1.0
      See Also:
    • empty

      public <T> Source<T,NotUsed> empty()
      A Source with no elements, i.e. an empty stream that is completed immediately for every connected Sink.
    • maybe

      public <T> Source<T,scala.concurrent.Promise<scala.Option<T>>> maybe()
      Create a Source which materializes a 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 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 None.
    • failed

      public <T> Source<T,NotUsed> failed(Throwable cause)
      Create a Source that immediately ends the stream with the cause error to every connected Sink.
    • future

      public <T> Source<T,NotUsed> future(scala.concurrent.Future<T> futureElement)
      Emits a single value when the given Future is successfully completed and then completes the stream. The stream fails if the Future is completed with a failure.
    • 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(CompletionStage<T> completionStage)
      Emits a single value when the given CompletionStage is successfully completed and then completes the stream. If the CompletionStage is completed with a failure the stream is failed.

      Here for Java interoperability, the normal use from Scala should be Source.future

    • futureSource

      public <T, M> Source<T,scala.concurrent.Future<M>> futureSource(scala.concurrent.Future<Source<T,M>> futureSource)
      Turn a Future[Source] into a source that will emit the values of the source when the future completes successfully. If the Future is completed with a failure the stream is failed.
    • lazySingle

      public <T> Source<T,NotUsed> lazySingle(scala.Function0<T> create)
      Defers invoking the create 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.

    • lazyFuture

      public <T> Source<T,NotUsed> lazyFuture(scala.Function0<scala.concurrent.Future<T>> create)
      Defers invoking the create 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.

    • lazySource

      public <T, M> Source<T,scala.concurrent.Future<M>> lazySource(scala.Function0<Source<T,M>> create)
      Defers invoking the create 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 pekko.stream.NeverMaterializedException

    • lazyFutureSource

      public <T, M> Source<T,scala.concurrent.Future<M>> lazyFutureSource(scala.Function0<scala.concurrent.Future<Source<T,M>>> create)
      Defers invoking the create 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 future completes successfully. Downstream completes when the created source completes and fails when the created source fails. If the future or the create 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 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 pekko.stream.NeverMaterializedException

    • asSubscriber

      public <T> Source<T,org.reactivestreams.Subscriber<T>> asSubscriber()
      Creates a Source that is materialized as a Subscriber
    • actorRef

      public <T> Source<T,ActorRef> actorRef(scala.PartialFunction<Object,CompletionStrategy> completionMatcher, scala.PartialFunction<Object,Throwable> failureMatcher, int bufferSize, OverflowStrategy overflowStrategy)
      Creates a Source that is materialized as an pekko.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 pekko.stream.OverflowStrategy it might drop elements if there is no space available in the buffer.

      The strategy pekko.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.

      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 extracted Throwable will be used to fail the stream. In case the Actor is still draining its internal buffer (after having received a message matched by completionMatcher) before signaling completion and it receives a message matched by failureMatcher, 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 pekko.actor.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 pekko.stream.scaladsl.Source.queue.

      Parameters:
      completionMatcher - catches the completion message to end the stream
      failureMatcher - catches the failure message to fail the stream
      bufferSize - The size of the buffer in element count
      overflowStrategy - Strategy that is used when incoming elements cannot fit inside the buffer
    • actorRefWithBackpressure

      public <T> Source<T,ActorRef> actorRefWithBackpressure(Object ackMessage, scala.PartialFunction<Object,CompletionStrategy> completionMatcher, scala.PartialFunction<Object,Throwable> failureMatcher)
      Creates a Source that is materialized as an pekko.actor.ActorRef. 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 extracted Throwable will be used to fail the stream. In case the Actor is still draining its internal buffer (after having received a message matched by completionMatcher) before signaling completion and it receives a message matched by failureMatcher, 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.

    • combine

      public <T, U> Source<U,NotUsed> combine(Source<T,?> first, Source<T,?> second, scala.collection.immutable.Seq<Source<T,?>> rest, scala.Function1<Object,Graph<UniformFanInShape<T,U>,NotUsed>> fanInStrategy)
      Combines several sources with fan-in strategy like Merge or Concat into a single Source.
    • combine

      public <T, U, M> Source<U,scala.collection.immutable.Seq<M>> combine(scala.collection.immutable.Seq<Graph<SourceShape<T>,M>> sources, scala.Function1<Object,Graph<UniformFanInShape<T,U>,NotUsed>> fanInStrategy)
      Combines several sources with fan-in strategy like Merge or Concat into a single Source.
      Since:
      1.1.0
    • combineMat

      public <T, U, M1, M2, M> Source<U,M> combineMat(Source<T,M1> first, Source<T,M2> second, scala.Function1<Object,Graph<UniformFanInShape<T,U>,NotUsed>> fanInStrategy, scala.Function2<M1,M2,M> matF)
      Combines several sources with fan-in strategy like Merge or Concat into a single Source with a materialized value.
    • zipN

      public <T> Source<scala.collection.immutable.Seq<T>,NotUsed> zipN(scala.collection.immutable.Seq<Source<T,?>> sources)
      Combine the elements of multiple streams into a stream of sequences.
    • zipWithN

      public <T, O> Source<O,NotUsed> zipWithN(scala.Function1<scala.collection.immutable.Seq<T>,O> zipper, scala.collection.immutable.Seq<Source<T,?>> sources)
      Combine the elements of multiple streams into a stream of sequences using a combiner function.
    • queue

      public <T> Source<T,BoundedSourceQueue<T>> queue(int bufferSize)
      Creates a Source that is materialized as an pekko.stream.BoundedSourceQueue. 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 the Source is cancelled by the downstream. An element that was reported to be enqueued is not guaranteed to be processed by the rest of the stream. If the queue is failed by calling BoundedQueueSource.fail or the downstream cancels the stream, elements in the buffer are discarded.

      Acknowledgement of pushed elements is immediate. pekko.stream.BoundedSourceQueue.offer returns pekko.stream.QueueOfferResult which is implemented as:

      QueueOfferResult.Enqueued element was added to buffer, but may still be discarded later when the queue is failed or cancelled QueueOfferResult.Dropped element was dropped QueueOfferResult.QueueComplete the queue was completed with pekko.stream.BoundedSourceQueue.complete QueueOfferResult.Failure the queue was failed with pekko.stream.BoundedSourceQueue.fail or if the stream failed

      Parameters:
      bufferSize - size of the buffer in number of elements
    • create

      public <T> Source<T,NotUsed> create(int bufferSize, scala.Function1<BoundedSourceQueue<T>,scala.runtime.BoxedUnit> producer)
      Creates a Source that will immediately execute the provided function producer with a BoundedSourceQueue when materialized. This allows defining element production logic at Source creation time.

      The function producer can push elements to the stream using the provided queue. The queue behaves the same as in Source.queue:
      - Elements are emitted when there is downstream demand, buffered otherwise
      - Elements are dropped if the buffer is full
      - Buffered elements are discarded if downstream terminates
      You should never block the producer thread, as it will block the stream from processing elements. If the function producer throws an exception, the queue will be failed and the exception will be propagated to the stream.

      Example usage:

      
       Source.create[Int](10) { queue =>
         // This code is executed when the source is materialized
         queue.offer(1)
         queue.offer(2)
         queue.offer(3)
         queue.complete()
       }
       

      Parameters:
      bufferSize - the size of the buffer (number of elements)
      producer - function that receives the queue and defines how to produce data
      Returns:
      a Source that emits elements pushed to the queue
      Since:
      1.2.0
    • queue

      public <T> Source<T,SourceQueueWithComplete<T>> queue(int bufferSize, OverflowStrategy overflowStrategy)
      Creates a Source that is materialized as an pekko.stream.scaladsl.SourceQueueWithComplete. 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 pekko.stream.OverflowStrategy it might drop elements if there is no space available in the buffer.

      Acknowledgement mechanism is available. pekko.stream.scaladsl.SourceQueueWithComplete.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 pekko.stream.OverflowStrategy.backpressure will not complete last offer():Future call when buffer is full.

      To drop the newest elements, it's recommended combining with Source.queue(bufferSize) which returns a QueueOfferResult synchronously.

      You can watch accessibility of stream with pekko.stream.scaladsl.SourceQueueWithComplete.watchCompletion. It returns future that completes with success when the operator is completed or fails when the 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 count
      overflowStrategy - 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 a Source that is materialized as an pekko.stream.scaladsl.SourceQueueWithComplete. 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 pekko.stream.OverflowStrategy it might drop elements if there is no space available in the buffer.

      Acknowledgement mechanism is available. pekko.stream.scaladsl.SourceQueueWithComplete.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 pekko.stream.OverflowStrategy.backpressure will not complete maxConcurrentOffers number of offer():Future call when buffer is full.

      To drop the newest elements, it's recommended combining with Source.queue(bufferSize) which returns a QueueOfferResult synchronously.

      You can watch accessibility of stream with pekko.stream.scaladsl.SourceQueueWithComplete.watchCompletion. It returns future that completes with success when the operator is completed or fails when the 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 count
      overflowStrategy - Strategy that is used when incoming elements cannot fit inside the buffer
      maxConcurrentOffers - maximum number of pending offers when buffer is full, should be greater than 0.
    • unfoldResource

      public <T, R> Source<T,NotUsed> unfoldResource(scala.Function0<R> create, scala.Function1<R,scala.Option<T>> read, scala.Function1<R,scala.runtime.BoxedUnit> close)
      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 pekko.stream.materializer.blocking-io-dispatcher or set it for a given Source by using ActorAttributes.

      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 when read returns None.
      close - - function that closes resource
    • unfoldResourceAsync

      public <T, R> Source<T,NotUsed> unfoldResourceAsync(scala.Function0<scala.concurrent.Future<R>> create, scala.Function1<R,scala.concurrent.Future<scala.Option<T>>> read, scala.Function1<R,scala.concurrent.Future<Done>> close)
      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 pekko.stream.materializer.blocking-io-dispatcher or set it for a given Source by using ActorAttributes.

      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 when Future from read function returns None.
      close - - function that closes resource
    • mergePrioritizedN

      public <T> Source<T,NotUsed> mergePrioritizedN(scala.collection.immutable.Seq<scala.Tuple2<Source<T,?>,Object>> sourcesAndPriorities, boolean eagerComplete)
      Merge multiple Sources. 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