Java请求挂起什么原因,Java 9 HttpClient挂起

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 response

= client.send(request, HttpResponse.BodyHandler.asString());

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

Debugging shows it infinitely waits in method waitUntilPrefaceSent().

解决方案

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

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值