Where to put dispatch.Http.shutdown()

落花浮王杯 提交于 2019-12-10 14:56:31

问题


where to place the call to dispatch.Http.shutdown() if there are n independent Http calls like, for example:

import com.typesafe.scalalogging.slf4j.Logging
import org.json4s._
import org.json4s.native.JsonMethods._
import scala.util.{ Failure, Success }

object Main extends App with Logging {
  logger.debug("github.cli")

  // GET /users/defunkt: `curl https://api.github.com/users/defunkt`
  val host: dispatch.Req    = dispatch.host("api.github.com").secure
  val request: dispatch.Req = host / "users" / "defunkt"
  logger.debug(s"Request URL: ${request.url}")

  import dispatch.Defaults.executor

  dispatch.Http(request > dispatch.as.Response(_.getHeaders())) onComplete {
    case Success(h) => logger.debug(h.toString())
    case Failure(e) => logger.debug(s"Error: $e")
  }
  dispatch.Http(request OK dispatch.as.json4s.Json) onComplete {
    case Success(j) => logger.debug(j.toString())
    case Failure(e) => logger.debug(s"Error: $e")
  }

  //dispatch.Http.shutdown()    // <<<<< ?????
}

Thanks, /nm

Edit: Could that be an option, say, is that a "proper" way to do it?

val headers = dispatch.Http(request > dispatch.as.Response(_.getHeaders()))
val user    = dispatch.Http(request OK dispatch.as.json4s.Json)
val all     = dispatch.Future.sequence(headers :: user :: Nil)

headers onComplete {
  case Success(h) => logger.debug(s"Header: ${h.toString()}")
  case Failure(e) => logger.debug(s"Error: $e")
}
user onComplete {
  case Success(j) => logger.debug(s"User: ${j.toString()}")
  case Failure(e) => logger.debug(s"Error: $e")
}
all onComplete { case _ => dispatch.Http.shutdown() }

回答1:


AFAIK you only do it at the end of the application (or, strictly speaking, when you are done with a specific thread), because once it is done, all the existing connections are gone and you cannot create any new ones.



来源:https://stackoverflow.com/questions/21722777/where-to-put-dispatch-http-shutdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!