concept Librarian in category reactive

appears as: Librarian, Librari, A Librari, The Librari, Librari
Reactive Application Development

This is an excerpt from Manning's book Reactive Application Development.

Customers send inquiries and receive entries from the catalog. These inquiries are modeled as immutable messages. From a programming point of view, you can think of the messages as being forms of application programming interfaces (APIs) for a given actor. For brevity’s sake, we’ll model two types of Find messages that RareBooks is expected to answer. The flow of a Find message through RareBooks and onward to a Librarian is shown in figure 4.2.

Figure 4.2. RareBooks routes book requests to a Librarian for processing, and the response flows from the Librarian directly back to the Customer.

So far, RareBooks has hired only two Librarians—enough to handle questions during normal business hours but not enough to stay open all the time. The model needs to include the concept that RareBooks is either open or closed. In addition, the Librarians are efficient, but researching each question does take some time. A Librarian is either ready to receive a question or busy working on a request. You can model this situation by creating two states for each of those actors, as shown in figure 4.3.

Figure 4.3. RareBooks and the Librarian change how they process messages when they change state.

In Akka, this situation is called a fault, and we model it in this section as an exception. When one of the Librarians exceeds its tolerance for complaints, it becomes frustrated and throws a ComplainException. When this exception occurs, Akka pauses the Librarian routee and escalates to the parent, which is RareBooks. Because RareBooks uses the default supervision strategy (which we talk about in the next section), it restarts the paused Librarian, effectively resetting that Librarian’s complainCount to zero. At this point, the Customer that complained is in a state in which it no longer requests information, as it didn’t receive a credit. The RareBooks owner can correct that situation.

Listing 4.17. Faulty Librarian parameters
object Librarian { // ... final case class ComplainException( #1 c: Complain, #1 customer: ActorRef) #1 extends IllegalStateException("Too many complaints!") #1 // ... def props( findBookDuration: FiniteDuration, maxComplainCount: Int): Props = #2 Props(new Librarian(findBookDuration, maxComplainCount)) #2 // ... } #1 Immutable exception descriptor #2 Adds maxComplain-Count parameter !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":69},{\"line\":2,\"ch\":71}],[{\"line\":3,\"ch\":69},{\"line\":3,\"ch\":71}],[{\"line\":4,\"ch\":69},{\"line\":4,\"ch\":71}],[{\"line\":5,\"ch\":69},{\"line\":5,\"ch\":71}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":2}],[{\"line\":2,\"ch\":69},{\"line\":2,\"ch\":71}],[{\"line\":3,\"ch\":69},{\"line\":3,\"ch\":71}],[{\"line\":4,\"ch\":69},{\"line\":4,\"ch\":71}],[{\"line\":5,\"ch\":69},{\"line\":5,\"ch\":71}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":2}],[{\"line\":2,\"ch\":69},{\"line\":2,\"ch\":71}],[{\"line\":3,\"ch\":69},{\"line\":3,\"ch\":71}],[{\"line\":4,\"ch\":69},{\"line\":4,\"ch\":71}],[{\"line\":5,\"ch\":69},{\"line\":5,\"ch\":71}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":2}],[{\"line\":2,\"ch\":69},{\"line\":2,\"ch\":71}],[{\"line\":3,\"ch\":69},{\"line\":3,\"ch\":71}],[{\"line\":4,\"ch\":69},{\"line\":4,\"ch\":71}],[{\"line\":5,\"ch\":69},{\"line\":5,\"ch\":71}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":2}],[{\"line\":9,\"ch\":69},{\"line\":9,\"ch\":71}],[{\"line\":10,\"ch\":69},{\"line\":10,\"ch\":71}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":2}],[{\"line\":9,\"ch\":69},{\"line\":9,\"ch\":71}],[{\"line\":10,\"ch\":69},{\"line\":10,\"ch\":71}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":2}]]"} !@%STYLE%@!

Next, add the complaining misbehavior to the Librarian, as shown in the following listing.

Listing 4.18. Faulty Librarian behavior
class Librarian( findBookDuration: FiniteDuration, maxComplainCount: Int) #1 extends Actor with ActorLogging with Stash { //... private var complainCount: Int = 0 #2 //... private def ready: Receive = { case m: Msg => m match { case c: Complain if complainCount == maxComplainCount => #3 throw ComplainException(c, sender()) #4 case c: Complain => #5 complainCount += 1 #5 sender ! Credit() #5 //... #1 Adds maxComplainCount to the constructor #2 Local mutable state for number of complaints received #3 Matches guard on complaint count #4 Too many! #5 Otherwise, process the complaint messages normally. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":65},{\"line\":2,\"ch\":67}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":2}],[{\"line\":5,\"ch\":65},{\"line\":5,\"ch\":67}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":2}],[{\"line\":9,\"ch\":65},{\"line\":9,\"ch\":67}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":2}],[{\"line\":10,\"ch\":65},{\"line\":10,\"ch\":67}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":2}],[{\"line\":11,\"ch\":65},{\"line\":11,\"ch\":67}],[{\"line\":12,\"ch\":65},{\"line\":12,\"ch\":67}],[{\"line\":13,\"ch\":65},{\"line\":13,\"ch\":67}],[{\"line\":20,\"ch\":0},{\"line\":20,\"ch\":2}],[{\"line\":11,\"ch\":65},{\"line\":11,\"ch\":67}],[{\"line\":12,\"ch\":65},{\"line\":12,\"ch\":67}],[{\"line\":13,\"ch\":65},{\"line\":13,\"ch\":67}],[{\"line\":20,\"ch\":0},{\"line\":20,\"ch\":2}],[{\"line\":11,\"ch\":65},{\"line\":11,\"ch\":67}],[{\"line\":12,\"ch\":65},{\"line\":12,\"ch\":67}],[{\"line\":13,\"ch\":65},{\"line\":13,\"ch\":67}],[{\"line\":20,\"ch\":0},{\"line\":20,\"ch\":2}]]"} !@%STYLE%@!

Finally, a small change makes the RareBooks actor fetch the max-complain-count and pass it to the Librarian on creation (shown in the following listing).

Listing 4.19. RareBooks actor changes
class RareBooks extends Actor with ActorLogging with Stash { //... private val maxComplainCount: Int = #1 context.system.setting.config getInt "rare-books.librarian.max-complain-count" //... protected def createLibrarian(): Router = { //... val r = context.actorOf(Librarian.props( findBookDuration, maxComplainCount), s"librarian-$cnt") #2 //... #1 Fetches the max-complain-count from application.conf #2 Passes maxComplainCount upon creation of Librarian routee !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":66},{\"line\":2,\"ch\":68}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}],[{\"line\":9,\"ch\":64},{\"line\":9,\"ch\":66}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":2}]]"} !@%STYLE%@!

Configure the simulation by making the number of tolerable complaints a property of Librarian in application.conf and add it to the properties factory as a parameter, as shown in the following listing.

Listing 4.20. Faulty Librarian actor application.conf changes
//... rare-books { open-duration = 20 seconds close-duration = 5 seconds nbr-of-librarians = 5 librarian { find-book-duration = 2 seconds max-complain-count = 2 #1 } } #1 Maximum number of tolerable complaints for a given Librarian !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":7,\"ch\":4},{\"line\":7,\"ch\":26}],[{\"line\":7,\"ch\":42},{\"line\":7,\"ch\":44}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":2}]]"} !@%STYLE%@!

Now that the Librarian simulation has a maximum tolerance for receiving complaints before giving up and failing, see how it behaves when you run it.