2014-09-03 21:54:18 +02:00
|
|
|
/**
|
2015-04-16 02:24:01 +02:00
|
|
|
* Copyright (C) 2014-2015 Typesafe Inc. <http://www.typesafe.com>
|
2014-09-03 21:54:18 +02:00
|
|
|
*/
|
2014-10-27 14:35:41 +01:00
|
|
|
package akka.stream.scaladsl
|
2014-09-03 21:54:18 +02:00
|
|
|
|
2015-03-06 12:22:14 +01:00
|
|
|
import akka.stream.javadsl
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.actor.{ ActorRef, Props }
|
2015-04-16 02:24:01 +02:00
|
|
|
import akka.stream._
|
2015-06-09 00:05:56 -04:00
|
|
|
import akka.stream.impl.Stages.{ MapAsyncUnordered, DefaultAttributes }
|
2015-04-16 02:24:01 +02:00
|
|
|
import akka.stream.impl.StreamLayout.Module
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.stream.impl._
|
2015-06-23 17:32:55 +02:00
|
|
|
import akka.stream.Attributes._
|
2015-06-14 03:12:30 -04:00
|
|
|
import akka.stream.stage.{ TerminationDirective, Directive, Context, PushStage, SyncDirective }
|
2015-01-28 14:19:50 +01:00
|
|
|
import org.reactivestreams.{ Publisher, Subscriber }
|
2015-04-16 02:24:01 +02:00
|
|
|
|
2015-06-09 00:05:56 -04:00
|
|
|
import scala.concurrent.{ ExecutionContext, Future, Promise }
|
2015-04-16 02:24:01 +02:00
|
|
|
import scala.util.{ Failure, Success, Try }
|
2014-09-03 21:54:18 +02:00
|
|
|
|
|
|
|
|
/**
|
2014-10-02 17:32:08 +02:00
|
|
|
* A `Sink` is a set of stream processing steps that has one open input and an attached output.
|
|
|
|
|
* Can be used as a `Subscriber`
|
2014-09-03 21:54:18 +02:00
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
final class Sink[-In, +Mat](private[stream] override val module: Module)
|
|
|
|
|
extends Graph[SinkShape[In], Mat] {
|
|
|
|
|
|
|
|
|
|
override val shape: SinkShape[In] = module.shape.asInstanceOf[SinkShape[In]]
|
2014-10-30 14:58:44 +01:00
|
|
|
|
2014-10-02 13:34:27 +02:00
|
|
|
/**
|
2014-10-17 14:05:50 +02:00
|
|
|
* Connect this `Sink` to a `Source` and run it. The returned value is the materialized value
|
2015-03-06 10:23:26 +01:00
|
|
|
* of the `Source`, e.g. the `Subscriber` of a [[Source#subscriber]].
|
2014-10-02 13:34:27 +02:00
|
|
|
*/
|
2015-06-23 18:28:53 +02:00
|
|
|
def runWith[Mat2](source: Graph[SourceShape[In], Mat2])(implicit materializer: Materializer): Mat2 =
|
2015-04-24 12:14:04 +02:00
|
|
|
Source.wrap(source).to(this).run()
|
2015-01-28 14:19:50 +01:00
|
|
|
|
2015-05-05 10:29:41 +02:00
|
|
|
def mapMaterializedValue[Mat2](f: Mat ⇒ Mat2): Sink[In, Mat2] =
|
2015-01-28 14:19:50 +01:00
|
|
|
new Sink(module.transformMaterializedValue(f.asInstanceOf[Any ⇒ Any]))
|
2014-10-10 10:39:29 +02:00
|
|
|
|
2015-06-23 17:32:55 +02:00
|
|
|
override def withAttributes(attr: Attributes): Sink[In, Mat] =
|
2015-07-06 22:00:21 +02:00
|
|
|
new Sink(module.withAttributes(attr).nest())
|
2015-03-05 12:21:17 +01:00
|
|
|
|
2015-06-23 17:32:55 +02:00
|
|
|
override def named(name: String): Sink[In, Mat] = withAttributes(Attributes.name(name))
|
2015-03-06 12:22:14 +01:00
|
|
|
|
|
|
|
|
/** Converts this Scala DSL element to it's Java DSL counterpart. */
|
|
|
|
|
def asJava: javadsl.Sink[In, Mat] = new javadsl.Sink(this)
|
2014-09-03 21:54:18 +02:00
|
|
|
}
|
2014-10-08 15:15:46 +02:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
object Sink extends SinkApply {
|
|
|
|
|
|
2015-04-16 02:24:01 +02:00
|
|
|
/** INTERNAL API */
|
2015-06-13 16:28:38 -04:00
|
|
|
private[stream] def shape[T](name: String): SinkShape[T] = SinkShape(Inlet(name + ".in"))
|
2015-01-28 14:19:50 +01:00
|
|
|
|
2014-10-08 15:15:46 +02:00
|
|
|
/**
|
2015-03-04 15:22:33 +01:00
|
|
|
* A graph with the shape of a sink logically is a sink, this method makes
|
2015-01-28 14:19:50 +01:00
|
|
|
* it so also in type.
|
2014-10-08 15:15:46 +02:00
|
|
|
*/
|
2015-06-06 17:17:23 +02:00
|
|
|
def wrap[T, M](g: Graph[SinkShape[T], M]): Sink[T, M] =
|
|
|
|
|
g match {
|
|
|
|
|
case s: Sink[T, M] ⇒ s
|
|
|
|
|
case other ⇒ new Sink(other.module)
|
|
|
|
|
}
|
2014-10-10 10:39:29 +02:00
|
|
|
|
|
|
|
|
/**
|
2015-01-28 14:19:50 +01:00
|
|
|
* Helper to create [[Sink]] from `Subscriber`.
|
2014-10-10 10:39:29 +02:00
|
|
|
*/
|
2015-03-05 12:21:17 +01:00
|
|
|
def apply[T](subscriber: Subscriber[T]): Sink[T, Unit] =
|
2015-04-20 21:04:03 +02:00
|
|
|
new Sink(new SubscriberSink(subscriber, DefaultAttributes.subscriberSink, shape("SubscriberSink")))
|
2014-10-17 14:05:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that immediately cancels its upstream after materialization.
|
|
|
|
|
*/
|
2015-04-20 21:04:03 +02:00
|
|
|
def cancelled[T]: Sink[T, Unit] =
|
|
|
|
|
new Sink[Any, Unit](new CancelSink(DefaultAttributes.cancelledSink, shape("CancelledSink")))
|
2014-10-17 14:05:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that materializes into a `Future` of the first value received.
|
|
|
|
|
*/
|
2015-04-20 21:04:03 +02:00
|
|
|
def head[T]: Sink[T, Future[T]] = new Sink(new HeadSink[T](DefaultAttributes.headSink, shape("HeadSink")))
|
2014-10-17 14:05:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that materializes into a [[org.reactivestreams.Publisher]].
|
|
|
|
|
* that can handle one [[org.reactivestreams.Subscriber]].
|
|
|
|
|
*/
|
2015-04-20 21:04:03 +02:00
|
|
|
def publisher[T]: Sink[T, Publisher[T]] =
|
|
|
|
|
new Sink(new PublisherSink[T](DefaultAttributes.publisherSink, shape("PublisherSink")))
|
2014-10-17 14:05:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that materializes into a [[org.reactivestreams.Publisher]]
|
|
|
|
|
* that can handle more than one [[org.reactivestreams.Subscriber]].
|
|
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
def fanoutPublisher[T](initialBufferSize: Int, maximumBufferSize: Int): Sink[T, Publisher[T]] =
|
2015-04-20 21:04:03 +02:00
|
|
|
new Sink(new FanoutPublisherSink[T](initialBufferSize, maximumBufferSize, DefaultAttributes.fanoutPublisherSink,
|
|
|
|
|
shape("FanoutPublisherSink")))
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that will consume the stream and discard the elements.
|
|
|
|
|
*/
|
2015-06-05 18:26:32 +02:00
|
|
|
def ignore: Sink[Any, Future[Unit]] =
|
2015-04-20 21:04:03 +02:00
|
|
|
new Sink(new BlackholeSink(DefaultAttributes.ignoreSink, shape("BlackholeSink")))
|
2014-10-17 14:05:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that will invoke the given procedure for each received element. The sink is materialized
|
|
|
|
|
* into a [[scala.concurrent.Future]] will be completed with `Success` when reaching the
|
2015-01-30 10:30:56 +01:00
|
|
|
* normal end of the stream, or completed with `Failure` if there is a failure signaled in
|
2014-10-17 14:05:50 +02:00
|
|
|
* the stream..
|
|
|
|
|
*/
|
2015-06-14 03:12:30 -04:00
|
|
|
def foreach[T](f: T ⇒ Unit): Sink[T, Future[Unit]] =
|
|
|
|
|
Flow[T].map(f).toMat(Sink.ignore)(Keep.right).named("foreachSink")
|
2014-10-17 14:05:50 +02:00
|
|
|
|
2015-06-09 00:05:56 -04:00
|
|
|
/**
|
|
|
|
|
* A `Sink` that will invoke the given function to each of the elements
|
|
|
|
|
* as they pass in. The sink is materialized into a [[scala.concurrent.Future]]
|
|
|
|
|
*
|
|
|
|
|
* If `f` throws an exception and the supervision decision is
|
|
|
|
|
* [[akka.stream.Supervision.Stop]] the `Future` will be completed with failure.
|
|
|
|
|
*
|
|
|
|
|
* If `f` throws an exception and the supervision decision is
|
|
|
|
|
* [[akka.stream.Supervision.Resume]] or [[akka.stream.Supervision.Restart]] the
|
|
|
|
|
* element is dropped and the stream continues.
|
|
|
|
|
*
|
|
|
|
|
* @see [[#mapAsyncUnordered]]
|
|
|
|
|
*/
|
|
|
|
|
def foreachParallel[T](parallelism: Int)(f: T ⇒ Unit)(implicit ec: ExecutionContext): Sink[T, Future[Unit]] =
|
2015-06-14 03:12:30 -04:00
|
|
|
Flow[T].mapAsyncUnordered(parallelism)(t ⇒ Future(f(t))).toMat(Sink.ignore)(Keep.right)
|
2015-06-09 00:05:56 -04:00
|
|
|
|
2014-10-17 14:05:50 +02:00
|
|
|
/**
|
|
|
|
|
* A `Sink` that will invoke the given function for every received element, giving it its previous
|
|
|
|
|
* output (or the given `zero` value) and the element as input.
|
|
|
|
|
* The returned [[scala.concurrent.Future]] will be completed with value of the final
|
|
|
|
|
* function evaluation when the input stream ends, or completed with `Failure`
|
2015-01-30 10:30:56 +01:00
|
|
|
* if there is a failure signaled in the stream.
|
2014-10-17 14:05:50 +02:00
|
|
|
*/
|
2015-06-14 03:12:30 -04:00
|
|
|
def fold[U, T](zero: U)(f: (U, T) ⇒ U): Sink[T, Future[U]] =
|
|
|
|
|
Flow[T].fold(zero)(f).toMat(Sink.head)(Keep.right).named("foldSink")
|
2014-10-17 14:05:50 +02:00
|
|
|
|
|
|
|
|
/**
|
2015-01-30 10:30:56 +01:00
|
|
|
* A `Sink` that when the flow is completed, either through a failure or normal
|
2014-10-17 14:05:50 +02:00
|
|
|
* completion, apply the provided function with [[scala.util.Success]]
|
|
|
|
|
* or [[scala.util.Failure]].
|
|
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
def onComplete[T](callback: Try[Unit] ⇒ Unit): Sink[T, Unit] = {
|
|
|
|
|
|
|
|
|
|
def newOnCompleteStage(): PushStage[T, Unit] = {
|
|
|
|
|
new PushStage[T, Unit] {
|
2015-04-09 22:28:16 +02:00
|
|
|
override def onPush(elem: T, ctx: Context[Unit]): SyncDirective = ctx.pull()
|
2015-06-09 00:05:56 -04:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
override def onUpstreamFailure(cause: Throwable, ctx: Context[Unit]): TerminationDirective = {
|
|
|
|
|
callback(Failure(cause))
|
|
|
|
|
ctx.fail(cause)
|
|
|
|
|
}
|
2015-06-09 00:05:56 -04:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
override def onUpstreamFinish(ctx: Context[Unit]): TerminationDirective = {
|
|
|
|
|
callback(Success[Unit](()))
|
|
|
|
|
ctx.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-20 21:04:03 +02:00
|
|
|
Flow[T].transform(newOnCompleteStage).to(Sink.ignore).named("onCompleteSink")
|
2015-01-28 14:19:50 +01:00
|
|
|
}
|
2015-03-30 14:42:30 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends the elements of the stream to the given `ActorRef`.
|
|
|
|
|
* If the target actor terminates the stream will be cancelled.
|
|
|
|
|
* When the stream is completed successfully the given `onCompleteMessage`
|
|
|
|
|
* will be sent to the destination actor.
|
|
|
|
|
* When the stream is completed with failure a [[akka.actor.Status.Failure]]
|
|
|
|
|
* message will be sent to the destination actor.
|
|
|
|
|
*
|
|
|
|
|
* It will request at most `maxInputBufferSize` number of elements from
|
|
|
|
|
* upstream, but there is no back-pressure signal from the destination actor,
|
|
|
|
|
* i.e. if the actor is not consuming the messages fast enough the mailbox
|
|
|
|
|
* of the actor will grow. For potentially slow consumer actors it is recommended
|
|
|
|
|
* to use a bounded mailbox with zero `mailbox-push-timeout-time` or use a rate
|
2015-03-31 15:13:57 +02:00
|
|
|
* limiting stage in front of this `Sink`.
|
2015-03-30 14:42:30 +02:00
|
|
|
*/
|
|
|
|
|
def actorRef[T](ref: ActorRef, onCompleteMessage: Any): Sink[T, Unit] =
|
2015-04-20 21:04:03 +02:00
|
|
|
new Sink(new ActorRefSink(ref, onCompleteMessage, DefaultAttributes.actorRefSink, shape("ActorRefSink")))
|
2015-03-30 14:42:30 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a `Sink` 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` should
|
|
|
|
|
* be [[akka.stream.actor.ActorSubscriber]].
|
|
|
|
|
*/
|
|
|
|
|
def actorSubscriber[T](props: Props): Sink[T, ActorRef] =
|
2015-04-20 21:04:03 +02:00
|
|
|
new Sink(new ActorSubscriberSink(props, DefaultAttributes.actorSubscriberSink, shape("ActorSubscriberSink")))
|
2015-03-30 14:42:30 +02:00
|
|
|
|
2014-10-17 14:05:50 +02:00
|
|
|
}
|