Search code examples
scalaakkaactorspray

How to compose routes in actor's receive with runRoute?


I need to add a few custom system messages processing to spray.routing.HttpService. I need to chain receive methods as follows:

def receive = {
 case ...my messages here
 case _ => ...call httpReceive below
}

def httpReceive = runRoute...

How to organize this?


Solution

  • in Spray runRoute converts your route structure into Akka's Receive, which is a type alias for PartialFunction[Any, Unit]. So if you have some method which handles your own messages and some route you can just compose them with orElse:

    def httpReceive: Receive = runRoute(...)
    def handle: Receive = ...
    
    def receive = handle orElse httpReceive
    

    Now if your messages will be handled with handle method and then, if it was a request, spray will handle this request