我正在研究一个带有spark的简单喷雾REST应用程序,我有以下代码(从spray-template生成)。我将其修改为包含json支持和一些返回业务列表的推荐程序。由于某种原因,即使我将timeout参数指定为无限,请求也会超时。谁能告诉我我做错了什么?看起来我采用了错误的技术选择,而论坛中的支持则非常少。推荐程序在后台运行,但服务器立即返回到客户端并发出超时错误。我玩了很多配置值,但似乎仍然不喜欢它。任何帮助是极大的赞赏。
case class Business(name:String)
object MyProtocol extends DefaultJsonProtocol with SprayJsonSupport{
implicit val elementFormat = jsonFormat1(Business)
}
trait MyService extends HttpService {
import MyProtocol._
val myRoute = {
path("") {
get {
respondWithMediaType(`application/json`) {
complete(Recommender.recommend(1000)) // times out
complete('some html') // works immediately
}
}
}
def Recommend(userId: Int) : List[Business] = {
......................
}
application.conf
spray.can {
server {
request-timeout = infinite
}
}
答案 0 :(得分:0)
尝试:
// In some file, define yopur model
case class Business(name: String, age: Int)
object MyProtocol extends DefaultJsonProtocol with SprayJsonSupport{
implicit val elementFormat = jsonFormat2(Business)
}
// In other file
trait MyService extends HttpService {
import MyProtocol._
val myRoutes = {
path("sthnotverytimeconsuming") {
get {
complete {
doRequest()
}
}
} ~
path("sthverytimeconsuming") {
get {
complete {
doTimeConsumingRequest()
}
}
}
}
private def doRequest(): Future[List[Business]] = {
Future{
val result : List[Business] = // ... do something that take times but not very long
result
}
}
private def doTimeConsumingRequest(): Future[List[Business]] = {
Future{
// ... you can do something that take a long time here
}
val result : List[Business] = //...
result //... this will return immediately
}
}