如何在scala中使方法同步?
How to make a method synchronous in scala?
在此方法中,我希望看到返回的实际响应(result.toJson.toString 或 StatusCodes.InternalServerError.toString)而不是空字符串。我该怎么做?
def process(msgIn : WebSocketMessageIn, service : ActorRef) : String = {
import model.Registration
import model.RegistrationJsonProtocol._
implicit val timeout = Timeout(10 seconds)
msgIn.method.toUpperCase match {
case "POST" =>
log.debug(s"Handing POST message with body ${msgIn.body}")
val registration = msgIn.body.convertTo[Registration]
val future = (service ? PostRegistrationMessage(registration)).mapTo[Registration]
var response = ""
future onComplete {
case Success(result) =>
response = result.toJson.toString
case Failure(e) =>
log.error(s"Error: ${e.toString}")
response = StatusCodes.InternalServerError.toString
}
response
case "PUT" =>
s"Handing PUT message ${msgIn.body}"
}
}
这是调用方法并将响应发送到 websocket 的代码片段
case Message(ws, msg, service) =>
log.debug("url {} received msg '{}'", ws.getResourceDescriptor, msg)
val wsMessageIn = msg.parseJson.convertTo[WebSocketMessageIn]
val response = process(wsMessageIn, service)
ws.send(response);
更新 1:更新为使用 Await.result(未来,5000 毫秒)而不是 'future onComplete { ... }'。这是已更改的代码片段。现在可用,但只是想知道我们将如何处理故障。
msgIn.method.toUpperCase match {
case "POST" =>
log.debug(s"Handing POST message with body ${msgIn.body}")
val registration = msgIn.body.convertTo[ADSRegistration]
val future = (service ? PostADSRegistrationMessage(registration)).mapTo[ADSRegistration]
val response = Await.result(future, 5000 millis)
response.toJson.toString
您可以使用阻塞的Await.result
。像这样:
import scala.concurrent.duration._
val result = Await.result(future, atMost = 10.second)
val response = //result processing
同样,您可以将 future 传回去并在 onComplete
中执行 send
,这会更具反应性
在此方法中,我希望看到返回的实际响应(result.toJson.toString 或 StatusCodes.InternalServerError.toString)而不是空字符串。我该怎么做?
def process(msgIn : WebSocketMessageIn, service : ActorRef) : String = {
import model.Registration
import model.RegistrationJsonProtocol._
implicit val timeout = Timeout(10 seconds)
msgIn.method.toUpperCase match {
case "POST" =>
log.debug(s"Handing POST message with body ${msgIn.body}")
val registration = msgIn.body.convertTo[Registration]
val future = (service ? PostRegistrationMessage(registration)).mapTo[Registration]
var response = ""
future onComplete {
case Success(result) =>
response = result.toJson.toString
case Failure(e) =>
log.error(s"Error: ${e.toString}")
response = StatusCodes.InternalServerError.toString
}
response
case "PUT" =>
s"Handing PUT message ${msgIn.body}"
}
}
这是调用方法并将响应发送到 websocket 的代码片段
case Message(ws, msg, service) =>
log.debug("url {} received msg '{}'", ws.getResourceDescriptor, msg)
val wsMessageIn = msg.parseJson.convertTo[WebSocketMessageIn]
val response = process(wsMessageIn, service)
ws.send(response);
更新 1:更新为使用 Await.result(未来,5000 毫秒)而不是 'future onComplete { ... }'。这是已更改的代码片段。现在可用,但只是想知道我们将如何处理故障。
msgIn.method.toUpperCase match {
case "POST" =>
log.debug(s"Handing POST message with body ${msgIn.body}")
val registration = msgIn.body.convertTo[ADSRegistration]
val future = (service ? PostADSRegistrationMessage(registration)).mapTo[ADSRegistration]
val response = Await.result(future, 5000 millis)
response.toJson.toString
您可以使用阻塞的Await.result
。像这样:
import scala.concurrent.duration._
val result = Await.result(future, atMost = 10.second)
val response = //result processing
同样,您可以将 future 传回去并在 onComplete
中执行 send
,这会更具反应性