scala - Finagle quickstart client -
i have bare sbt project have added "com.twitter" %% "finagle-http" % "6.33.0"
. following quickstart guide twitter finagle. code have direct copy-paste:
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 }
without "modification 2
" no output @ all. println
added, get
needed success: response("http/1.1 status(200)") process finished exit code 0
- why didn't response print without "
modification 2
"? - why there no
contentstring
printed "modification 1
"?
if set breakpoint on "modification 1
", , evaluate resp.contentstring
using current state, html website returned desired.
how can print while program running normally?
the signature of onsuccess
method on twitter's future
different 1 on standard library's future
—instead of this:
def onsuccess[u](pf: partialfunction[t, u])(implicit executor: executioncontext): unit
you have this:
def onsuccess(f: (a) ⇒ unit): future[a]
i.e. returns new future returns same value old future, performs side effect, instead of performing side effect. (as side note, in view 1 of many ways twitter future api better standard library's—i prefer both fact function argument's return type unit
, method's isn't).
what's happening in case threads finagle using client daemonized, if don't explicitly await result of future, there's no guarantee jvm won't exit before future satisfied. changing code await result of future returned onsuccess
make work expected.
Comments
Post a Comment