如何在scalaz Task中关闭AsyncHttpClient

Xia*_*ong 2 scala scalaz asynchttpclient

我试图将AsyncHttpClient和Scalaz Task结合在一起.通常,如果我使用AsyncHttpClient,我可以调用client.close来停止客户端.

val asyncHttpClient = new AsyncHttpClient()
println(asyncHttpClient.prepareGet("http://www.google.com"))

asyncHttpClient.close()
Run Code Online (Sandbox Code Playgroud)

所以当前将停止.但是,如果我将api调用包装到Task中.我不知道如何阻止它.

  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)
Run Code Online (Sandbox Code Playgroud)

当前进程仍在运行.我在想,原因是Task和AsynClient都是异步的.我不知道应该怎么做才能关闭它

提前谢谢了

Tra*_*own 5

问题是Task.async需要一个可以注册回调的函数.这有点令人困惑,类型没有多大帮助,因为那里有太多该死Unit的,但这里的意思是你想要更像这样的东西:

import com.ning.http.client._
import scalaz.syntax.either._
import scalaz.concurrent.Task

val asyncHttpClient = new AsyncHttpClient()

def get(s: String): Task[Response] = Task.async[Response](callback =>
  asyncHttpClient.prepareGet(s).execute(
    new AsyncCompletionHandler[Unit] {
      def onCompleted(r: Response): Unit = callback(r.right)
      def onError(e: Throwable): Unit = callback(e.left)
    }
  )
)
Run Code Online (Sandbox Code Playgroud)

这不会处理关闭客户端 - 它只是为了显示一般的想法.你可以在处理程序中关闭客户端,但我建议更像这样的东西:

import com.ning.http.client._
import scalaz.syntax.either._
import scalaz.concurrent.Task

def get(client: AsyncHttpClient)(s: String): Task[Response] =
  Task.async[Response](callback =>
    client.prepareGet(s).execute(
      new AsyncCompletionHandler[Unit] {
        def onCompleted(r: Response): Unit = callback(r.right)
        def onError(e: Throwable): Unit = callback(e.left)
      }
    )
  )

def initClient: Task[AsyncHttpClient] = Task(new AsyncHttpClient())
def closeClient(client: AsyncHttpClient): Task[Unit] = Task(client.close())
Run Code Online (Sandbox Code Playgroud)

然后:

val res = for {
  c <- initClient
  r <- get(c)("http://www.google.com")
  _ <- closeClient(c)
} yield r

res.unsafePerformAsync(
  _.fold(
    _ => println("some error"),
    r => println("get response " + r.getResponseBody)
  )
)
Run Code Online (Sandbox Code Playgroud)

这避免了closeAsynchronously(无论如何似乎都会消失).