watch

Watch a specific ActorRef and signal a failure downstream once the actor terminates.

Actor interop operators

Signature

Source.watchSource.watch Flow.watchFlow.watch

Description

Watch a specific ActorRef and signal a failure downstream once the actor terminates. The signaled failure will be an WatchedActorTerminatedException WatchedActorTerminatedException.

Example

An ActorRef can be can be watched and the stream will fail with WatchedActorTerminatedException when the actor terminates.

Scala
sourceval ref: ActorRef = someActor()
val flow: Flow[String, String, NotUsed] =
  Flow[String].watch(ref).recover {
    case _: WatchedActorTerminatedException => s"$ref terminated"
  }
Java
sourcefinal ActorRef ref = someActor();
Flow<String, String, NotUsed> flow =
    Flow.of(String.class)
        .watch(ref)
        .recover(
            org.apache.pekko.stream.WatchedActorTerminatedException.class,
            () -> ref + " terminated");

Reactive Streams semantics

emits when upstream emits

backpressures when downstream backpressures

completes when upstream completes