Frage

So in Spray, after doing the basic example, I wanted to extend it to do more things. This is my route:

val API_ROUTING_TREE: Route = pathPrefix("api") {

    pathPrefix("content") {

      pathPrefix("rendered" / Segment) {

        pageName => {

          /*Matches /block/{template}/{blockName}*/
          pathPrefix("block" / Segment) {

            templateName => {

              path(Segment) {

                blockName => (get | post) {
                   ??????????WHAT DOES GO HERE???????????????
                }

              }
            }
          }
        }
      }
    } ~
      path("structured") {
        failWith(new RuntimeException("Not Implemented"))
      }
  }

of course this doesn't compile because of the missing part. What I'd like is to just forward the request (or possibly the request encapsulated with the parameters already extracted) to another actor like myActor ! request... That doesn't work. I can't find examples for this or they really don't fit.

War es hilfreich?

Lösung

You could refer to this wonderful example and look at the code - Akka and Spray You will find that the example shows how you could delegate processing from a route to another actor. If you follow it you should be able to solve the compilation problem that you seem to face with using "?" like I did...

Andere Tipps

what about

complete {
    actor ? request
}

When the supervisor player is invoked, for example:

 TemplateActors.templateSupervisor ! new TemplateMessage("***POST***", 1)

The following code is executed(belongs to the same class):

 import akka.actor.{ Actor, Props, ActorLogging }

 /**
 * Actor TemplateSupervisor
 */
class TemplateSupervisor extends Actor with ActorLogging {
  import scala.concurrent.duration._
  import akka.actor.SupervisorStrategy._
  import akka.actor.OneForOneStrategy

Creating the new Actors

  val templateActor = context.actorOf( Props[ TemplateActor ], "TemplateActor" ))
  val templateActor2 = context.actorOf( Props[ TemplateActorJuan ], "juan_carlos_actor" )

Query path from Actor templateActor

  //log.info( "path templateActor2: " + templateActor2.path )
  //log.info( "path templateActor: " + templateActor.path       

We sent the message to Actor:

  // sent message
  def receive = {
    // Forward message to templateActor
    case message: TemplateMessage => templateActor forward message
  }

  override val supervisorStrategy = OneForOneStrategy( ) {
    case exception: Exception =>
      exception.printStackTrace( )
      log.error( exception, exception.getMessage(  ) )
      Restart
  }
}

/**
 * TemplateActor
 */
class TemplateActor extends Actor with ActorLogging {

  import akka.pattern._
  import scala.concurrent.{Future, ExecutionContext}

  implicit val _: ExecutionContext = context.dispatcher
  val actor = context.actorSelection("//ms-service-executor/user/TemplateSupervisor/juan_carlos_actor")
  def receive = {
    case message: TemplateMessage =>
      log.info("************Realizando PING from TemplateActor")
      actor ! new PING( "PING" )
    case message: PONG =>
      log.info("************PONG make in TemplateActor")
  }
}

/**
 * TemplateActorJuan
 */
class TemplateActorJuan extends Actor with ActorLogging {

  import akka.pattern._
  import scala.concurrent.{Future, ExecutionContext}
  implicit val _: ExecutionContext = context.dispatcher
  val actor = context.actorSelection("//ms-service-executor/user/TemplateSupervisor/TemplateActor")
  def receive = {
    case message: PING =>
      log.info("************make PONG from TemplateActorJuan")
      actor ! new PONG( "PONG" )
  }
}

case class PING( val id: String )
case class PONG( val id: String )

TemplateActors Class

import akka.actor.{ Props, ActorSystem }
import TemplateSupervisor

/**
 * Method override for the unique ActorSystem instance
 */
trait Core {
  implicit def system: ActorSystem
}

/**
 * Definition of the ActorSystem and the ExecutionContext
 */
trait BootedCore extends Core {
  import scala.concurrent.ExecutionContext

  implicit lazy val system = ActorSystem( "ms-service-executor" )
  implicit lazy val ex: ExecutionContext = system.dispatcher
  sys.addShutdownHook( system.shutdown( ) )
}

/**
 * Template project actors instantiation
 */
trait CoreActors { this: Core =>
  /*
  * Creacion del actor "TemplateSupervisor"
  * Props: configuracion del actor
  * system: unico actor existente
  */
  val templateSupervisor = system.actorOf( Props[ TemplateSupervisor ], "TemplateSupervisor" )
}

/**
 * Template actor references
 */
object TemplateActors extends BootedCore with CoreActors

A minimal working example can be the following:

First you define your actor:

class MiningActor extends Actor {
   private val timeRemaining = 10

   override def receive = {
      case RemainingMiningTime => sender ! timeRemaining
   }
}

object RemainingMiningTime

Then, you can link it to your favourite Spray route

lazy val miningRoute = {
get {
  path("mining" / "remaining") {
    complete {
      (miningActor ? RemainingMiningTime).mapTo[Int]
        .map(s => s"Your amber will be mined in $s")
    }
  }
}

}

Reference:

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top