在調用rest api時,我想將actor的路由切換到另一個路由。 請參閱下面的代碼。Akka Actor hotswapping using rest api
幾個問題:
- 代碼編譯罰款,但是當應用程序被啓動,一個HTTP調用時,我得到配置的1註冊超時第二期滿,停止的消息,我不得到任何迴應服務器。
- 我希望能夠通過api將路由切換到另一組路由。
取得
package com.example
import akka.actor.Actor
import akka.io.IO
import spray.httpx.RequestBuilding._
import spray.http.MediaTypes._
import spray.routing.{RoutingSettings, RejectionHandler, ExceptionHandler, HttpService}
import spray.util.LoggingContext
import scala.concurrent.Future
import spray.can.Http
import spray.http._
import akka.util.Timeout
import HttpMethods._
import akka.pattern.ask
import akka.event.Logging
import scala.concurrent.duration._
case object Swap
class MyServiceActor extends Actor with MyService with akka.actor.ActorLogging {
implicit def actorRefFactory = context
import context._
def receive = {
case Swap =>
become {
case Swap => unbecome()
case _ => runRoute(otherRoutes)
}
case _ => runRoute(myRoute)
}
}
trait MyService extends HttpService { this: MyServiceActor =>
implicit val timeout: Timeout = Timeout(15.seconds)
implicit val system = context.system
val myRoute =
{
path("") {
get {
complete("MyRoute")
}
} ~ path("swap") {
get{
self ! Swap
complete("Swapped")
}
}
}
val otherRoutes =path("") {
get {
complete("OtherRoutes")
}
} ~ path("swap") {
get{
self ! Swap
complete("Reverted")
}
}
}
我不看到一個明顯的問題。您收到的錯誤消息似乎表明您的演員出於某種原因未處理「已連接」消息。 – jrudolph 2014-11-21 07:57:48
如果我將接收方法更改爲def receive = runRoute(myRoute),則服務器響應沒有任何問題。 – Phani 2014-11-23 01:42:41
是的,另一個答案解釋了原因:您需要通過路由傳遞實際消息。 – jrudolph 2014-11-24 07:16:33