我正在开发一个简单的带有 Spark 的喷雾 REST 应用程序,我有以下代码(从喷雾模板生成)。我修改了它以包括 json 支持和一些返回业务列表的推荐程序。由于某种原因,即使我将超时参数指定为无限,请求也会超时。有人可以告诉我我做错了什么吗?看起来我选择了错误的技术,论坛中的支持非常少。推荐器在后台运行,尽管服务器会立即返回客户端并显示超时错误。我玩了很多配置值,但似乎仍然不喜欢它。任何帮助将不胜感激。
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
}
}
尝试:
// 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
}
}