Docs: Actor sink stream operators (#29171)

This commit is contained in:
Enno 2020-07-01 21:27:29 +02:00 committed by GitHub
parent 1e5cb5a720
commit 101c1d9b65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 89 additions and 24 deletions

View file

@ -48,6 +48,13 @@ object ActorSink {
* will be sent to the destination actor.
* When the stream is completed with failure - result of `onFailureMessage(throwable)`
* function will be sent to the destination actor.
*
* @param ref the receiving actor as `ActorRef<T>` (where `T` must include the control messages below)
* @param messageAdapter a function that wraps the stream elements to be sent to the actor together with an `ActorRef[A]` which accepts the ack message
* @param onInitMessage a function that wraps an `ActorRef<A>` into a messages to couple the receiving actor to the sink
* @param ackMessage a fixed message that is expected after every element sent to the receiving actor
* @param onCompleteMessage the message to be sent to the actor when the stream completes
* @param onFailureMessage a function that creates a message to be sent to the actor in case the stream fails from a `Throwable`
*/
def actorRefWithBackpressure[T, M, A](
ref: ActorRef[M],

View file

@ -34,8 +34,8 @@ object ActorSink {
Sink.actorRef(ref.toClassic, onCompleteMessage, onFailureMessage)
/**
* Sends the elements of the stream to the given `ActorRef` that sends back back-pressure signal.
* First element is always `onInitMessage`, then stream is waiting for acknowledgement message
* Sends the elements of the stream to the given `ActorRef` that sends back back-pressure signals.
* The first element is always `onInitMessage`, then stream is waiting for acknowledgement message
* `ackMessage` from the given actor which means that it is ready to process
* elements. It also requires `ackMessage` message after each stream element
* to make backpressure work.
@ -45,6 +45,13 @@ object ActorSink {
* will be sent to the destination actor.
* When the stream is completed with failure - result of `onFailureMessage(throwable)`
* function will be sent to the destination actor.
*
* @param ref the receiving actor as `ActorRef[T]` (where `T` must include the control messages below)
* @param messageAdapter a function that wraps the stream elements to be sent to the actor together with an `ActorRef[A]` which accepts the ack message
* @param onInitMessage a function that wraps an `ActorRef[A]` into a messages to couple the receiving actor to the sink
* @param ackMessage a fixed message that is expected after every element sent to the receiving actor
* @param onCompleteMessage the message to be sent to the actor when the stream completes
* @param onFailureMessage a function that creates a message to be sent to the actor in case the stream fails from a `Throwable`
*/
def actorRefWithBackpressure[T, M, A](
ref: ActorRef[M],

View file

@ -7,7 +7,7 @@ package docs.akka.stream.typed;
// #actor-sink-ref-with-backpressure
import akka.NotUsed;
import akka.actor.typed.ActorRef;
import akka.stream.ActorMaterializer;
import akka.actor.typed.ActorSystem;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
import akka.stream.typed.javadsl.ActorSink;
@ -50,18 +50,27 @@ public class ActorSinkWithAckExample {
}
// #actor-sink-ref-with-backpressure
final ActorMaterializer mat = null;
final ActorSystem<Void> system = null;
{
// #actor-sink-ref-with-backpressure
final ActorRef<Protocol> actor = null;
final ActorRef<Protocol> actorRef = // spawned actor
null; // #hidden
final Ack ackMessage = new Ack();
final Complete completeMessage = new Complete();
final Sink<String, NotUsed> sink =
ActorSink.actorRefWithBackpressure(
actor, Message::new, Init::new, new Ack(), new Complete(), Fail::new);
actorRef,
(responseActorRef, element) -> new Message(responseActorRef, element),
(responseActorRef) -> new Init(responseActorRef),
ackMessage,
completeMessage,
(exception) -> new Fail(exception));
Source.single("msg1").runWith(sink, mat);
Source.single("msg1").runWith(sink, system);
// #actor-sink-ref-with-backpressure
}
}

View file

@ -162,11 +162,11 @@ object ActorSourceSinkExample {
val sink: Sink[String, NotUsed] = ActorSink.actorRefWithBackpressure(
ref = actor,
messageAdapter = (responseActorRef: ActorRef[Ack], element) => Message(responseActorRef, element),
onInitMessage = (responseActorRef: ActorRef[Ack]) => Init(responseActorRef),
ackMessage = Ack,
onCompleteMessage = Complete,
onFailureMessage = Fail.apply,
messageAdapter = Message.apply,
onInitMessage = Init.apply,
ackMessage = Ack)
onFailureMessage = (exception) => Fail(exception))
Source.single("msg1").runWith(sink)
// #actor-sink-ref-with-backpressure