gpt4 book ai didi

asynchronous - 在所有异步请求中设置默认超时

转载 作者:行者123 更新时间:2023-12-04 04:59:21 25 4
gpt4 key购买 nike

我正在使用 @Suspended AsyncResponse response在我的请求中并启动线程来处理请求。进程完成后,我试图恢复响应,但 RestEasy 将请求标记为已完成,因为请求线程已完成且响应中未设置超时。如果我设置超时,它工作正常,但我需要在我想要实现的每个异步请求中设置超时。无论如何,是否可以将超时水平设置为我所有暂停的 AsyncRequests?

最佳答案

不幸的是,JAX-RS 2.0 specification , RESTEasy documentationJersey documentation不要提及为 AsyncResponse 设置默认超时的任何内容.

Jersey documentation提到以下内容:

By default, there is no timeout defined on the suspended AsyncResponse instance. A custom timeout and timeout event handler may be defined using setTimeoutHandler(TimeoutHandler) and setTimeout(long, TimeUnit) methods. The setTimeoutHandler(TimeoutHandler) method defines the handler that will be invoked when timeout is reached. The handler resumes the response with the response code 503 (from Response.Status.SERVICE_UNAVAILABLE). A timeout interval can be also defined without specifying a custom timeout handler (using just the setTimeout(long, TimeUnit) method).



因此,该解决方案不会与您已经使用的解决方案不同:

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

// Register a timeout handler
asyncResponse.setTimeoutHandler(new TimeoutHandler() {

@Override
public void handleTimeout(AsyncResponse asyncResponse) {
asyncResponse.resume(Response.status(SERVICE_UNAVAILABLE)
.entity("Operation timed out. Please try again.").build());
}
});

// Set timeout
asyncResponse.setTimeout(15, SECONDS);

// Execute long running operation in new thread
executor.execute(new Runnable() {

@Override
public void run() {
executeLongRunningOp();
asyncResponse.resume("Hello async world!");
}
});
}

关于asynchronous - 在所有异步请求中设置默认超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34047613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com