concept receive function in category reactive

appears as: receive function, The receive function
Reactive Application Development

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

  • Actors have a receive function that accepts a message and doesn’t have a return value.
  • Actors are called by the actor system, not directly by other actors. The actor system guarantees that there’s never more than one thread at a time calling an actor’s receive function, which simplifies the receive function because it doesn’t have to be thread-safe.
  • While RareBooks is closed, the receive function can’t process new requests from Customers because no Librarians are available. Those requests don’t have to be rejected, however; instead, they can be stashed away safely for later processing. When the state switches back to open, those requests can be unstashed, and Akka takes care of sending them to the receive function a second time.

    Listing 10.10. Changes to the actor to support tracing
    import java.util.{Currency, Locale} import akka.actor.Actor import Guidebook.Inquiry import Tourist.Guidance import io.opentracing.Tracer class Guidebook(val tracer: Tracer) #1 extends Actor with Spanned { #2 def describe(locale: Locale) = s"""In ${locale.getDisplayCountry}, ${locale.getDisplayLanguage} is spoken and the currency is the ${Currency.getInstance(locale).getDisplayName}""" override def receive = TracingReceive(this){ #3 case Inquiry(code) => println(s"Actor ${self.path.name} responding to inquiry about $code") Locale.getAvailableLocales.filter(_.getCountry == code). foreach { locale => sender ! Guidance(code, describe(locale))(trace()) #4 } } } #1 The tracer is passed to the class constructor. #2 Adds span state to the actor #3 Stack TracingReceive on the receive function. #4 Adds the trace information to the message !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":7,\"ch\":16},{\"line\":7,\"ch\":34}],[{\"line\":7,\"ch\":68},{\"line\":7,\"ch\":70}],[{\"line\":26,\"ch\":0},{\"line\":26,\"ch\":2}],[{\"line\":9,\"ch\":1},{\"line\":9,\"ch\":13}],[{\"line\":9,\"ch\":66},{\"line\":9,\"ch\":68}],[{\"line\":27,\"ch\":0},{\"line\":27,\"ch\":2}],[{\"line\":16,\"ch\":25},{\"line\":16,\"ch\":45}],[{\"line\":16,\"ch\":68},{\"line\":16,\"ch\":70}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":2}],[{\"line\":21,\"ch\":51},{\"line\":21,\"ch\":59}],[{\"line\":21,\"ch\":68},{\"line\":21,\"ch\":70}],[{\"line\":29,\"ch\":0},{\"line\":29,\"ch\":2}]]"} !@%STYLE%@!