actorRef

Materialize an ActorRef; sending messages to it will emit them on the stream.

Source operators

Signature

def actorRef[T](bufferSize: Int, overflowStrategy: OverflowStrategy): Source[T, ActorRef]

Description

Materialize an ActorRef, sending messages to it will emit them on the stream. The actor contains a buffer but since communication is one way, there is no back pressure. Handling overflow is done by either dropping elements or failing the stream; the strategy is chosen by the user.

emits when there is demand and there are messages in the buffer or a message is sent to the ActorRef

completes when the ActorRef is sent akka.actor.Status.Success

Examples

Scala

import akka.actor.Status.Success import akka.actor.ActorRef import akka.stream.OverflowStrategy import akka.stream.scaladsl._ implicit val system: ActorSystem = ActorSystem() implicit val materializer: ActorMaterializer = ActorMaterializer() val bufferSize = 100 val source: Source[Any, ActorRef] = Source.actorRef[Any](bufferSize, OverflowStrategy.dropHead) val actorRef: ActorRef = source.to(Sink.foreach(println)).run() actorRef ! "hello" actorRef ! "hello" // The stream completes successfully with the following message actorRef ! Success("completes stream")
Java
import akka.actor.ActorRef;
import akka.actor.Status.Success;
import akka.stream.OverflowStrategy;
import akka.stream.javadsl.Sink;

final ActorSystem system = ActorSystem.create();
final Materializer materializer = ActorMaterializer.create(system);

int bufferSize = 100;
Source<Object, ActorRef> source = Source.actorRef(bufferSize, OverflowStrategy.dropHead());

ActorRef actorRef = source.to(Sink.foreach(System.out::println)).run(materializer);
actorRef.tell("hello", ActorRef.noSender());
actorRef.tell("hello", ActorRef.noSender());

// The stream completes successfully with the following message
actorRef.tell(new Success(CompletionStrategy.draining()), ActorRef.noSender());
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.