我想通过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)
}
}
}
}
}
}
您不提供RegisterRequest
的类型,但是如果它是case class
,则需要这样:
case _: RegisterRequest => sender() ! "REGISTER"
按现状,您正在匹配同伴对象,而不是类的实例。