我有以下定义的演员,意味着“登录”用户。
object AuthenticationActor {
def props = Props[AuthenticationActor]
case class LoginUser(id: UUID)
}
class AuthenticationActor @Inject()(cache: CacheApi, userService: UserService) extends Actor{
import AuthenticationActor._
def receive = {
case LoginEmployee(id: UUID) => {
userService.getUserById(id).foreach {
case Some(e) => {
println("Logged user in")
val sessionId = UUID.randomUUID()
cache.set(sessionId.toString, e)
sender() ! Some(e, sessionId)
}
case None => println("No user was found")
}
}
}
}
注意:userService.getUserById
会返回Future[Option[User]]
以下非常简单的API cal
class EmployeeController @Inject()(@Named("authentication-actor") authActor: ActorRef)(implicit ec: ExecutionContext) extends Controller {
override implicit val timeout: Timeout = 5.seconds
def login(id: UUID) = Action.async { implicit request =>
(authActor ? LoginUser(id)).mapTo[Option[(User, UUID)]].map {
case Some(authInfo) => Ok("Authenticated").withSession(request.session + ("auth" -> authInfo._2.toString))
case None => Forbidden("Not Authenticated")
}
}
}
两个println
调用都会执行,但login
调用总是会失败,表示请求已超时。有什么建议吗?
答案 0 :(得分:3)
当您执行此类操作(在Future
回调中访问发件人)时,您需要在收到请求时将sender
存储在范围内的val
中,因为它很可能在更改之前发生变化Future
完成。
def receive = {
case LoginEmployee(id: UUID) => {
val recipient = sender
userService.getUserById(id).foreach {
case Some(e) => {
...
recipient ! Some(e, sessionId)
}
...
}
}
}
在找不到用户时,您也永远不会发送结果。
您实际应该做的是将Future
结果传送到sender
def receive = {
case LoginEmployee(id: UUID) => {
userService.getUserById(id) map { _.map { e =>
val sessionId = UUID.randomUUID()
cache.set(sessionId.toString, e)
(e, sessionId)
}
} pipeTo sender
}
}
或印刷品
def receive = {
case LoginEmployee(id: UUID) => {
userService.getUserById(id) map {
case Some(e) =>
println("logged user in")
val sessionId = UUID.randomUUID()
cache.set(sessionId.toString, e)
Some(e, sessionId)
case None =>
println("user not found")
None
} pipeTo sender
}
}