简体   繁体   中英

Scala Actor receive definition with Spray

I am using spray server, and I have a working receive implementation, which I wanted to extend, but the second one is not working: First, working implementation:

def receive = runRoutes(routes)

Second version:

def receive = {
  case GetRoutes => sender() ! getRoute ~ deleteRoute ~ postRoute ~ patchRoute
  case x => runRoute(getRoute ~ deleteRoute ~ postRoute ~ patchRoute)
}

runRoute is of type Actor.Receive .

Thanks

You have to actually call the runRoute if you write it as you do in the 2nd example:

val run = runRoute(getRoute) // takes a number of implicit params to construct the route
def receive = {
  case x => run(x)
}

Instead of that however I'd suggest to compose the Actor.Receive functions using this pattern:

val special: Actor.Receive = { case GetRoutes => sender() ! getRoute ! ... } 
def receive: Actor.Receive = special orElse runRoute(getRoute ~ ...)

You can read more about chaining PartialFunction s on from this blog post for example: http://daily-scala.blogspot.com/2010/02/chaining-partial-functions-with-orelse.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM