我正在试图弄清楚如何设置一个调用适当孩子的主演员,以支持我尝试模拟db调用的一些喷射路线。我是akka / spray的新手,所以只是想更好地了解如何正确设置喷雾 - >演员 - > db调用(等)。我可以从顶级演员那里得到回复,但是当我试图从父母以下的一个演员层面回来时,我似乎无法获得任何工作。
当观察演员的路径时,从我从喷射路线拨打电话的方式看来,我是从一个临时演员那里传来的。以下是我到目前为止所说的内容。这必须是用户错误/无知,只是不知道如何继续。任何建议,将不胜感激。
下面的Demo Spray Service和Redis Actor代码片段显示我从我的路线调用演员的地方以及我遇到问题的多个演员(希望我的路线从SummaryActor获得响应)。谢谢!
启动
object Boot extends App {
// we need an ActorSystem to host our application in
implicit val system = ActorSystem("on-spray-can")
// create and start our service actor
val service = system.actorOf(Props[DemoServiceActor], "demo-service")
implicit val timeout = Timeout(5.seconds)
// start a new HTTP server on port 8080 with our service actor as the handler
IO(Http) ? Http.Bind(service, interface = "localhost", port = 8080)
}
演示服务演员(喷涂)
class DemoServiceActor extends Actor with Api {
// the HttpService trait defines only one abstract member, which
// connects the services environment to the enclosing actor or test
def actorRefFactory = context
// this actor only runs our route, but you could add
// other things here, like request stream processing
// or timeout handling
def receive = handleTimeouts orElse runRoute(route)
//Used to watch for request timeouts
//http://spray.io/documentation/1.1.2/spray-routing/key-concepts/timeout-handling/
def handleTimeouts: Receive = {
case Timedout(x: HttpRequest) =>
sender ! HttpResponse(StatusCodes.InternalServerError, "Too late")
}
}
//Master trait for handling large APIs
//http://stackoverflow.com/questions/14653526/can-spray-io-routes-be-split-into-multiple-controllers
trait Api extends DemoService {
val route = {
messageApiRouting
}
}
演示喷涂服务(路线):
trait DemoService extends HttpService with Actor {
implicit val timeout = Timeout(5 seconds) // needed for `?` below
val redisActor = context.actorOf(Props[RedisActor], "redisactor")
val messageApiRouting =
path("summary" / Segment / Segment) { (dataset, timeslice) =>
onComplete(getSummary(redisActor, dataset, timeslice)) {
case Success(value) => complete(s"The result was $value")
case Failure(ex) => complete(s"An error occurred: ${ex.getMessage}")
}
}
def getSummary(redisActor: ActorRef, dataset: String, timeslice: String): Future[String] = Future {
val dbMessage = DbMessage("summary", dataset + timeslice)
val future = redisActor ? dbMessage
val result = Await.result(future, timeout.duration).asInstanceOf[String]
result
}
}
Redis演员(模拟没有真正的redis客户端)
class RedisActor extends Actor with ActorLogging {
// val pool = REDIS
implicit val timeout = Timeout(5 seconds) // needed for `?` below
val summaryActor = context.actorOf(Props[SummaryActor], "summaryactor")
def receive = {
case msg: DbMessage => {
msg.query match {
case "summary" => {
log.debug("Summary Query Request")
log.debug(sender.path.toString)
summaryActor ! msg
}
}
}
//If not match log an error
case _ => log.error("Received unknown message: {} ")
}
}
class SummaryActor extends Actor with ActorLogging{
def receive = {
case msg: DbMessage =>{
log.debug("Summary Actor Received Message")
//Send back to Spray Route
}
}
}
答案 0 :(得分:3)
您的代码的第一个问题是您需要从主操作员转发到子操作符,以便sender
正确传播并可供孩子响应。所以改变它(在RedisActor
中):
summaryActor ! msg
要:
summaryActor forward msg
这是主要问题。修复它,你的代码应该开始工作。还有一些需要注意的事情。您的getSummary
方法目前定义为:
def getSummary(redisActor: ActorRef, dataset: String, timeslice: String): Future[String] =
Future {
val dbMessage = DbMessage("summary", dataset + timeslice)
val future = redisActor ? dbMessage
val result = Await.result(future, timeout.duration).asInstanceOf[String]
result
}
这里的问题是ask
操作(?
)已经返回Future
,所以你在那里阻止它以获得结果,将其包装在另一个Future
中Future
1}}这样您就可以为onComplete
返回Future
来使用。您应该能够直接使用从ask
返回的def getSummary(redisActor: ActorRef, dataset: String, timeslice: String): Future[String] = {
val dbMessage = DbMessage("summary", dataset + timeslice)
(redisActor ? dbMessage).mapTo[String]
}
来简化操作:
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<style>
.vmselement{
color: #e8b215;
}
.color-right{
border-bottom: 1px solid #e8b215;
width:50%;
float:right;
position:relative;
}
</style>
</head>
<body>
<div class="vmselement"><img src="img/play.png" alt="play" />
<h2>AFSLEELBAAR </br> OP ELD DEVICE</h2>
<div class="color-right"> </div>
</div>
</body>
</html>
答案 1 :(得分:1)
只是对上述方法的重要评论。
由于getSummary(...)函数返回Future [String]对象,并且您在onComplete(...)函数中调用它,您需要导入:
import ExecutionContext.Implicits.global
通过让Future,你将在范围内拥有ExecutionContext 声明一个隐式的ExecutionContext参数。
**如果不这样做,最终会出现不匹配的错误 因为onComplete(...)需要一个onComplete Future magnet对象,但你给了一个Future [String]对象。