Akka:演员没有发送响应

问题描述 投票:0回答:1

我想通过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)
            }
          }
        }
      }
    }
}
scala akka actor akka-http
1个回答
0
投票

您不提供RegisterRequest的类型,但是如果它是case class,则需要这样:

case _: RegisterRequest => sender() ! "REGISTER"

按现状,您正在匹配同伴对象,而不是类的实例。

© www.soinside.com 2019 - 2024. All rights reserved.