Akka:Actor 没有发送响应
Akka: Actor isn't sending response
我想通过 api 代码从 Akka Actor return 获得一个字符串响应。但是,我总是遇到超时,而且我不知道我的代码有什么问题。
演员:
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)
你没有给出 RegisterRequest
的类型,但如果它是 case class
那么你想要这个:
case _: RegisterRequest => sender() ! "REGISTER"
就目前而言,您匹配的是伴随对象而不是 class 的实例。
要从请求中提取详细信息,请执行以下操作:
case RegisterRequest(name, pw, pwRep) =>
sender ! s"Register user $name with password $pw($pwRep)"
我想通过 api 代码从 Akka Actor return 获得一个字符串响应。但是,我总是遇到超时,而且我不知道我的代码有什么问题。
演员:
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)
你没有给出 RegisterRequest
的类型,但如果它是 case class
那么你想要这个:
case _: RegisterRequest => sender() ! "REGISTER"
就目前而言,您匹配的是伴随对象而不是 class 的实例。
要从请求中提取详细信息,请执行以下操作:
case RegisterRequest(name, pw, pwRep) =>
sender ! s"Register user $name with password $pw($pwRep)"