我已经创建了一条路线:
post {
path("validate" / Segment / Segment / Segment) { (userId, listId, prefix) =>
parameters('filter.?) { filter =>
def result = (
phoneValidationActor ? ValidateUserList(userId.toInt, listId.toInt, prefix.toUpperCase, filter)
)
complete(result)
}
}
}
还有演员
class PhoneNumberActor extends Actor with ActorLogging {
import PhoneNumberActor._
def receive: Receive = {
case ValidateUserList(userId, listId, prefix, filter) =>
sender() ! validateUserList(userId, listId, prefix, filter)
}
}
还有演员的接收功能
def validateUserList(user_id: Int, list_id: Int, prefix: String, filter: Option[String]): Future[Seq[PhoneNumber]] = {
val prefixTrim = prefix.trim
val listContact = new ListContactRepository
listContact.getAllContacts(user_id, list_id).map { lines =>
lines.map { line =>
validateNumber(line.phone, prefixTrim)
}
}
}
在路由中,结果被解析为Future[Any]
而不是Future[Seq[PhoneNumber]]
需要帮助解决此问题
答案 0 :(得分:0)
您需要致电询问电话mapTo
。
(phoneValidationActor ? ValidateUserList(userId.toInt, listId.toInt, prefix.toUpperCase, filter))).mapTo[Seq[PhoneNumber]]
并在actor内部处理将来以避免ClassCastException
class PhoneNumberActor extends Actor with ActorLogging {
import PhoneNumberActor._
def receive: Receive = {
case ValidateUserList(userId, listId, prefix, filter) =>
val theSender = sender() //do not call this method in callback function onSuccess as it breaks actor principles and can send message to a wrong sender
validateUserList(userId, listId, prefix, filter).onSuccess { phoneNumbers =>
theSender ! phoneNumbers
}
}
}
答案 1 :(得分:0)
就像@Ivan Stanislavciuc所说的那样,您需要添加mapTo
方法以将Future强制转换为期望的类型,在这种情况下为[Seq[PhoneNumber]]
。
对于您的路线,我会将其更新为类似这样的内容:
post {
path("validate" / Segment / Segment / Segment) { (userId, listId, prefix) =>
parameters('filter.?) { filter =>
val result =
(phoneValidationActor ? ValidateUserList(userId.toInt, listId.toInt, prefix.toUpperCase, filter)).mapTo[Seq[PhoneNumber]]
onSuccess(result) { maybeResult =>
complete(maybeResult)
}
}
}
}
如果您想处理成功和失败,则可以使用onComplete
代替onSuccess
,并具有以下内容:
onComplete(result) {
case scala.util.Success(res) => complete(res)
case scala.util.Failure(ex) => complete(StatusCodes.BadRequest, ex)}