0

它设置为每 5 秒向服务器发送一次 PING。最初,每 5 秒发送一次 ping。自该计划开始以来已经过去了一个月。目前,ping 每秒向服务器发送 2-3 次。“vertx.setPeriodic”函数中似乎有一个BUG。

long[] pID = {0};
pID[0] = vertx.setPeriodic(5000, handler -> {       
    sendHTTP2PING(mapKey, conn);
});

private void sendHTTP2PING(String mapKey, HttpConnection conn) {
    conn.ping(pingData, pong -> {
        if(pong.succeeded()) {
            localAddressPort = conn.localAddress().port();
            logger.info("[HTTP/2 CLIENT PING] RECEIVED PONG");
        } else {
            logger.info("[HTTP/2 CLIENT PING] DOES NOT RECEIVED PONG...!!!");
            disconnectToServer(mapKey);
        }
    }).closeHandler(ch -> {
        disconnectToServer(mapKey);
    }).exceptionHandler(exh -> {
        disconnectToServer(mapKey);
    });
}

日志文件

2020-10-27 16:13:10 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:10 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:11 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:11 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:11 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:12 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:12 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:13 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:15 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:15 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:16 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:16 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:16 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:17 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:17 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:18 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:20 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

2020-10-27 16:13:20 [vert.x-eventloop-thread-2] - [HTTP/2 CLIENT PING] 没有收到 PONG ......!!!

4

1 回答 1

0

当然,这可能是一个错误,您可以打开一个新问题,指定 Vert.x 版本和任何重现它的方法。

不过,我想指出的是,您的代码不等于“每 5 秒打印一次消息”。它每 5 秒安排一次调用,是的,但它仅在删除服务响应或响应失败时打印出来。
这意味着,如果您的远程服务器以一些随机延迟响应,您将看到随机延迟的消息,甚至是乱序的。

我建议尝试改为使用setTimer。这样,您将确保不会创建出站请求队列。

pID[0] = vertx.setTimer(5000, handler -> {       
    sendHTTP2PING(mapKey, conn);
});

private void sendHTTP2PING(String mapKey, HttpConnection conn) {
    conn.ping(pingData, pong -> {
        if(pong.succeeded()) {
            localAddressPort = conn.localAddress().port();
            logger.info("[HTTP/2 CLIENT PING] RECEIVED PONG");
        } else {
            logger.info("[HTTP/2 CLIENT PING] DOES NOT RECEIVED PONG...!!!");
            disconnectToServer(mapKey);
        }
        vertx.setTimer(5000, handler -> {       
            sendHTTP2PING(mapKey, conn);
        });
    }).closeHandler(ch -> {
        disconnectToServer(mapKey);
    }).exceptionHandler(exh -> {
        disconnectToServer(mapKey);
    });
}
于 2020-10-27T09:38:18.900 回答