【发布时间】:2014-09-26 20:31:35
【问题描述】:
这段代码运行良好,但我想通过Future 管理线程。
sendSMS 方法执行通常需要 3 到 5 秒,我想申请未来并申请在一个地方但想知道它是否足够?
val c = for {
t <- Future { doSendSms("+9178787878787","i scare with threads") }
} yield t
c.map { res =>
res match {
case e: Error => {
Ok(write(Map("result" -> "error")))
}
case Success() => {
Ok(write(Map("result" -> "success")))
}
def doSendSms(recipient: String, body: String): SentSmsResult = {
try {
sendSMS(recipient, body)
Success()
} catch {
case twilioEx: TwilioRestException =>
return Error(twilioEx.toString)
case e: Exception =>
return Error(e.toString)
}
}
def sendSMS(smsTo: String, body: String) = {
val params = Map("To" -> smsTo, "From" -> twilioNumber, "Body" -> body)
val messageFactory = client.getAccount.getSmsFactory
messageFactory.create(params)
}// sending sms from twilio, this method takes 3 to 5 seconds to execute
如果不是如何在这段代码中管理 Future
【问题讨论】:
标签: multithreading scala playframework thread-safety future