gpt4 book ai didi

playframework - Play框架处理请求超时

转载 作者:行者123 更新时间:2023-12-02 19:08:20 25 4
gpt4 key购买 nike

如何处理 play ws 客户端中的超时异常?当我指定 RequestTimeout 并处理流时,CompletionStage 成功完成,即使存在异常。

请参阅下面的示例。

代码(取自 here ):

// Make the request, which should timeout
CompletionStage<StreamedResponse> futureResponse = ws.url("https://download.docker.com/mac/stable/Docker.dmg")
.setMethod("GET").setRequestTimeout(1000)
.stream();

CompletionStage<Long> bytesReturned = futureResponse.thenCompose(res -> {
Source<ByteString, ?> responseBody = res.getBody();

// Count the number of bytes returned
Sink<ByteString, CompletionStage<Long>> bytesSum = Sink.fold(0L, (total, bytes) ->
{
long len = bytes.toArray().length;
return total + len;
});

return responseBody.runWith(bytesSum, materializer);
}).handle((res, err) -> {
LOGGER.info("Res = {}, err = {}", res, err);
return 200L;
}).exceptionally(ex -> {
LOGGER.error("See exception");
return 100L;
});
try {
bytesReturned.toCompletableFuture().get();
LOGGER.info("Got response");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LOGGER.error("Step completed");

我在日志中看到抛出了 java.util.concurrent.TimeoutExceptiono.a.netty.timeout.TimeoutTimerTask,但 futureResponse 成功完成。有什么方法可以捕获该错误吗?

我的问题是,我试图通过 Controller 将该响应返回给用户,但由于并非所有数据都已传输,所以该响应只是挂起。

[debug] i.n.u.i.JavassistTypeParameterMatcherGenerator - Generated: io.netty.util.internal.__matchers__.org.asynchttpclient.HttpResponseBodyPartMatcher
[debug] o.a.netty.channel.DefaultChannelPool - Closed 0 connections out of 0 in 0 ms
[debug] o.a.netty.timeout.TimeoutTimerTask - Request timeout to download.docker.com/52.84.239.124:443 after 1000 ms for NettyResponseFuture{currentRetry=0,
isDone=0,
isCancelled=0,
asyncHandler=play.api.libs.ws.ahc.Streamed$DefaultStreamedAsyncHandler@376665d1,
nettyRequest=org.asynchttpclient.netty.request.NettyRequest@39a19d4d,
future=java.util.concurrent.CompletableFuture@3d9e0ea1[Not completed],
uri=https://download.docker.com/mac/stable/Docker.dmg,
keepAlive=true,
redirectCount=0,
timeoutsHolder=org.asynchttpclient.netty.timeout.TimeoutsHolder@789c9776,
inAuth=0,
statusReceived=1,
touch=1491606083196} after 1075 ms
[debug] o.a.netty.channel.ChannelManager - Closing Channel [id: 0x66ec6bbd, L:/10.155.124.116:52337 - R:download.docker.com/52.84.239.124:443]
[debug] o.a.netty.request.NettyRequestSender - Aborting Future NettyResponseFuture{currentRetry=0,
isDone=0,
isCancelled=0,
asyncHandler=play.api.libs.ws.ahc.Streamed$DefaultStreamedAsyncHandler@376665d1,
nettyRequest=org.asynchttpclient.netty.request.NettyRequest@39a19d4d,
future=java.util.concurrent.CompletableFuture@3d9e0ea1[Not completed],
uri=https://download.docker.com/mac/stable/Docker.dmg,
keepAlive=true,
redirectCount=0,
timeoutsHolder=org.asynchttpclient.netty.timeout.TimeoutsHolder@789c9776,
inAuth=0,
statusReceived=1,
touch=1491606083196}

[debug] o.a.netty.request.NettyRequestSender - Request timeout to download.docker.com/52.84.239.124:443 after 1000 ms
java.util.concurrent.TimeoutException: Request timeout to download.docker.com/52.84.239.124:443 after 1000 ms
at org.asynchttpclient.netty.timeout.TimeoutTimerTask.expire(TimeoutTimerTask.java:43)
at org.asynchttpclient.netty.timeout.RequestTimeoutTimerTask.run(RequestTimeoutTimerTask.java:48)
at io.netty.util.HashedWheelTimer$HashedWheelTimeout.expire(HashedWheelTimer.java:588)
at io.netty.util.HashedWheelTimer$HashedWheelBucket.expireTimeouts(HashedWheelTimer.java:662)
at io.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:385)
at java.lang.Thread.run(Thread.java:745)
[debug] o.a.netty.handler.HttpHandler - Channel Closed: [id: 0x66ec6bbd, L:/10.155.124.116:52337 ! R:download.docker.com/52.84.239.124:443] with attribute INSTANCE
[info] controllers.RedirectController - Res = 2556824, err = null
[info] controllers.RedirectController - Got response
[error] controllers.RedirectController - Step completed

最佳答案

即使我在 Play 1.2 中也遇到了超时异常,因为 api 需要很长时间才能发回响应。我能够手动设置超时来解决我的问题。

WSRequest request = WS.url(your_url).timeout("600s");

关于playframework - Play框架处理请求超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43288367/

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