Java 9 HttpClient挂起 [英] Java 9 HttpClient hangs

查看:104
本文介绍了Java 9 HttpClient挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jdk 9-ea+171中的HTTP/2客户端.该代码摘自此示例:

I'm experimenting with HTTP/2 client from jdk 9-ea+171. The code is taken from this example:

HttpClient client = HttpClient.newHttpClient();

HttpRequest request = HttpRequest.newBuilder()
    .uri(new URI("https://www.google.com/"))
    .build();

HttpResponse<String> response
        = client.send(request, HttpResponse.BodyHandler.asString());

但是客户端永远挂在最后一行.请提出如何解决的建议?

But the client hangs on the last line forever. Please advice how to fix it?

调试显示它在方法waitUntilPrefaceSent()中无限等待.

Debugging shows it infinitely waits in method waitUntilPrefaceSent().

推荐答案

这是最新版本的HTTP2连接实现中的错误.在以前的版本中不会发生.

This is a bug in the latest build's implementation of a HTTP2 connection. It does not occure with previous builds.

首先,您需要指定GET方法,以避免出现空指针异常.

First of all, you need to specify the GET method to avoid getting a null pointer exception.

发生的事情是主线程正在等待发送连接序.它锁定一个倒数锁存器,以等待该序言的接收.为了唤醒自己,任何HttpClient都会创建一个帮助程序线程来读取传入的流量.该线程应该唤醒主线程,但是有时这种情况永远不会发生.如果经常运行示例,您会发现有时这样做是可行的.我想这是一场阅读序言的比赛.

What happens is that the main thread is waiting for the connection preface to be sent. It locks a count down latch to await the receival of this preface. In order to wake itself up, any HttpClient creates a helper thread that reads incoming traffic. This thread is supposed to wake up the main thread but sometimes, this never happens. If you run your example, often enough, you will see that this sometimes work. I guess there is a race for reading the preface.

不幸的是,序言的阅读也不考虑任何超时,因此除了中断主线程外,没有其他方法可以唤醒主线程.

Unfortunately, the reading of the preface does not respect any timeout either, so there is no way of waking up the main thread, other than interrupting the main thread.

这是一张官方机票: https://bugs.openjdk.java.net /browse/JDK-8181430

这篇关于Java 9 HttpClient挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆