2014-02-12 66 views
5

哪里调用dispatch.Http.shutdown()是否有n独立的HTTP调用等等,例如放置: 放在哪里dispatch.Http.shutdown()

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() } 
+1

编辑中的代码是要走的路!像这样重新使用这种期货相当优雅。 –

+0

谢谢@KevinWright! –

+0

@KevinWright:您可能也有一个答案http://stackoverflow.com/questions/21791580/where-to-put-dispatch-http-shutdown-in-case-of-cascading-http-calls。谢谢,/ nm –

回答

3

据我所知,你只能做它在应用程序(或严格地说,当你与一个特定的线程中完成的),因为一旦完成,所有现有的连接都没有了年底,你不能创建任何新的。