围绕Java操作应用超时控制 [英] Apply timeout control around Java operation

查看:114
本文介绍了围绕Java操作应用超时控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第三方Java库与REST API进行交互。 REST API有时需要很长时间才能响应,最终导致抛出 java.net.ConnectException

I'm using a third party Java library to interact with a REST API. The REST API can sometimes take a long time to respond, eventually resulting in a java.net.ConnectException being thrown.

我想缩短超时时间,但无法修改第三方库。

I'd like to shorten the timeout period but have no means of modifying the third party library.

我想在周围应用某种形式的超时控制调用Java方法,以便我可以确定在什么时候放弃等待。

I'd like to apply some form of timeout control around the calling of a Java method so that I can determine at what point to give up waiting.

这与网络超时没有直接关系。我希望能够尝试执行操作,并能够在指定的等待时间后放弃。

This doesn't relate directly to network timeouts. I'd like to be able to try and perform an operation and be able to give up after a specified wait time.

以下内容绝不是有效的Java,但是从概念上证明了我想要实现的目标:

The following is by no means valid Java but does conceptually demonstrate what I'd like to achieve:

try {
    Entity entity = new Entity();
    entity.methodThatMakesUseOfRestApi();
} catch (<it's been ages now, I don't want to wait any longer>) {
    throw TimeoutException();
}


推荐答案

我建议 TimeLimiter

这篇关于围绕Java操作应用超时控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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