Stateful actors

The behavior of the fetcher manager depends on whether it has work to give out to the fetchers:

  • If it has work to give, it needs to respond to GiveMeWork messages with a Fetcher.Fetch message
  • If it does not have work, it must ignore the GiveMeWork messages and, if work gets added, it must send a WorkAvailable message to the fetchers

Encoding the notion of state is straightforward in Akka. We specify different receive methods and switch from one to the other depending on the state. We will define the following receive methods for our fetcher manager, corresponding to each of the states:

// receive method when the queue is empty def receiveWhileEmpty: Receive = { ... } // receive method when the queue is not empty def receiveWhileNotEmpty: ...

Get Scala: Guide for Data Science Professionals now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.