【问题标题】:How to add timeout in JAX-RS API如何在 JAX-RS API 中添加超时
【发布时间】:2019-05-26 04:38:05
【问题描述】:

我有一个可以长时间工作的 JAX-RS API,并且客户端通过 ajax 调用来调用 API。客户端收到 503 状态 - 50 秒后服务不可用。

如何增加此超时值。我尝试增加tomcat(托管API)中的连接超时。我也尝试在 ajax 调用中添加超时,但这也没有用。

【问题讨论】:

  • 您需要在客户端设置超时值。根据客户端的实现和版本,有不同的方法来设置超时。你用的是哪个客户端?可以分享一下代码吗?

标签: ajax timeout jax-rs http-status-code-503


【解决方案1】:

您可以使用Suspended 注释并创建一个TimeoutHandler

不确定是否需要使用此示例增加 tomcat 中的超时时间。

    public class Resource {

        private Executor executor = Executors.newSingleThreadExecutor();

        @GET
        public void asyncGet(@Suspended final AsyncResponse asyncResponse) {

            asyncResponse.setTimeoutHandler(new TimeoutHandler() {
                @Override
                public void handleTimeout(AsyncResponse asyncResponse) {
                    asyncResponse.resume("Processing timeout.");
                    executor.shutdown();
                }
            });

            asyncResponse.setTimeout(60, TimeUnit.SECONDS);
            executor.submit(() -> {
                String result = someService.expensiveOperation();
                asyncResponse.resume(result);
                executor.shutdown();
            });
        }
    }

球衣文档here

【讨论】:

  • 我尝试了 TimeoutHandler 并将超时设置为 5 分钟,但客户端仍然收到 503 - 50 秒后服务不可用。
猜你喜欢
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 2017-12-11
  • 2016-12-18
相关资源
最近更新 更多