Monitoring observes actors so that messages, termination, or failure behavior can be detected and recovery actions can follow. In Akka, it serves both automated actor tests and fault-tolerant actor arrangements. section 4.3.1 section 5.5.1
Use monitoring to:
- Place a test probe in front of an actor so messages sent to the monitored behavior can be intercepted and checked. section 4.3.1
- Verify message cascades when one actor forwards work to another actor or to a probe. section 4.3.1
- Monitor actors in a processing chain and respawn them when unexpected stopping would otherwise leave references sending messages to deadLetters. section 5.5.1
Intercepting a message
Create a probe, wrap the behavior with Behaviors.monitor(probe.ref, behaviorUnderTest), spawn the monitored behavior, send it "checking", and assert probe.expectMessage("checking"). The probe observes the message while the actor remains the object under test. section 4.3.1
class AsyncForwardSpec ...
...
"a monitor" must {
"intercept the messages" in {
val probe = createTestProbe[String]
val behaviorUnderTest = Behaviors.receiveMessage[String] { _ =>
Behaviors.ignore
}
val behaviorMonitored = Behaviors.monitor(probe.ref, behaviorUnderTest) #1
val actor = testkit.spawn(behaviorMonitored)
actor ! "checking"
probe.expectMessage("checking")
}
}
...
}
Shows the complete monitor test structure, including the probe, monitored behavior, spawned actor, sent message, and expected observation. section 4.3.1







































