使用编年史网络图书馆,

Using chronicle network library,

您好,我正在尝试使用低级库编年史线发送 http 请求。我能够通过使用 java 套接字来实现这一点,但我很好奇使用这个库的差异。 调用代码如下:

private static void call() throws IOException, TimeoutException {
        final String desc = "google.com";
        TCPRegistry.createServerSocketChannelFor(desc);

        EventLoop eg = new EventGroup(true);
        eg.start();
        TcpChannelHub tcpChannelHub = new TcpChannelHub(null, eg, WireType.TEXT, "",
                SocketAddressSupplier.uri(desc), false, null, HandlerPriority.HIGH, new FatalFailureConnectionStrategy(0, false));
        final long tid = tcpChannelHub.nextUniqueTransaction(System.currentTimeMillis());

        final Wire wire = new TextWire(Bytes.elasticByteBuffer());

        wire.writeDocument(true, w -> w.write("tid").int64(tid));
        wire.write("GET / HTTP/1.1\r\n");
        wire.write("Host google.com\r\n");
        tcpChannelHub.writeSocket(wire, false, false);
        tcpChannelHub.proxyReply(TimeUnit.SECONDS.toMillis(1), tid);
    }

我收到以下错误:

20:26:43.275 [main/TcpChannelHub-Reads--(none)] DEBUG net.openhft.chronicle.network.connection.TcpChannelHub - connected to remoteAddress=(none)
20:26:43.279 [main] DEBUG net.openhft.chronicle.network.connection.TcpChannelHub - tid=1606937203253 of client request
20:26:44.279 [main] WARN net.openhft.chronicle.network.connection.TcpChannelHub - 
java.util.concurrent.TimeoutException: timeoutTimeMs=1000
    at net.openhft.chronicle.network.connection.TcpChannelHub$TcpSocketConsumer.syncBlockingReadSocket(TcpChannelHub.java:1210)
    at net.openhft.chronicle.network.connection.TcpChannelHub.proxyReply(TcpChannelHub.java:685)
    at com.example.demo.DemoApplication.call(DemoApplication.java:115)
    at com.example.demo.DemoApplication.main(DemoApplication.java:31)
20:26:44.280 [main] DEBUG net.openhft.chronicle.network.connection.TcpChannelHub - closing
net.openhft.chronicle.core.StackTrace: only added for logging - please ignore ! on main
    at net.openhft.chronicle.network.connection.TcpChannelHub.closeSocket(TcpChannelHub.java:502)
    at net.openhft.chronicle.network.connection.TcpChannelHub.proxyReply(TcpChannelHub.java:693)
    at com.example.demo.DemoApplication.call(DemoApplication.java:115)
    at com.example.demo.DemoApplication.main(DemoApplication.java:31)
20:26:44.281 [main] DEBUG net.openhft.chronicle.network.connection.TcpChannelHub - disconnected to remoteAddress=(none)

Exception in thread "main" java.util.concurrent.TimeoutException: timeoutTimeMs=1000

1000 毫秒应该足够了,所以问题出在配置的某个地方。

虽然我相信您可以让它工作,但它不是与 HTTP 一起使用的自然选择。

但是,如果这只是一个练习,您应该避免writingDocument 因为这会在 HTTP 不需要的内容之前添加一个 32 位长度。

createServerSocketChannelFor 方法用于生成用于测试目的的虚拟服务器,因此它会阻止您联系您想要的服务器。

我建议您使用专为 HTTP 设计的库,例如内置于 Java 中的库。