Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 欺骗快速启动客户端_Scala_Http_Finagle_Twitter Finagle - Fatal编程技术网

Scala 欺骗快速启动客户端

Scala 欺骗快速启动客户端,scala,http,finagle,twitter-finagle,Scala,Http,Finagle,Twitter Finagle,我有一个简单的sbt项目,我在其中添加了“com.twitter”%%“finagle http”%%“6.33.0”。我正在遵循《推特骗局指南》。我的代码是直接复制粘贴: import com.twitter.finagle.{Http, Service} import com.twitter.finagle.http import com.twitter.util.{Await, Future} object Client extends App { val client: Servi

我有一个简单的sbt项目,我在其中添加了
“com.twitter”%%“finagle http”%%“6.33.0”
。我正在遵循《推特骗局指南》。我的代码是直接复制粘贴:

import com.twitter.finagle.{Http, Service}
import com.twitter.finagle.http
import com.twitter.util.{Await, Future}

object Client extends App {
  val client: Service[http.Request, http.Response] = Http.newService("www.scala-lang.org:80")
  val request = http.Request(http.Method.Get, "/")
  request.host = "www.scala-lang.org"
  val response: Future[http.Response] = client(request)
  response.onSuccess { resp: http.Response =>
    println("GET success: " + resp) 
    println(resp.contentString)    // modification 1
  }
  Await.ready(response)
  println("needed this")           // modification 2
}
如果没有“
modification 2
”,我将无法获得任何输出。添加了
println

needed this
GET success: Response("HTTP/1.1 Status(200)")

Process finished with exit code 0
  • 为什么没有在没有“
    修改2
    ”的情况下打印响应
  • 为什么“
    modification 1
    ”中没有打印
    contentString
  • 如果我在“
    modification 1
    ”上设置断点,并使用当前状态评估
    resp.contentString
    ,则会根据需要返回网站的HTML


    当程序正常运行时,我如何才能将其打印出来?

    Twitter的
    Future
    上的
    onSuccess
    方法的签名与标准库的
    Future
    上的签名不同-而不是:

    def onSuccess[U](pf: PartialFunction[T, U])(implicit executor: ExecutionContext): Unit
    
    你有这个:

    def onSuccess(f: (A) ⇒ Unit): Future[A]
    
    也就是说,它返回一个新的未来,该未来返回与旧未来相同的值,但也会产生副作用,而不仅仅是产生副作用。(作为旁注,在我看来,这是Twitter future API比标准库更好的许多方式之一——我更喜欢函数参数的返回类型是
    Unit
    ,而方法的返回类型不是)


    在您的情况下,Finagle用于客户端的线程被后台监控,因此如果您没有明确等待未来的结果,就不能保证JVM不会在未来满足之前退出。更改代码以等待成功返回的未来结果将使一切按预期工作。

    谢谢Travis!我认为这是quickstart中的一个bug,所以很容易混淆。我会解决的。