我想測量調用RestTemplate.getForObject
調用的HTTP GET請求的時間,而不需要解析響應所需的時間。所以只要遠程HTTP調用需要。我已經嘗試設置ClientHttpRequestInterceptor
但我不認爲這是做正確的方式隨着時間似有不妥:測量Spring RestTemplate HTTP請求時間
public class PerfRequestSyncInterceptor implements ClientHttpRequestInterceptor {
private Logger log = Logger.getLogger(this.getClass());
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
long start = System.nanoTime();
ClientHttpResponse resp = execution.execute(request, body);
log.debug("remote request time: "
+ ((System.nanoTime() - start) * Math.pow(10, -9)));
return resp;
}
}
電話:
RestTemplate rest = new RestTemplate();
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(new PerfRequestSyncInterceptor());
rest.setInterceptors(interceptors);
Response inob = rest.getForObject(xmlURL, Response.class);
我如何測量的時間RestTemplate HTTP請求?
我有同樣的問題。您需要對響應對象進行方法調用以阻止執行,直到響應可用。 – jaydfwtx