如何获取使用 Jetty Client 连接所需的时间

问题描述

我正在使用 Jetty Client 11,想计算建立连接所需的时间。

我正在使用高级 HttpClient 。 我已阅读此文档,但没有看到有关如何执行此操作的任何信息:

我的猜测是这个信息在 Connection#onOpen 中,但我没有看到如何插入我的代码

ClientConnector connector = new ClientConnector();
connector.setConnectTimeout(Duration.ofSeconds(1));
connector.setIdleTimeout(Duration.ofSeconds(1));
ClientConnectionFactory.Info http1 = HttpClientConnectionFactory.HTTP11;

HTTP2Client http2Client = new HTTP2Client(connector);
ClientConnectionFactoryOverHTTP2.HTTP2 http2 = new 
          ClientConnectionFactoryOverHTTP2.HTTP2(http2Client);
HttpClientTransportDynamic transport = new HttpClientTransportDynamic(connector,http2,http1);

HttpClient httpclient = new HttpClient(transport);

QueuedThreadPool threadPool = new QueuedThreadPool(3);
threadPool.setName("test");
httpClient.setExecutor(threadPool);
httpClient.start();

解决方法

这取决于您所说的“建立连接”是什么意思。

如果您的意思只是建立 TCP 连接,您可能需要包装/子类化 UUID 并包装/覆盖 SocketAddressResolver

您可以轻松保存调用resolve(host,port,promise)时的开始时间,并记录承诺完成时的停止时间。

或者,您可以将 resolve() 作为 bean 添加到 Connection.Listener

HttpClient

如果你的意思是TCP连接建立+TLS握手,你可以用一个HttpClient httpclient = new HttpClient(transport); httpClient.addBean(new Connection.Listener() { @Override public void onOpened(Connection connection) { ... } }); 来知道TLS握手什么时候完成,也把它作为bean加入:

SslHandshakeListener

另请查看 HttpClient httpclient = new HttpClient(transport); httpClient.addBean(new SslHandshakeListener() { @Override public void handshakeSucceeded(Event event) { ... } }); org.eclipse.jetty.io.ConnectionStatistics 以获取记录有关连接的大量统计信息的实用程序类。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...