concept RareBooks in category reactive

This is an excerpt from Manning's book Reactive Application Development.
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.
The RareBooks actor needs to handle messages differently depending on whether it’s open or closed. Now that you have messages that tell RareBooks when to open and close, the next step is defining the behaviors for the open and closed states. When RareBooks is open, Librarians are available to process requests. When RareBooks is closed, it sets aside new requests until the next time it’s open, and while RareBooks is closed, it needs to run the report.
Now you’re ready to implement the RareBooks actor, as shown in listing 4.4. The state transitions are the ones depictured in figure 4.4 earlier in this chapter:
When RareBooks begins, it immediately creates a Librarian, schedules the first Close event, and starts receiving messages in the open state. While RareBooks remains open, it forwards protocol messages to the Librarian. When it receives the scheduled Close event, it schedules an Open event, becomes closed, and sends another message to itself to produce the daily report. While RareBooks is closed, it processes the daily Report event and waits for an Open event. When the Open event arrives, it schedules reopening, unstashes saved messages, and becomes open. Any other messages that it receives while it’s closed are stashed. Listing 4.4. The RareBooks actor
class RareBooks extends Actor with ActorLogging with Stash { import context.dispatcher import RareBooks._ #1 import RareBooksProtocol._ #1 private val openDuration: FiniteDuration = ... #2 private val closeDuration: FiniteDuration = ... #2 private val findBookDuration: FiniteDuration = ... #2 private val librarian = createLibrarian() #3 var requestsToday: Int = 0 #4 var totalRequests: Int = 0 #4 context.system.scheduler.scheduleOnce( #5 openDuration, self, Close) #5 override def receive: Receive = open #6 private def open: Receive = { case m: Msg => #7 requestsToday += 1 #7 librarian forward m #7 case Close => context.system.scheduler.scheduleOnce( #8 closeDuration, self, Open) #8 context.become(closed) #9 self ! Report #10 } private def closed: Receive = { case Report => totalRequests += requestsToday #11 log.info(s"$requestsToday ... #11 requests processed = $totalRequests") #11 requestsToday = 0 #11 case Open => context.system.scheduler.scheduleOnce( #12 openDuration, self, Close) #12 unstashAll() #13 context.become(open) #14 case _ => stash() #15 } protected def createLibrarian(): ActorRef = { #16 context.actorOf( #16 Librarian.props(findBookDuration), "librarian") #16 } #16 } #1 Imports message definitions from the protocol object and the companion object #2 Defines how long various events take in the simulation #3 One Librarian for now #4 Running totals #5 Schedules the first close event #6 Begins in the open state #7 Forwards protocol messages to the Librarian #8 On close, schedule when to reopen. #9 Closes the shop #10 RareBooks tells itself to run the report. #11 Updates the running totals, prints the report, and resets the daily total #12 On open, schedule when to close. #13 Unstashes messages that arrived while the shop was closed #14 Opens the shop #15 Stashes other messages that arrive while the shop is closed #16 Utility to create a Librarian actor !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":3,\"ch\":67},{\"line\":3,\"ch\":69}],[{\"line\":4,\"ch\":67},{\"line\":4,\"ch\":69}],[{\"line\":31,\"ch\":67},{\"line\":31,\"ch\":69}],[{\"line\":37,\"ch\":67},{\"line\":37,\"ch\":69}],[{\"line\":38,\"ch\":67},{\"line\":38,\"ch\":69}],[{\"line\":39,\"ch\":65},{\"line\":39,\"ch\":67}],[{\"line\":40,\"ch\":67},{\"line\":40,\"ch\":69}],[{\"line\":43,\"ch\":67},{\"line\":43,\"ch\":69}],[{\"line\":44,\"ch\":65},{\"line\":44,\"ch\":67}],[{\"line\":46,\"ch\":67},{\"line\":46,\"ch\":69}],[{\"line\":48,\"ch\":67},{\"line\":48,\"ch\":69}],[{\"line\":51,\"ch\":67},{\"line\":51,\"ch\":69}],[{\"line\":54,\"ch\":67},{\"line\":54,\"ch\":69}],[{\"line\":55,\"ch\":67},{\"line\":55,\"ch\":69}],[{\"line\":56,\"ch\":65},{\"line\":56,\"ch\":67}],[{\"line\":57,\"ch\":67},{\"line\":57,\"ch\":69}],[{\"line\":60,\"ch\":0},{\"line\":60,\"ch\":2}],[{\"line\":69,\"ch\":0},{\"line\":69,\"ch\":2}],[{\"line\":70,\"ch\":0},{\"line\":70,\"ch\":2}],[{\"line\":71,\"ch\":0},{\"line\":71,\"ch\":2}],[{\"line\":72,\"ch\":0},{\"line\":72,\"ch\":2}],[{\"line\":73,\"ch\":0},{\"line\":73,\"ch\":2}],[{\"line\":74,\"ch\":0},{\"line\":74,\"ch\":2}],[{\"line\":75,\"ch\":0},{\"line\":75,\"ch\":2}],[{\"line\":3,\"ch\":67},{\"line\":3,\"ch\":69}],[{\"line\":4,\"ch\":67},{\"line\":4,\"ch\":69}],[{\"line\":31,\"ch\":67},{\"line\":31,\"ch\":69}],[{\"line\":37,\"ch\":67},{\"line\":37,\"ch\":69}],[{\"line\":38,\"ch\":67},{\"line\":38,\"ch\":69}],[{\"line\":39,\"ch\":65},{\"line\":39,\"ch\":67}],[{\"line\":40,\"ch\":67},{\"line\":40,\"ch\":69}],[{\"line\":43,\"ch\":67},{\"line\":43,\"ch\":69}],[{\"line\":44,\"ch\":65},{\"line\":44,\"ch\":67}],[{\"line\":46,\"ch\":67},{\"line\":46,\"ch\":69}],[{\"line\":48,\"ch\":67},{\"line\":48,\"ch\":69}],[{\"line\":51,\"ch\":67},{\"line\":51,\"ch\":69}],[{\"line\":54,\"ch\":67},{\"line\":54,\"ch\":69}],[{\"line\":55,\"ch\":67},{\"line\":55,\"ch\":69}],[{\"line\":56,\"ch\":65},{\"line\":56,\"ch\":67}],[{\"line\":57,\"ch\":67},{\"line\":57,\"ch\":69}],[{\"line\":60,\"ch\":0},{\"line\":60,\"ch\":2}],[{\"line\":69,\"ch\":0},{\"line\":69,\"ch\":2}],[{\"line\":70,\"ch\":0},{\"line\":70,\"ch\":2}],[{\"line\":71,\"ch\":0},{\"line\":71,\"ch\":2}],[{\"line\":72,\"ch\":0},{\"line\":72,\"ch\":2}],[{\"line\":73,\"ch\":0},{\"line\":73,\"ch\":2}],[{\"line\":74,\"ch\":0},{\"line\":74,\"ch\":2}],[{\"line\":75,\"ch\":0},{\"line\":75,\"ch\":2}],[{\"line\":6,\"ch\":67},{\"line\":6,\"ch\":69}],[{\"line\":7,\"ch\":67},{\"line\":7,\"ch\":69}],[{\"line\":8,\"ch\":67},{\"line\":8,\"ch\":69}],[{\"line\":61,\"ch\":0},{\"line\":61,\"ch\":2}],[{\"line\":6,\"ch\":67},{\"line\":6,\"ch\":69}],[{\"line\":7,\"ch\":67},{\"line\":7,\"ch\":69}],[{\"line\":8,\"ch\":67},{\"line\":8,\"ch\":69}],[{\"line\":61,\"ch\":0},{\"line\":61,\"ch\":2}],[{\"line\":6,\"ch\":67},{\"line\":6,\"ch\":69}],[{\"line\":7,\"ch\":67},{\"line\":7,\"ch\":69}],[{\"line\":8,\"ch\":67},{\"line\":8,\"ch\":69}],[{\"line\":61,\"ch\":0},{\"line\":61,\"ch\":2}],[{\"line\":10,\"ch\":67},{\"line\":10,\"ch\":69}],[{\"line\":62,\"ch\":0},{\"line\":62,\"ch\":2}],[{\"line\":12,\"ch\":67},{\"line\":12,\"ch\":69}],[{\"line\":13,\"ch\":67},{\"line\":13,\"ch\":69}],[{\"line\":63,\"ch\":0},{\"line\":63,\"ch\":2}],[{\"line\":12,\"ch\":67},{\"line\":12,\"ch\":69}],[{\"line\":13,\"ch\":67},{\"line\":13,\"ch\":69}],[{\"line\":63,\"ch\":0},{\"line\":63,\"ch\":2}],[{\"line\":15,\"ch\":67},{\"line\":15,\"ch\":69}],[{\"line\":16,\"ch\":65},{\"line\":16,\"ch\":67}],[{\"line\":64,\"ch\":0},{\"line\":64,\"ch\":2}],[{\"line\":15,\"ch\":67},{\"line\":15,\"ch\":69}],[{\"line\":16,\"ch\":65},{\"line\":16,\"ch\":67}],[{\"line\":64,\"ch\":0},{\"line\":64,\"ch\":2}],[{\"line\":18,\"ch\":15},{\"line\":18,\"ch\":22}],[{\"line\":6,\"ch\":14},{\"line\":6,\"ch\":18}],[{\"line\":16,\"ch\":3},{\"line\":16,\"ch\":7}],[{\"line\":18,\"ch\":34},{\"line\":18,\"ch\":38}],[{\"line\":20,\"ch\":14},{\"line\":20,\"ch\":18}],[{\"line\":44,\"ch\":7},{\"line\":44,\"ch\":11}],[{\"line\":48,\"ch\":21},{\"line\":48,\"ch\":25}],[{\"line\":65,\"ch\":17},{\"line\":65,\"ch\":21}],[{\"line\":67,\"ch\":32},{\"line\":67,\"ch\":36}],[{\"line\":71,\"ch\":7},{\"line\":71,\"ch\":11}],[{\"line\":18,\"ch\":67},{\"line\":18,\"ch\":69}],[{\"line\":65,\"ch\":0},{\"line\":65,\"ch\":2}],[{\"line\":6,\"ch\":14},{\"line\":6,\"ch\":18}],[{\"line\":16,\"ch\":3},{\"line\":16,\"ch\":7}],[{\"line\":18,\"ch\":34},{\"line\":18,\"ch\":38}],[{\"line\":20,\"ch\":14},{\"line\":20,\"ch\":18}],[{\"line\":44,\"ch\":7},{\"line\":44,\"ch\":11}],[{\"line\":48,\"ch\":21},{\"line\":48,\"ch\":25}],[{\"line\":65,\"ch\":17},{\"line\":65,\"ch\":21}],[{\"line\":67,\"ch\":32},{\"line\":67,\"ch\":36}],[{\"line\":71,\"ch\":7},{\"line\":71,\"ch\":11}],[{\"line\":21,\"ch\":67},{\"line\":21,\"ch\":69}],[{\"line\":22,\"ch\":67},{\"line\":22,\"ch\":69}],[{\"line\":23,\"ch\":67},{\"line\":23,\"ch\":69}],[{\"line\":66,\"ch\":0},{\"line\":66,\"ch\":2}],[{\"line\":21,\"ch\":67},{\"line\":21,\"ch\":69}],[{\"line\":22,\"ch\":67},{\"line\":22,\"ch\":69}],[{\"line\":23,\"ch\":67},{\"line\":23,\"ch\":69}],[{\"line\":66,\"ch\":0},{\"line\":66,\"ch\":2}],[{\"line\":21,\"ch\":67},{\"line\":21,\"ch\":69}],[{\"line\":22,\"ch\":67},{\"line\":22,\"ch\":69}],[{\"line\":23,\"ch\":67},{\"line\":23,\"ch\":69}],[{\"line\":66,\"ch\":0},{\"line\":66,\"ch\":2}],[{\"line\":26,\"ch\":67},{\"line\":26,\"ch\":69}],[{\"line\":27,\"ch\":65},{\"line\":27,\"ch\":67}],[{\"line\":67,\"ch\":0},{\"line\":67,\"ch\":2}],[{\"line\":26,\"ch\":67},{\"line\":26,\"ch\":69}],[{\"line\":27,\"ch\":65},{\"line\":27,\"ch\":67}],[{\"line\":67,\"ch\":0},{\"line\":67,\"ch\":2}],[{\"line\":29,\"ch\":67},{\"line\":29,\"ch\":69}],[{\"line\":68,\"ch\":0},{\"line\":68,\"ch\":2}],[{\"line\":31,\"ch\":67},{\"line\":31,\"ch\":70}],[{\"line\":69,\"ch\":0},{\"line\":69,\"ch\":3}],[{\"line\":29,\"ch\":21},{\"line\":29,\"ch\":27}],[{\"line\":34,\"ch\":12},{\"line\":34,\"ch\":18}],[{\"line\":72,\"ch\":55},{\"line\":72,\"ch\":61}],[{\"line\":74,\"ch\":57},{\"line\":74,\"ch\":63}],[{\"line\":37,\"ch\":67},{\"line\":37,\"ch\":70}],[{\"line\":38,\"ch\":67},{\"line\":38,\"ch\":70}],[{\"line\":39,\"ch\":65},{\"line\":39,\"ch\":68}],[{\"line\":40,\"ch\":67},{\"line\":40,\"ch\":70}],[{\"line\":70,\"ch\":0},{\"line\":70,\"ch\":3}],[{\"line\":37,\"ch\":67},{\"line\":37,\"ch\":70}],[{\"line\":38,\"ch\":67},{\"line\":38,\"ch\":70}],[{\"line\":39,\"ch\":65},{\"line\":39,\"ch\":68}],[{\"line\":40,\"ch\":67},{\"line\":40,\"ch\":70}],[{\"line\":70,\"ch\":0},{\"line\":70,\"ch\":3}],[{\"line\":37,\"ch\":67},{\"line\":37,\"ch\":70}],[{\"line\":38,\"ch\":67},{\"line\":38,\"ch\":70}],[{\"line\":39,\"ch\":65},{\"line\":39,\"ch\":68}],[{\"line\":40,\"ch\":67},{\"line\":40,\"ch\":70}],[{\"line\":70,\"ch\":0},{\"line\":70,\"ch\":3}],[{\"line\":37,\"ch\":67},{\"line\":37,\"ch\":70}],[{\"line\":38,\"ch\":67},{\"line\":38,\"ch\":70}],[{\"line\":39,\"ch\":65},{\"line\":39,\"ch\":68}],[{\"line\":40,\"ch\":67},{\"line\":40,\"ch\":70}],[{\"line\":70,\"ch\":0},{\"line\":70,\"ch\":3}],[{\"line\":43,\"ch\":67},{\"line\":43,\"ch\":70}],[{\"line\":44,\"ch\":65},{\"line\":44,\"ch\":68}],[{\"line\":71,\"ch\":0},{\"line\":71,\"ch\":3}],[{\"line\":43,\"ch\":67},{\"line\":43,\"ch\":70}],[{\"line\":44,\"ch\":65},{\"line\":44,\"ch\":68}],[{\"line\":71,\"ch\":0},{\"line\":71,\"ch\":3}],[{\"line\":46,\"ch\":67},{\"line\":46,\"ch\":70}],[{\"line\":72,\"ch\":0},{\"line\":72,\"ch\":3}],[{\"line\":48,\"ch\":67},{\"line\":48,\"ch\":70}],[{\"line\":73,\"ch\":0},{\"line\":73,\"ch\":3}],[{\"line\":51,\"ch\":67},{\"line\":51,\"ch\":70}],[{\"line\":74,\"ch\":0},{\"line\":74,\"ch\":3}],[{\"line\":54,\"ch\":67},{\"line\":54,\"ch\":70}],[{\"line\":55,\"ch\":67},{\"line\":55,\"ch\":70}],[{\"line\":56,\"ch\":65},{\"line\":56,\"ch\":68}],[{\"line\":57,\"ch\":67},{\"line\":57,\"ch\":70}],[{\"line\":75,\"ch\":0},{\"line\":75,\"ch\":3}],[{\"line\":54,\"ch\":67},{\"line\":54,\"ch\":70}],[{\"line\":55,\"ch\":67},{\"line\":55,\"ch\":70}],[{\"line\":56,\"ch\":65},{\"line\":56,\"ch\":68}],[{\"line\":57,\"ch\":67},{\"line\":57,\"ch\":70}],[{\"line\":75,\"ch\":0},{\"line\":75,\"ch\":3}],[{\"line\":54,\"ch\":67},{\"line\":54,\"ch\":70}],[{\"line\":55,\"ch\":67},{\"line\":55,\"ch\":70}],[{\"line\":56,\"ch\":65},{\"line\":56,\"ch\":68}],[{\"line\":57,\"ch\":67},{\"line\":57,\"ch\":70}],[{\"line\":75,\"ch\":0},{\"line\":75,\"ch\":3}],[{\"line\":54,\"ch\":67},{\"line\":54,\"ch\":70}],[{\"line\":55,\"ch\":67},{\"line\":55,\"ch\":70}],[{\"line\":56,\"ch\":65},{\"line\":56,\"ch\":68}],[{\"line\":57,\"ch\":67},{\"line\":57,\"ch\":70}],[{\"line\":75,\"ch\":0},{\"line\":75,\"ch\":3}]]"} !@%STYLE%@!A lot is happening in the RareBooks actor. Study it for a few moments to ensure that you understand how the pieces interact, and give yourself a pat on the back when you do. You can grab the source code from the book’s website, www.manning.com/books/reactive-application-development, or http://mng.bz/71O3.