如何在scalaz中关闭AsyncHttpClient [英] How to close AsyncHttpClient in scalaz Task

查看:112
本文介绍了如何在scalaz中关闭AsyncHttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将AsyncHttpClient和Scalaz Task结合在一起。通常,如果我使用的是AsyncHttpClient,则可以调用client.close停止客户端。

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()

因此当前停了但是,如果我将api调用包装到Task中。我不知道如何阻止它。

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)

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

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

在此先感谢

推荐答案

问题在于 Task.async 带有可以注册回调的函数。这有点令人困惑,类型也无济于事,因为里面有太多该死的 Unit ,但这意味着您想要的是更多这样的东西:

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 damn Unit in there, but what it means here is that you want something more like this:

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)
    }
  )
)

这不能解决关闭客户的问题,它只是用来显示一般想法。您可以在处理程序中关闭客户端,但我建议使用类似这样的东西:

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:

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())

然后:

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)
  )
)

这样可以避免 closeAsynchronously (这似乎是消失)。

This avoids closeAsynchronously (which seems to be going away, anyway).

这篇关于如何在scalaz中关闭AsyncHttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆