Class SourceWithContext<Out,Ctx,Mat>

java.lang.Object
org.apache.pekko.stream.GraphDelegate<SourceShape<scala.Tuple2<Out,Ctx>>,Mat>
org.apache.pekko.stream.javadsl.SourceWithContext<Out,Ctx,Mat>
All Implemented Interfaces:
Graph<SourceShape<scala.Tuple2<Out,Ctx>>,Mat>

public final class SourceWithContext<Out,Ctx,Mat> extends org.apache.pekko.stream.GraphDelegate<SourceShape<scala.Tuple2<Out,Ctx>>,Mat>
A source that provides operations which automatically propagate the context of an element. Only a subset of common operations from Source is supported. As an escape hatch you can use via(org.apache.pekko.stream.Graph<org.apache.pekko.stream.FlowShape<org.apache.pekko.japi.Pair<Out, Ctx>, org.apache.pekko.japi.Pair<Out2, Ctx2>>, Mat2>) to manually provide the context propagation for otherwise unsupported operations.

Can be created by calling Source.asSourceWithContext

  • Constructor Details

  • Method Details

    • fromPairs

      public static <Out, CtxOut, Mat> SourceWithContext<Out,CtxOut,Mat> fromPairs(Source<Pair<Out,CtxOut>,Mat> under)
      Creates a SourceWithContext from a regular flow that operates on Pair<data, context> elements.
    • unsafeOptionalDataVia

      public static <SOut, FOut, Ctx, SMat, FMat, Mat> SourceWithContext<Optional<FOut>,Ctx,Mat> unsafeOptionalDataVia(SourceWithContext<Optional<SOut>,Ctx,SMat> source, Flow<SOut,FOut,FMat> viaFlow, Function2<SMat,FMat,Mat> combine)
      Creates a SourceWithContext from an existing base SourceWithContext 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. This flow only works on the data portion of flow and ignores the context so this flow *must* not re-order, drop or emit multiple elements for one incoming element
      combine - How to combine the materialized values of source and viaFlow
      Returns:
      a SourceWithContext with the viaFlow applied onto defined elements of the flow. The output value is contained within an Optional which indicates whether the original source's element had viaFlow applied.
      Since:
      1.1.0
    • via

      public <Out2, Ctx2, Mat2> SourceWithContext<Out2,Ctx2,Mat> via(Graph<FlowShape<Pair<Out,Ctx>,Pair<Out2,Ctx2>>,Mat2> viaFlow)
      Transform this flow by the regular flow. The given flow must support manual context propagation by taking and producing tuples of (data, context).

      It is up to the implementer to ensure the inner flow does not exhibit any behavior that is not expected by the downstream elements, such as reordering. For more background on these requirements see https://pekko.apache.org/docs/pekko/current/stream/stream-context.html.

      This can be used as an escape hatch for operations that are not (yet) provided with automatic context propagation here.

      See Also:
      • pekko.stream.javadsl.Flow.via
    • unsafeDataVia

      public <Out2, Mat2> SourceWithContext<Out2,Ctx,Mat> unsafeDataVia(Graph<FlowShape<Out,Out2>,Mat2> viaFlow)
      Transform this flow by the regular flow. The given flow works on the data portion of the stream and ignores the context.

      The given flow *must* not re-order, drop or emit multiple elements for one incoming element, the sequence of incoming contexts is re-combined with the outgoing elements of the stream. If a flow not fulfilling this requirement is used the stream will not fail but continue running in a corrupt state and re-combine incorrect pairs of elements and contexts or deadlock.

      For more background on these requirements see https://pekko.apache.org/docs/pekko/current/stream/stream-context.html.

    • withAttributes

      public SourceWithContext<Out,Ctx,Mat> withAttributes(Attributes attr)
      Context-preserving variant of pekko.stream.javadsl.Source.withAttributes.

      See Also:
      • pekko.stream.javadsl.Source.withAttributes
    • mapError

      public SourceWithContext<Out,Ctx,Mat> mapError(scala.PartialFunction<Throwable,Throwable> pf)
      Context-preserving variant of pekko.stream.javadsl.Source.mapError.

      See Also:
      • pekko.stream.javadsl.Source.mapError
    • mapMaterializedValue

      public <Mat2> SourceWithContext<Out,Ctx,Mat2> mapMaterializedValue(Function<Mat,Mat2> f)
      Context-preserving variant of pekko.stream.javadsl.Source.mapMaterializedValue.

      See Also:
      • pekko.stream.javadsl.Flow.mapMaterializedValue
    • asSource

      public Source<Pair<Out,Ctx>,Mat> asSource()
      Stops automatic context propagation from here and converts this to a regular stream of a pair of (data, context).
    • collect

      public <Out2> SourceWithContext<Out2,Ctx,Mat> collect(scala.PartialFunction<Out,Out2> pf)
      Context-preserving variant of pekko.stream.javadsl.Source.collect.

      Note, that the context of elements that are filtered out is skipped as well.

      See Also:
      • pekko.stream.javadsl.Source.collect
    • filter

      public SourceWithContext<Out,Ctx,Mat> filter(Predicate<Out> p)
      Context-preserving variant of pekko.stream.javadsl.Source.filter.

      Note, that the context of elements that are filtered out is skipped as well.

      See Also:
      • pekko.stream.javadsl.Source.filter
    • filterNot

      public SourceWithContext<Out,Ctx,Mat> filterNot(Predicate<Out> p)
      Context-preserving variant of pekko.stream.javadsl.Source.filterNot.

      Note, that the context of elements that are filtered out is skipped as well.

      See Also:
      • pekko.stream.javadsl.Source.filterNot
    • grouped

      public SourceWithContext<List<Out>,List<Ctx>,Mat> grouped(int n)
      Context-preserving variant of pekko.stream.javadsl.Source.grouped.

      Each output group will be associated with a Seq of corresponding context elements.

      See Also:
      • pekko.stream.javadsl.Source.grouped
    • map

      public <Out2> SourceWithContext<Out2,Ctx,Mat> map(Function<Out,Out2> f)
      Context-preserving variant of pekko.stream.javadsl.Source.map.

      See Also:
      • pekko.stream.javadsl.Source.map
    • mapAsync

      public <Out2> SourceWithContext<Out2,Ctx,Mat> mapAsync(int parallelism, Function<Out,CompletionStage<Out2>> f)
      Context-preserving variant of pekko.stream.javadsl.Source.mapAsync.

      See Also:
      • pekko.stream.javadsl.Source.mapAsync
    • mapAsyncPartitioned

      public <Out2, P> SourceWithContext<Out2,Ctx,Mat> mapAsyncPartitioned(int parallelism, Function<Out,P> partitioner, Function2<Out,P,CompletionStage<Out2>> f)
      Context-preserving variant of pekko.stream.javadsl.Source.mapAsyncPartitioned.

      Since:
      1.1.0
      See Also:
      • pekko.stream.javadsl.Source.mapAsyncPartitioned
    • mapAsyncPartitionedUnordered

      public <Out2, P> SourceWithContext<Out2,Ctx,Mat> mapAsyncPartitionedUnordered(int parallelism, Function<Out,P> partitioner, Function2<Out,P,CompletionStage<Out2>> f)
      Context-preserving variant of pekko.stream.javadsl.Source.mapAsyncPartitionedUnordered.

      Since:
      1.1.0
      See Also:
      • pekko.stream.javadsl.Source.mapAsyncPartitionedUnordered
    • mapConcat

      public <Out2> SourceWithContext<Out2,Ctx,Mat> mapConcat(Function<Out,? extends Iterable<Out2>> f)
      Context-preserving variant of pekko.stream.javadsl.Source.mapConcat.

      The context of the input element will be associated with each of the output elements calculated from this input element.

      Example:

      def dup(element: String) = Seq(element, element)

      Input:

      ("a", 1) ("b", 2)

      inputElements.mapConcat(dup)

      Output:

      ("a", 1) ("a", 1) ("b", 2) ("b", 2)

      See Also:
      • pekko.stream.javadsl.Source.mapConcat
    • mapContext

      public <Ctx2> SourceWithContext<Out,Ctx2,Mat> mapContext(Function<Ctx,Ctx2> extractContext)
      Apply the given function to each context element (leaving the data elements unchanged).
    • sliding

      public SourceWithContext<List<Out>,List<Ctx>,Mat> sliding(int n, int step)
      Context-preserving variant of pekko.stream.javadsl.Source.sliding.

      Each output group will be associated with a Seq of corresponding context elements.

      See Also:
      • pekko.stream.javadsl.Source.sliding
    • sliding$default$2

      public int sliding$default$2()
    • log

      public SourceWithContext<Out,Ctx,Mat> log(String name, Function<Out,Object> extract, LoggingAdapter log)
      Context-preserving variant of pekko.stream.javadsl.Source.log.

      See Also:
      • pekko.stream.javadsl.Source.log
    • log

      public SourceWithContext<Out,Ctx,Mat> log(String name, Function<Out,Object> extract)
      Context-preserving variant of pekko.stream.javadsl.Flow.log.

      See Also:
      • pekko.stream.javadsl.Flow.log
    • log

      public SourceWithContext<Out,Ctx,Mat> log(String name, LoggingAdapter log)
      Context-preserving variant of pekko.stream.javadsl.Flow.log.

      See Also:
      • pekko.stream.javadsl.Flow.log
    • log

      public SourceWithContext<Out,Ctx,Mat> log(String name)
      Context-preserving variant of pekko.stream.javadsl.Flow.log.

      See Also:
      • pekko.stream.javadsl.Flow.log
    • logWithMarker

      public SourceWithContext<Out,Ctx,Mat> logWithMarker(String name, Function2<Out,Ctx,LogMarker> marker, Function<Out,Object> extract, MarkerLoggingAdapter log)
      Context-preserving variant of pekko.stream.javadsl.Source.logWithMarker.

      See Also:
      • pekko.stream.javadsl.Source.logWithMarker
    • logWithMarker

      public SourceWithContext<Out,Ctx,Mat> logWithMarker(String name, Function2<Out,Ctx,LogMarker> marker, Function<Out,Object> extract)
      Context-preserving variant of pekko.stream.javadsl.Flow.logWithMarker.,

      See Also:
      • pekko.stream.javadsl.Flow.logWithMarker
    • logWithMarker

      public SourceWithContext<Out,Ctx,Mat> logWithMarker(String name, Function2<Out,Ctx,LogMarker> marker, MarkerLoggingAdapter log)
      Context-preserving variant of pekko.stream.javadsl.Flow.logWithMarker.

      See Also:
      • pekko.stream.javadsl.Flow.logWithMarker
    • logWithMarker

      public SourceWithContext<Out,Ctx,Mat> logWithMarker(String name, Function2<Out,Ctx,LogMarker> marker)
      Context-preserving variant of pekko.stream.javadsl.Flow.logWithMarker.

      See Also:
      • pekko.stream.javadsl.Flow.logWithMarker
    • throttle

      public SourceWithContext<Out,Ctx,Mat> throttle(int elements, Duration per)
      Context-preserving variant of pekko.stream.javadsl.Source.throttle.

      See Also:
      • pekko.stream.javadsl.Source.throttle
    • throttle

      public SourceWithContext<Out,Ctx,Mat> throttle(int elements, Duration per, int maximumBurst, ThrottleMode mode)
      Context-preserving variant of pekko.stream.javadsl.Source.throttle.

      See Also:
      • pekko.stream.javadsl.Source.throttle
    • throttle

      public SourceWithContext<Out,Ctx,Mat> throttle(int cost, Duration per, Function<Out,Integer> costCalculation)
      Context-preserving variant of pekko.stream.javadsl.Source.throttle.

      See Also:
      • pekko.stream.javadsl.Source.throttle
    • throttle

      public SourceWithContext<Out,Ctx,Mat> throttle(int cost, Duration per, int maximumBurst, Function<Out,Integer> costCalculation, ThrottleMode mode)
      Context-preserving variant of pekko.stream.javadsl.Source.throttle.

      See Also:
      • pekko.stream.javadsl.Source.throttle
    • to

      public <Mat2> RunnableGraph<Mat> to(Graph<SinkShape<Pair<Out,Ctx>>,Mat2> sink)
      Connect this pekko.stream.javadsl.SourceWithContext to a pekko.stream.javadsl.Sink, concatenating the processing steps of both.
    • toMat

      public <Mat2, Mat3> RunnableGraph<Mat3> toMat(Graph<SinkShape<Pair<Out,Ctx>>,Mat2> sink, Function2<Mat,Mat2,Mat3> combine)
      Connect this pekko.stream.javadsl.SourceWithContext to a pekko.stream.javadsl.Sink, concatenating the processing steps of both.
    • runWith

      public <M> M runWith(Graph<SinkShape<Pair<Out,Ctx>>,M> sink, ClassicActorSystemProvider systemProvider)
      Connect this pekko.stream.javadsl.SourceWithContext to a pekko.stream.javadsl.Sink and run it. The returned value is the materialized value of the Sink.
    • runWith

      public <M> M runWith(Graph<SinkShape<Pair<Out,Ctx>>,M> sink, Materializer materializer)
      Connect this pekko.stream.javadsl.SourceWithContext to a pekko.stream.javadsl.Sink and run it. The returned value is the materialized value of the Sink.

      Prefer the method taking an ActorSystem unless you have special requirements.

    • asScala

      public SourceWithContext<Out,Ctx,Mat> asScala()