Finagle - 将ClientBuilder()升级到新的API版本

时间:2015-05-11 00:12:36

标签: scala playframework finagle

我有一个Scala Play项目 - Finagle和ElasticSearch

我正在使用具有旧版API的Finagle客户端,如下所示:

val client = ClientBuilder()
  .codec(Http)
  .hosts("localhost:10000,localhost:10001,localhost:10003")
  .hostConnectionLimit(1)
  .build()

这是我的代码:

https://gist.github.com/hectorgool/f217d16e2c15b122d7a7

并且工作正常,但现在我想将我的代码升级到新的API版本,如下所示:

val client = Http.newService("localhost:10000,localhost:10001")

我的代码的新版本在这里:

https://github.com/hectorgool/es-client/blob/master/app/lib/FinagleClient.scala

但是,现在我的项目没有编译,我对这一行(111)有错误:

  

111:val client = clientFactory.apply()()

我不知道如何解决它

1 个答案:

答案 0 :(得分:0)

解决

我改变了这个:

  val clientFactory: ServiceFactory[HttpRequest, HttpResponse] = ClientBuilder()
    .codec(Http())
    .hosts(hosts)
    .tcpConnectTimeout(1.second)
    .hostConnectionLimit(1)
    .buildFactory() 

为此:

val clientFactory: Service[HttpRequest, HttpResponse] = Http.newService(hosts) 

我对此很不满意:

val client = clientFactory.apply()()

我改变了这个:

httpResponse.onSuccess{
  response =>
    Logger.debug("Received response: " + response)
    client.close()
}.onFailure{ err: Throwable =>
    Logger.error(err.toString)      
    client.close()
}

为此:

httpResponse.onSuccess{
  response =>
    Logger.debug("Received response: " + response)
}.onFailure{ err: Throwable =>
    Logger.error(err.toString)      
}

问题的原因是:

client.close()

因为关闭连接