2

如果我有一个 servlet 直接取自 Scalatra 文档页面上的示例:

package me.myself.andi

import _root_.akka.dispatch._
import org.scalatra.akka.AkkaSupport
import org.scalatra.ScalatraServlet

class MyAppServlet extends ScalatraServlet with AkkaSupport {
  get("/"){
    Future {
      // Add other logic here

      <html><body>Hello Akka</body></html>
    }
  }
}

我得到一个错误class MyAppServlet needs to be abstract, since method system in trait AkkaSupport of type => akka.actor.ActorSystem is not defined

然后,我尝试了:

package me.myself.andi

import _root_.akka.dispatch._
import org.scalatra.akka.AkkaSupport
import org.scalatra.ScalatraServlet

class MyAppServlet extends ScalatraServlet with AkkaSupport {
  val system = ActorSystem("MySystem")
  get("/"){
    Future(system) { // and also Future {
      // Add other logic here

      <html><body>Hello Akka</body></html>
    }
  }
}

但收到另一个错误type mismatch; found : org.scalatra.ActionResult required: akka.dispatch.ExecutionContext。对阿卡不熟悉,这是怎么回事?

libraryDependencies ++= Seq(
  "org.scalatra" % "scalatra" % "2.2.0-SNAPSHOT",
  "org.scalatra" % "scalatra-scalate" % "2.2.0-SNAPSHOT",
  "org.scalatra" % "scalatra-specs2" % "2.2.0-SNAPSHOT" % "test",
  "org.scalatra" % "scalatra-akka" % "2.2.0-SNAPSHOT",
  "com.typesafe.akka" % "akka" % "2.0.4",
  "ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime",
  "eu.infomas" % "annotation-detector" % "3.0.0",
  "org.atmosphere" % "atmosphere-runtime" % "1.1.0-SNAPSHOT",
  "org.eclipse.jetty" % "jetty-websocket" % "8.1.4.v20120524",
  "org.eclipse.jetty" % "jetty-webapp" % "8.1.7.v20120910" % "container",
  "org.eclipse.jetty" % "test-jetty-servlet" % "8.1.5.v20120716" % "test",
  "org.eclipse.jetty" % "jetty-websocket" % "8.1.7.v20120910" % "container",
  "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar"))
)
4

1 回答 1

3

编译器错误非常清楚。导入akka.actor.ActorSystem并添加val system = ActorSystem("MySystem")到类中,它应该可以工作。

编辑:

system应该是一个隐式值,所以它必须是然后implicit val system = ActorSystem("MySystem")不要手动传入系统。所以总的来说

class MyAppServlet extends ScalatraServlet with AkkaSupport {
  implicit val system = ActorSystem("MySystem")
  get("/"){
    Future { // and also Future {
      // Add other logic here

      <html><body>Hello Akka</body></html>
    }
  }
}
于 2012-11-29T19:55:52.893 回答