本文介绍了Scala Dispatch库:如何处理连接失败或超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在客户端中使用Databinder Dispatch库获取简单的REST-ish API.我知道如何检测是否收到错误状态的HTTP响应:

I've been using the Databinder Dispatch library in a client for a simple REST-ish API. I know how to detect if I get an HTTP response with an error status:

Http x (request) {
    case (200, _, _, content) => successResult(content())
    case (404, _, _, _) => notFoundErrorResult
    case (_, _, _, _) => genericErrorResult
}

但是,由于域无效或连接失败,如何区分错误响应和根本无法获得任何响应?还有什么方法可以在仍然使用同步语义的情况下实现超时?如果API中有任何相关内容,我会错过它.

But how can I distinguish an error response from a failure to get any response at all, because of an invalid domain or failure to connect? And is there any way to implement a timeout while still using synchronous semantics? If there's anything relevant in the API, I've missed it.

推荐答案

还有一种使用Http.configure方法配置客户端的更优雅的方法,该方法接收Builder => Builder函数作为参数:

There is also a more elegant way to configure client using Http.configure method which receives Builder => Builder function as an argument:

val http = Http.configure(_.setAllowPoolingConnection(true).setConnectionTimeoutInMs(5000))

这篇关于Scala Dispatch库:如何处理连接失败或超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:50