Suppose I have an actor hierarchy like this:

SprayAPIRoutingActor
XYZServiceActor
..DbActor
..OtherActor1
..OtherActor2

The DbActor wraps one or more blocking DAOs and has its own dispatcher to
isolate the blocking.
The router actor has access to the root service actor.

I have implemented one route using a per-request actor, just to see how
that worked. Now I'm trying another simple crud operation without the
per-request approach.

The route looks something like this:

    post {

      val result = (xyzService ?
XYZService.CreateXYZ(xyzAttributes)).mapTo[Long]

      onComplete(result) {

        case Success(value) => complete(s"The result was $value")

        case Failure(ex) => complete(StatusCodes.InternalServerError, s"An
error occurred: ${ex.getMessage}")

     }

    }

In this case the XYZService actor forwards a transformed message to the
DbActor

    case msg @ XYZService. CreateXYZ(attributes) => {

      dbActor forward DbActor.AddXYZ(attributes) // Interesting that I can
"forward" a new message

    }

If the response needed transforming, presumably an additional ask would be
needed within the XYZService.

The DbActor replies with a result

    case AddXYZ(attributes) => db withSession { implicit session =>

      try {

        val result = XYZDAO.add(attributes)

        sender() ! result

      } catch {

        case e: Exception =>

          sender() ! akka.actor.Status.Failure(e)

          throw e

      }

    }

Does this seem reasonable? Would it be considered better practice to use
per-request actors for every route, to avoid the use of tell, or somehow
pass the request context to the service actor for completion? I've read the
Ian Forsey article, but am a bit unclear on whether to standardize on the
per-request model. I did notice that the route I implemented using
per-request is slow on all but the first request, and I haven't had time to
look into that.

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to