如果 blaze 客户端无法连接到服务,我会尝试以虚拟响应进行响应http4s
,但我无法弄清楚如何处理池错误。
设置连接池
import java.util.concurrent._
private val uncaughtExceptionHandler = new Thread.UncaughtExceptionHandler {
// This should have handled it ideally? But this log never gets printed
override def uncaughtException(t: Thread, e: Throwable) = {
logger.error("Uncaught exception")
}
}
val executor: ExecutorService = new ForkJoinPool(
Runtime.getRuntime().availableProcessors(),
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
uncaughtExceptionHandler,
true
)
并使用这个池执行器调用 REST API——</p>
从 URI 获取
val client = PooledHttp1Client(
maxTotalConnections = 20,
config = BlazeClientConfig.defaultConfig.copy(customExecutor = Some(Context.executor)) // same as the executor defined above
)
// Make a call
client.fetch(Request(uri = uri)) { res =>
{
if (res.status.code === Status.Ok.code)
// do something
else
// do something else
}
但是,如果uri
指定的 inclient.fetch
是我无法捕获的不可到达的端点。
错误
ERROR org.http4s.client.PoolManager Error establishing client connection for key RequestKey(http,localhost:3000)
java.net.ConnectException: Connection refused
client.fetch
结果,由于连接池本身会引发错误,因此我无法使用调用的回退进行响应。我该如何处理这些?