【问题标题】:How to handle timeout for service call如何处理服务调用超时
【发布时间】:2017-11-22 10:36:42
【问题描述】:

我有一个基于 struts 的应用程序,我在其中调用我的 Restful Web 服务。我的实际服务调用如下所示:

      RestTemplate restTemplate = new RestTemplate();
      ResponseEntity<String> response = restTemplate.exchange(testUrl, httpMethod, entity, String.class);

通话正常,但我想处理这种情况,以防万一我的服务中断,我想在 1 分钟内超时,而不是等待这么长时间。

【问题讨论】:

标签: java web-services timeout


【解决方案1】:

您可以选择提供超时处理方法的 Spring Rest 模板:

连接超时:

RestTemplate restTemplate = new RestTemplate();
((SimpleClientHttpRequestFactory)restTemplate.getRequestFactory()).setConnectTimeout(2000);

读取超时:

((SimpleClientHttpRequestFactory)restTemplate.getRequestFactory()).setReadTimeout(2000);

有关更多信息,请访问文档页面: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/client/SimpleClientHttpRequestFactory.html

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2022-07-29
    • 2015-07-02
    • 2017-01-30
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    相关资源
    最近更新 更多