PlayWS - 如何在达到请求超时时抛出异常?
PlayWS - How to throw an exception when Request Timeout is reached?
使用 CompletableFuture
使用 PlayWS 执行长时间下载时,有时会达到定义的请求超时。发生这种情况时,PlayWS 似乎不会抛出异常(至少在我的配置中如此),因此下载不会被标记为失败并且会被处理,尽管数据已损坏。
请原谅这种可恶的代码:
final CompletionStage<WSResponse> futureResponse = this.playWS.client
.url(importSource.getDownloadUrl())
.setMethod(HttpMethod.GET)
.setRequestTimeout(Duration.ofSeconds(5)) // When the timeout is reached, the download gets canceled
.stream();
try {
futureResponse
.thenAccept(res -> {
try (OutputStream outputStream = Files.newOutputStream(file.toPath())) {
final Source<ByteString, ?> responseBody = res.getBodyAsSource();
final Sink<ByteString, CompletionStage<Done>> outputWriter =
Sink.foreach(bytes -> {
outputStream.write(bytes.toArray());
});
responseBody
.runWith(outputWriter, this.playWS.materializer)
.whenComplete((value, error) -> {
System.out.println("VALUE: "+value); // == "Done"
System.out.println("Error: "+error); // == null
})
.exceptionally(exception -> {
throw new IllegalStateException("Download failed for: " + importSource.getDownloadUrl(), exception);
})
.toCompletableFuture().join();
} catch (final IOException e) {
throw new IllegalStateException("Couldn't open or write to OutputStream.", e);
}
})
.exceptionally(exception -> {
throw new IllegalStateException("Download failed for: " + importSource.getDownloadUrl(), exception);
})
.toCompletableFuture().get();
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException("Couldn't complete CompletableFuture.", e);
}
我是不是做错了什么或者这是一个错误?
我看到的唯一解决方案是:
- 计算接收到的字节数并将它们与
Content-Length header
. 进行比较
- 将请求超时设置为 -1(无限期)。
感谢任何建议。
我认为有些过于复杂了。
您可以只从 Future
或 CompletableStage
附加到 Source
。 Akka Streams API 比 CompletableFuture
更强大(我的意见)
final CompletionStage<WSResponse> futureResponse = this.playWS.client
.url(importSource.getDownloadUrl())
.setMethod(HttpMethod.GET)
.setRequestTimeout(Duration.ofSeconds(5))
.stream();
Source<WSResponse> source = Source.fromCompletableStage(futureResponse);
source.map(...).filter(...).recover(...).runforeach(..., playWS.materializer)
使用 CompletableFuture
使用 PlayWS 执行长时间下载时,有时会达到定义的请求超时。发生这种情况时,PlayWS 似乎不会抛出异常(至少在我的配置中如此),因此下载不会被标记为失败并且会被处理,尽管数据已损坏。
请原谅这种可恶的代码:
final CompletionStage<WSResponse> futureResponse = this.playWS.client
.url(importSource.getDownloadUrl())
.setMethod(HttpMethod.GET)
.setRequestTimeout(Duration.ofSeconds(5)) // When the timeout is reached, the download gets canceled
.stream();
try {
futureResponse
.thenAccept(res -> {
try (OutputStream outputStream = Files.newOutputStream(file.toPath())) {
final Source<ByteString, ?> responseBody = res.getBodyAsSource();
final Sink<ByteString, CompletionStage<Done>> outputWriter =
Sink.foreach(bytes -> {
outputStream.write(bytes.toArray());
});
responseBody
.runWith(outputWriter, this.playWS.materializer)
.whenComplete((value, error) -> {
System.out.println("VALUE: "+value); // == "Done"
System.out.println("Error: "+error); // == null
})
.exceptionally(exception -> {
throw new IllegalStateException("Download failed for: " + importSource.getDownloadUrl(), exception);
})
.toCompletableFuture().join();
} catch (final IOException e) {
throw new IllegalStateException("Couldn't open or write to OutputStream.", e);
}
})
.exceptionally(exception -> {
throw new IllegalStateException("Download failed for: " + importSource.getDownloadUrl(), exception);
})
.toCompletableFuture().get();
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException("Couldn't complete CompletableFuture.", e);
}
我是不是做错了什么或者这是一个错误?
我看到的唯一解决方案是:
- 计算接收到的字节数并将它们与
Content-Length header
. 进行比较
- 将请求超时设置为 -1(无限期)。
感谢任何建议。
我认为有些过于复杂了。
您可以只从 Future
或 CompletableStage
附加到 Source
。 Akka Streams API 比 CompletableFuture
更强大(我的意见)
final CompletionStage<WSResponse> futureResponse = this.playWS.client
.url(importSource.getDownloadUrl())
.setMethod(HttpMethod.GET)
.setRequestTimeout(Duration.ofSeconds(5))
.stream();
Source<WSResponse> source = Source.fromCompletableStage(futureResponse);
source.map(...).filter(...).recover(...).runforeach(..., playWS.materializer)