I want to compute per request the time between the begining of request and time connection is established (TCP connection establishment).
I asked this question question but it was ambiguous.
I had a look at proposed answer, maybe I misunderstood, but I understand that Connection.Listener works on a global basis at least (Overall connect time)
So I tried the approach based on SocketAddressResolver:
private static class CustomSocketAddressResolver implements SocketAddressResolver {
private StopWatch stopWatch = new StopWatch();
private SocketAddressResolver adaptee;
public CustomSocketAddressResolver(SocketAddressResolver adaptee) {
this.adaptee = adaptee;
}
public long getConnectTime() {
return stopWatch.getTime();
}
@Override
public void resolve(String host, int port, Promise<List<InetSocketAddress>> promise) {
stopWatch.reset();
stopWatch.start();
adaptee.resolve(host, port, new Promise<List<InetSocketAddress>>() {
@Override
public void succeeded(List<InetSocketAddress> result) {
// Add as first address an invalid address so that we test
// that the connect operation iterates over the addresses.
stopWatch.stop();
promise.succeeded(result);
}
@Override
public void failed(Throwable x) {
stopWatch.stop();
promise.failed(x);
}
});
}
}
Then I can get connect time like this:
((CustomSocketAddressResolver) httpClient.getSocketAddressResolver()).getConnectTime()
It works but is there a better way ?