I am trying to combine AsyncHttpClient and Scalaz Task together. Normally, if I am using AsyncHttpClient, I can invoke client.close to stop the client.
val asyncHttpClient = new AsyncHttpClient()
println(asyncHttpClient.prepareGet("http://www.google.com"))
asyncHttpClient.close()
So current will be stopped. However, if I wrap the api call into the Task. I dont know how to stop it.
def get(s: String) = Task.async[Int](k => {
asyncHttpClient.prepareGet(s).execute(toHandler)
Thread.sleep(5000)
asyncHttpClient.closeAsynchronously()
} )
def toHandler[A] = new AsyncCompletionHandler[Response] {
def onCompleted(r: Response) = {
println("get response ", r.getResponseBody)
r
}
def onError(e: Throwable) = {
println("some error")
e
}
}
println(get("http://www.google.com").run)
The current Process is still running. I am thinking the reason is that Task and AsynClient are both async. I dont know what I should do to close it
Many thanks in advance
The problem is that
Task.async
takes a function that can register callbacks. This is a little confusing, and the types aren't much help because there's so much damnUnit
in there, but what it means here is that you want something more like this:This doesn't handle closing the client—it's just intended to show the general idea. You could close the client in the handler, but I'd suggest something more like this:
And then:
This avoids
closeAsynchronously
(which seems to be going away, anyway).