在哪里放置dispatch.Http.shutdown() [英] Where to put dispatch.Http.shutdown()

查看:124
本文介绍了在哪里放置dispatch.Http.shutdown()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里放置对dispatch.Http.shutdown()的调用:

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

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

推荐答案

AFAIK只能在应用程序的末尾执行(或者严格地说,当您完成特定线程后才执行),因为一旦完成,所有现有的连接都已消失,您无法创建任何新连接.

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.

这篇关于在哪里放置dispatch.Http.shutdown()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆