4

How can I handle timeout exception in play ws client? When I specify RequestTimeout and process stream, CompletionStage finishes successfully even though there are exception.

See below for an example.

Code (taken from 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");

I see in logs that java.util.concurrent.TimeoutException and o.a.netty.timeout.TimeoutTimerTask are thrown, but futureResponse completes successfully. Is there any way to catch that error?

My problem is that I am trying to return that response to the user though controller and that simply hangs because not all the data was transmitted.

[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
Tigran
  • 1,049
  • 3
  • 15
  • 31

1 Answers1

0

Even I was facing timeout exception in Play 1.2 as the api was taking long time to send back a response. I was able to manually set the timeout which solved my issue.

WSRequest request = WS.url(your_url).timeout("600s");
skypjack
  • 49,335
  • 19
  • 95
  • 187
Neil
  • 821
  • 9
  • 17
  • 2
    I do not recall details, but that does not work. I have set all possible timeouts there, but exception will be thrown at the lower level and there was no way for me to intercept that. I finished up with Apache Async Http Client which I called directly. There is a way to override exception handler there. – Tigran Jun 16 '17 at 18:20