Sam*_*enn 5 scala sbt akka playframework
我一直在努力用Akka actor构建一个应用程序,现在我已经完成了基于actor的业务逻辑,我想给它一个RESTful + websocket前端.我正在尝试查找有关如何在现有应用程序的上下文中设置Play的说明.我能找到的唯一指令是如何创建新的Play应用程序.有没有关于如何做到这一点的文件?
更新:这个问题与SBT设置有关,而不是将控制器连接到基于actor的业务逻辑.我试图修改build.sbt并plugins.sbt包含激活器在我做的时候构建的东西,activator new但是IDEA正在抱怨Cannot resolve symbol PlayScala.另外,我想知道如何将我的演员从SBT标准src/main/scala转移到app/- 如果它app/actors(如我在其中一个模板中看到的那样)或在app/models?
这是我的build.sbt:
name := "test"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(play.PlayScala)
scalaVersion := "2.11.6"
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
specs2 % Test
)
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
scalaVersion := "2.11.6"
resolvers += "repo.novus rels" at "http://repo.novus.com/releases/"
resolvers += "repo.novus snaps" at "http://repo.novus.com/snapshots/"
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.1" % "test"
libraryDependencies += "com.github.nscala-time" %% "nscala-time" % "1.8.0"
libraryDependencies += "org.slf4j" % "slf4j-simple" % "1.6.4"
libraryDependencies += "org.reactivemongo" %% "reactivemongo" % "0.10.5.0.akka23"
routesGenerator := InjectedRoutesGenerator
Run Code Online (Sandbox Code Playgroud)
这是我的plugins.sbt:
logLevel := Level.Warn
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.0")
// web plugins
addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.0.6")
addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0")
Run Code Online (Sandbox Code Playgroud)
其中一部分是将 Businesslayer(基于 Actor 的业务逻辑 - ActorSystem)与Play MVC 中的控制器 ( play.api.mvc.Controller ) 连接起来。
以下示例展示了如何执行此操作:
import play.api.mvc._
import akka.actor._
import javax.inject._
import actors.HelloActor
@Singleton
class Application @Inject() (system: ActorSystem) extends Controller {
val helloActor = system.actorOf(HelloActor.props, "hello-actor")
//...
}
Run Code Online (Sandbox Code Playgroud)
那么你需要了解一些关于 Play Framework 的知识:
现在定义一些路由请求路径:
- GET /clients/all controllers. ... .list()
- GET /clients/:id controllers. ... .show(id: Long)
Run Code Online (Sandbox Code Playgroud)
并在控制器中实现操作:
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.duration._
import akka.pattern.ask
implicit val timeout = 5.seconds
def show(id: Long) = Action.async {
// This ist a ask pattern returns a Future
// Here ist the Connection between the Action from Play to your
// Actor System - your Business Layer
// map it to your own result type
(helloActor ? SayHello(id)).mapTo[String].map { message =>
Ok(message)
}
}
Run Code Online (Sandbox Code Playgroud)