Skip to content

Instantly share code, notes, and snippets.

@a-hisame
Created January 8, 2015 13:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-hisame/d6c1b19079d8a2fd42c8 to your computer and use it in GitHub Desktop.
Save a-hisame/d6c1b19079d8a2fd42c8 to your computer and use it in GitHub Desktop.
実行するとMainスレッドが落ちても裏でスレッドが動作しててアプリケーションが終了しない。
import dispatch.url
import dispatch.Http
import dispatch.as
import dispatch.Defaults._
object Main {
def main(args: Array[String]): Unit = {
import dispatch._, Defaults._
val svc = url("http://www.google.co.jp/")
for {
response <- Http(svc OK as.String)
} {
println(response)
}
}
}
@a-hisame
Copy link
Author

a-hisame commented Jan 8, 2015

うーん、Windows依存の可能性が。。。

@a-hisame
Copy link
Author

a-hisame commented Jan 9, 2015

Ubuntuでも同様の現象を確認。
原因は、Http(Singleton)オブジェクトが所有するclientが閉じられないため。
末尾に Http.shutdown() を実行するとアプリケーションは終了するようになるが、今度は再度Httpオブジェクトが使えなくなってしまう。

解決策としては、Httpオブジェクトを自分で作ってクローズすればよい。

    val svc = url("http://www.google.co.jp/")
    val http = new Http(new com.ning.http.client.AsyncHttpClient)
    val futureResponse: Future[String] = http(svc OK as.String)
    Await.ready(futureResponse, Duration.Inf)
    println("response is " + futureResponse.value.get)
    http.shutdown()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment