3

我正在尝试使用来自 JDK10 的新的闪亮 httpclient。代码非常简单。我使用多个线程的同步 HTTP 调用:

private final HttpClient httpClient = HttpClient.newBuilder()
        .executor(Utils.newFixedThreadPoolExecutor(1, "HttpClient"))
        .build();

private JsonObject useHttpClient(URL url, String params) throws Exception {
    HttpRequest req = HttpRequest.newBuilder()
            .uri(url.toURI())
            .setHeader("Connection", "keep-alive")
            .setHeader("Accept-Encoding", "gzip")
            .timeout(timeout)
            .POST(HttpRequest.BodyPublisher.fromString(params))
            .build();
    HttpResponse<InputStream> response = httpClient.send(req, HttpResponse.BodyHandler.asInputStream());
    if (response.statusCode() != 200) {
        throw new IOException("Server returned " + response.statusCode());
    }
    String encoding = response.headers().firstValue("content-encoding").orElse("");
    return parseResponseStream(encoding, response.body());
}

有时我会得到 NPE:

java.lang.NullPointerException: null
at jdk.incubator.http.internal.hpack.HeaderTable$Table.remove(HeaderTable.java:455) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.internal.hpack.HeaderTable.evictEntry(HeaderTable.java:264) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.internal.hpack.HeaderTable.put(HeaderTable.java:233) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.internal.hpack.HeaderTable.put(HeaderTable.java:215) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.internal.hpack.Decoder.resumeLiteralWithIndexing(Decoder.java:464) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.internal.hpack.Decoder.proceed(Decoder.java:268) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.internal.hpack.Decoder.decode(Decoder.java:246) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.Http2Connection.decodeHeaders(Http2Connection.java:471) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.Http2Connection.processFrame(Http2Connection.java:635) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.internal.frame.FramesDecoder.decode(FramesDecoder.java:156) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.Http2Connection$FramesController.processReceivedData(Http2Connection.java:195) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.Http2Connection.asyncReceive(Http2Connection.java:528) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.Http2Connection$Http2TubeSubscriber.processQueue(Http2Connection.java:1054) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.internal.common.SequentialScheduler$SynchronizedRestartableTask.run(SequentialScheduler.java:175) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.internal.common.SequentialScheduler$CompleteRestartableTask.run(SequentialScheduler.java:147) ~[jdk.incubator.httpclient:?]
at jdk.incubator.http.internal.common.SequentialScheduler$SchedulableTask.run(SequentialScheduler.java:198) ~[jdk.incubator.httpclient:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135) [?:?]

我的代码有问题吗?这个例外的原因是什么?

C:\Program Files\Java\jdk-10\bin>java -version
java version "10" 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)
4

1 回答 1

3

我的代码有问题吗?

作为熟悉 JDK HTTP 客户端 API 的人,我看不出您发布的代码有任何具体问题。

这个例外的原因是什么?

异常的原因是 JDK HTTP Client HPACK 代码中的错误。在您的代码中,您无能为力来解决它。

JDK HPACK bug 的具体信息可以在https://bugs.openjdk.java.net/browse/JDK-8200625找到(技术资料重复,来自前面提到的OpenJDK JIRA issue,这里不会增值,很可能变成很快就过时了)

于 2018-04-04T14:23:51.340 回答