我想通过api代码从Akka Actor返回一个字符串响应。但是,我一直在超时,我不知道我的代码可能出什么毛病。
演员:
object CredentialsActor {
def props(implicit timeout: Timeout) = Props(new CredentialsActor)
}
class CredentialsActor extends Actor with ActorLogging {
override def receive: Receive = {
case RegisterRequest => sender() ! "REGISTER"
}
}
Api代码:
class RestApi(system: ActorSystem, timeout: Timeout) extends Routes {
implicit def requestTimeout: Timeout = timeout
implicit def executionContext: ExecutionContextExecutor = system.dispatcher
def createCredentialsActor(): ActorRef = system.actorOf(CredentialsActor.props)
}
trait Routes extends CredentialsApi {
val routes: Route =
pathPrefix("app") {
credentialsRoute
}
}
trait CredentialsApi {
def createCredentialsActor(): ActorRef
implicit def requestTimeout: Timeout
lazy val credentialsActor: ActorRef = createCredentialsActor()
val credentialsRoute: Route =
path("register") {
get {
pathEndOrSingleSlash {
entity(as[RegisterRequest]) {
request => {
System.out.println(request.name, request.password, request.passwordRepeated)
val response: Future[String] =
(credentialsActor ? request).mapTo[String]
complete(OK, response)
}
}
}
}
}
}
请求
final case class RegisterRequest(name: String, password: String, passwordRepeated: String)
答案 0 :(得分:3)
您没有提供RegisterRequest
的类型,但是如果它是case class
,则需要这样:
case _: RegisterRequest => sender() ! "REGISTER"
按现状显示,您正在匹配同伴对象,而不是类的实例。
要从请求中提取详细信息,请执行以下操作:
case RegisterRequest(name, pw, pwRep) =>
sender ! s"Register user $name with password $pw($pwRep)"