Scala 如何在休息服务中使用喷洒路线内的演员?
我正在尝试使用scala构建带有REST接口的事件源服务。我对scala有些陌生,尽管我熟悉函数式编程(haskell初级) 因此,我构建了持久化的actor和view,没有重大问题。我认为演员的想法很简单Scala 如何在休息服务中使用喷洒路线内的演员?,scala,rest,akka,spray,Scala,Rest,Akka,Spray,我正在尝试使用scala构建带有REST接口的事件源服务。我对scala有些陌生,尽管我熟悉函数式编程(haskell初级) 因此,我构建了持久化的actor和view,没有重大问题。我认为演员的想法很简单 object Main extends App { val system = ActorSystem("HelloSystem") val systemActor = system.actorOf(Props[SystemActor], name = "systemactor")
object Main extends App {
val system = ActorSystem("HelloSystem")
val systemActor = system.actorOf(Props[SystemActor], name = "systemactor")
val trajectoryView = system.actorOf(Props[TrajectoryView], name = "trajectoryView")
var datas = List()
val processData = ProcessData(0, List(1,2,3), Coordinates(50, 50))
implicit val timeout = Timeout(5 seconds)
def intialDatas(): List[ProcessData] =
(for (i <- 1 to 3) yield ProcessData(i, List(1,2,3), Coordinates(50 + i, 50 + i)))(collection.breakOut)
val command = RegisterProcessCommand(3, this.intialDatas())
val id = Await.result(systemActor ? command, timeout.duration).asInstanceOf[String]
println(id)
systemActor ! MoveProcessCommand(4, ProcessData(4, List(3,4,5), Coordinates(54, 54)), id)
val processes = Await.result(systemActor ? "get", timeout.duration).asInstanceOf[Set[Process]]
println(processes)
implicit val json4sFormats = DefaultFormats
println(write(processes))
println("*****************")
systemActor ! "print"
val getTrajectoryCommand = GetTrajectoryCommand(id)
Thread.sleep(10000)
trajectoryView ! "print"
// val trajectory = Await.result(trajectoryView ? getTrajectoryCommand, timeout.duration).asInstanceOf[ListBuffer[Coordinates]]
println("******* TRAJECTORY *********")
trajectoryView ! "print"
// println(trajectory)
system.shutdown()
}
还有一项服务
class ProcessesService(systemActor: ActorRef) extends Actor with HttpService {
def actorRefFactory = context
def receive = runRoute(route)
val json4sFormats = DefaultFormats
implicit val timeout = Timeout(5 seconds)
val route = path("processes") {
get {
respondWithMediaType(`application/json`) {
complete {
write(Await.result(systemActor ? "get", timeout.duration).asInstanceOf[Set[Process]])
}
}
}
}
}
我想我需要以某种方式将actorRef for SystemActor传递给这个ProcessService,但我不确定如何传递。此外,我不确定如何返回对请求的响应。我知道我需要以某种方式通过ActorRef将“get”消息传递给SystemActor,然后序列化json的答案,但我不知道如何做到这一点
我会感激你的帮助 在spray中,您可以使用
Future
完成路线
你应该可以做一些类似的事情
complete { systemActor ? "get" }
Json序列化是另一个问题
哦,你的问题很模糊。是的,您需要能够引用路线中的参与者。您可以从定义val的
boot
导入val。它们只是Scala变量,所以放在哪里取决于您。好吧,这是分开的,但我的问题是关于它们两个。我已经找到了关于完整的未来(谷歌搜索很容易)。如果您可以提供一些简单的完全工作的示例(带有序列化),这将非常有用。与演员一起工作与让喷洒路由应用程序工作是分开的。听起来你可能想先把重点放在让一个简单的喷雾应用程序工作上。一旦你开始工作,你就可以担心演员的问题了。我这里有一个相当基本的spray应用程序示例
complete { systemActor ? "get" }