LoginSignup
2
2

More than 5 years have passed since last update.

Finagleでのエラー処理の一例

Posted at
  • Finagle 6.28.+で確認。ClientBuilderを使う方法は現在は古い方法になっている

  • どこかのサーバに問い合わせに行く処理。エラーが発生した時は例外を投げたりせずにNOT_FOUNDの結果を返す

def query(param:String) :Future[HttpResponse] = {
    val retVal = new Promice[HttpResponse]

    val client: Service[HttpRequest, HttpResponse] = ClientBuilder().codec(http.Http()).
      hostConnectionLimit(32).hosts("hoge.com:8080").        
      tcpConnectTimeout(100.milliseconds).timeout(50.milliseconds).build()
    val request = RequestBuilder.safeBuildGet(RequestBuilder.create().url(s"http://hoge.com:8080?url=${java.net.URLEncoder.encode(param, "UTF-8")}"))

    val f1 = client(request).onSuccess{ f =>
      retVal.setValue(f)
    } onFailure { exc =>
      log.ifWarning(s"エラー発生: $param")
      val response = Response()
      response.setStatus(HttpResponseStatus.NOT_FOUND)
      retVal.setValue(response)
    }

    Await.ready(f1)
    retVal
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2