5

dispatch.Http.shutdown()如果有独立的 Http调用n,例如:

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()    // <<<<< ?????
}

谢谢,/纳米

编辑:这可能是一种选择,比如说,这是一种“正确”的方式吗?

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() }
4

1 回答 1

3

AFAIK 您只在应用程序结束时执行此操作(或者,严格来说,当您完成特定线程时),因为一旦完成,所有现有连接都将消失,您无法创建任何新连接。

于 2014-04-05T14:37:39.363 回答