2013-12-10 35 views
1

所以在Spray中,在做了基本的例子之後,我想擴展它來做更多的事情。這是我的路線:如何將請求轉發給Spray中的其他演員?

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")) 
     } 
    } 

當然這不會因爲缺少部分而編譯。我只想將請求(或可能是已經提取的參數封裝的請求)轉發給另一個像myActor的演員!請求...這是行不通的。我找不到這方面的例子,或者他們確實不適合。

+1

這取決於其他的演員應該用怎麼辦請求?另一個actor是否也包含路由?或者它只是爲這個確切的請求產生一個結果? – jrudolph

+0

它應該產生一個結果,但我仍然需要Request對象。所以可以有整個請求,或者可以發送一個包含它的類加上其他數據。 – gotch4

+0

只是爲了表明它可以完成,看看這個傢伙代碼: https://github.com/mhamrah/spray-sample/blob/master/src/main/scala/basic/BasicSample。scala – byrnedo

回答

3

您可以參考這個奇妙的例子,看代碼 - Akka and Spray 你會發現,示例說明了如何能夠從另一個演員的路由代理處理。如果你遵循它,你應該能夠解決你在使用「?」時看起來會遇到的編譯問題。像我一樣...

2

怎麼樣

complete { 
    actor ? request 
} 
+0

這是異步完成請求 – gotch4

+0

@ gotch4是的,它是 – 1esha

+0

它不編譯,因爲請求對象不在範圍內 – gotch4

0

當主管調用播放器,例如:

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

下面的代碼執行(屬於同一類):

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 

創建新從演員templateActor

演員

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

查詢路徑

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

我們發送郵件給演員:

// 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

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 
0

的最小工作示例可以是以下:

首先定義你的演員:

class MiningActor extends Actor { 
    private val timeRemaining = 10 

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

object RemainingMiningTime 

然後,您可以將其鏈接到自己喜歡的噴霧路線

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

}

參考:

相關問題