Is this a good way of implementing akka actor best practices

49 views
Skip to first unread message

Avi

unread,
May 10, 2015, 2:43:05 AM5/10/15
to akka...@googlegroups.com
Hi,
I want to implement I was looking at this example taken from typesafe activator (spray-actor-per-request)


class RestRouting extends HttpService with Actor with PerRequestCreator {

implicit def actorRefFactory: ActorContext = context

def receive = runRoute(route)

val petService = context.actorOf(Props[PetClient])
val ownerService = context.actorOf(Props[OwnerClient])

val route = {
 get {
   path("pets") {
          parameters('names) { names =>
    petsWithOwner {
           GetPetsWithOwners(names.split(',').toList)
     }
   }
  }
 }
}

def petsWithOwner(message : RestMessage): Route =
ctx => perRequest(ctx, Props(new GetPetsWithOwnersActor(petService, ownerService)), message)
}



and I wonder if this is the best parctice to implement the actors creation :
ctx => perRequest(ctx, Props(new GetPetsWithOwnersActor(petService, ownerService)), message)
because I saw at the akka documentation this warning regarding creating actor within an actor :
val props2 = Props(new ActorWithArgs("arg")) // careful, see below

also if we define an actor within an actor
val ownerService = context.actorOf(Props[OwnerClient])
how can it be tested ?


Just to make things clear - I am not criticizing, I am just trying to learn the best practice of implementation specially as I see the typesafe activator as educational source


Best wishes
Avi

Martynas Mickevičius

unread,
May 14, 2015, 8:57:04 AM5/14/15
to akka...@googlegroups.com
Hi Avi,

On Sun, May 10, 2015 at 9:43 AM, Avi <123...@gmail.com> wrote:
Hi,
I want to implement I was looking at this example taken from typesafe activator (spray-actor-per-request)


class RestRouting extends HttpService with Actor with PerRequestCreator {

implicit def actorRefFactory: ActorContext = context

def receive = runRoute(route)

val petService = context.actorOf(Props[PetClient])
val ownerService = context.actorOf(Props[OwnerClient])

val route = {
 get {
   path("pets") {
          parameters('names) { names =>
    petsWithOwner {
           GetPetsWithOwners(names.split(',').toList)
     }
   }
  }
 }
}

def petsWithOwner(message : RestMessage): Route =
ctx => perRequest(ctx, Props(new GetPetsWithOwnersActor(petService, ownerService)), message)
}



and I wonder if this is the best parctice to implement the actors creation :
ctx => perRequest(ctx, Props(new GetPetsWithOwnersActor(petService, ownerService)), message)
because I saw at the akka documentation this warning regarding creating actor within an actor :
val props2 = Props(new ActorWithArgs("arg")) // careful, see below

The warning is here to remind users to be careful when constructing Props. It is important that Props are serializable. When constructing Props instances inside an actor one can easily close over the surrounding actor instance. To avoid this, there is a recommended practice to create Props factory methods inside the companion object that corresponds to your actor.
 


also if we define an actor within an actor
val ownerService = context.actorOf(Props[OwnerClient])
how can it be tested ?

To make this testable you would need to either OwnerClient ActorRef or Props into the RestRouting class. You can find more information with examples on testing parent-child relationships in the Akka documentation.
 


Just to make things clear - I am not criticizing, I am just trying to learn the best practice of implementation specially as I see the typesafe activator as educational source


Best wishes
Avi

--
>>>>>>>>>> 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 akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Martynas Mickevičius
TypesafeReactive Apps on the JVM

Avi Levi

unread,
May 14, 2015, 10:18:18 AM5/14/15
to akka...@googlegroups.com
Thank you very much , exactly what I was looking for 
You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/XGzZvxP_Rak/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages