首页 > 解决方案 > 如果主机不可用,Vert.x 3.7.0 HttpRequest sendForm()/sendBuffer()/sendJson() 方法永远不会返回

问题描述

Vert.x 3.7.0 和 3.7.1 有一个奇怪的问题:如果主机不可用,在HttpRequest请求正文中传输数据的 send 方法(sendForm(),等 - 所有除了)永远不会返回。sendJson()sendBuffer()send()

如果端口 4012 上没有服务器,则以下代码永远不会返回:

        HttpClientOptions options = new HttpClientOptions()
                .setDefaultHost("localhost")
                .setDefaultPort(4012);

        Vertx vertx = Vertx.vertx();
        WebClient webClient = WebClient.create(vertx, new WebClientOptions(options));

        HttpRequest<Buffer> request = webClient.request(HttpMethod.POST, "/test");

        logger.info("Sending request as sendForm()");
        request.sendForm(MultiMap.caseInsensitiveMultiMap(), event -> {
            if (event.failed()) {
                logger.error("Failed!", event.cause());
            } else {
                logger.info("Succeeded! Code {} {}", event.result().statusCode(), event.result().statusMessage());
            }
            vertx.close();
        });

如果我request.send(event -> {...})改用,那么它会打印(如预期的那样):

27.06.19 16:47:17,352 ERROR - Failed! io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: localhost/127.0.0.1:4012
    at java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:779)
    at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:327)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:340)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:632)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:579)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:496)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:458)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.ConnectException: Connection refused: no further information
    ... 11 more

3.7.0 之前的 Vert.x 没有问题:我尝试了 3.6.3,并且send()sendForm()使用java.net.ConnectException: Connection refused: no further information. 这就是为什么我怀疑这是一个错误(在 Vert.x 中,或者可能在底层 io.netty 中),除非我在我的代码中做一些未定义行为的事情。

这里有来自 Vert.x 社区​​的人吗?:)

提前感谢您的帮助。

PS 我在 Windows 10 上,Java 使用 JVM Amazon Corretto 11.0.3。

标签: javahttprequestvert.x

解决方案


我曾经在 Windows 10 上遇到过这种情况。我禁用了防火墙,然后重新启动了 Vert.x 应用程序,这就是魔法。


推荐阅读