0

我之前在 vertx 上编写了一个小系统,但直到现在才测试过 NetServer 和 NetClients 之间的通信。

即使我在本地编写了一个非常简单的程序,从 NetServer 向 NetClient 发送一条非常小的消息也需要 50-100ms。如果我删除了选项,情况会变得更糟。下面的例子。

与简单 Java ServerSocket/Socket 相同的测试情况以 0 毫秒的时间结束。

我阅读了大部分 vertx 文档,但没有发现任何提示我在这里遗漏的内容。

亲切的问候

public class NetServerTest extends AbstractVerticle {

    private NetServerOptions options = new NetServerOptions().setReceiveBufferSize(10000000).setSendBufferSize(1000000)
            .setTcpKeepAlive(true).setIdleTimeout(600);
    private NetServer server;

    public static void main(String[] args) throws IOException {
        Vertx.vertx().deployVerticle(new NetServerTest());
    }

    @Override
    public void start() {
        server = vertx.createNetServer(options);
        startServerSocket();
    }

    private void startServerSocket() {
        server.connectHandler(socket -> {

            socket.write(Buffer.buffer(System.currentTimeMillis() + ""));

            socket.closeHandler(v -> {
                System.out.println("The client socket has been closed");
            });

        });

        server.listen(5555, "0.0.0.0", res ->

        {
            if (res.succeeded()) {
                System.out.println("Server is now listening!");
            } else {
                System.out.println("Failed to bind!");
            }
        });
    }
}

public class NetClientTest extends AbstractVerticle {

    private static NetClientOptions options = new NetClientOptions().setConnectTimeout(10000)
            .setReconnectAttempts(Integer.MAX_VALUE).setReconnectInterval(10000).setReceiveBufferSize(1000000)
            .setSendBufferSize(10000000).setTcpKeepAlive(true).setIdleTimeout(600);

    public static void main(String[] args) throws UnknownHostException, IOException {
        Vertx.vertx().deployVerticle(new NetClientTest());
    }

    private NetClient client;

    @Override
    public void start() {
        client = vertx.createNetClient(options);

        connectClient();
    }

    private void connectClient() {
        client.connect(5555, "localhost", res -> {
            if (res.succeeded()) {
                NetSocket socket = res.result();

                socket.handler(inputBuffer -> {
                    System.out.println(
                            "took " + (System.currentTimeMillis() - Long.parseLong(inputBuffer.toString())) + "ms");
                });

                socket.closeHandler(v -> {
                    System.out.println("The server socket has been closed. Trying to connect again.");
                });

            } else {
                System.out.println("Failed to connect. Trying to connect again." + res.cause());
            }
        });
        System.out.println("connectClient end");
    }
}
4

0 回答 0