我想知道如果我可以使用過濾器(在我的情況下CorsFilter)來衡量時間和把時間花在消息本身。我知道以下工作正常:如何使用過濾器來衡量性能?
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
long startTime = System.nanoTime();
chain.doFilter(request, response);
long endTime = System.nanoTime();
System.out.println("Time: " + (endTime - startTime));
}
哪一個當然會輸出以秒爲單位的總時間。我想把時間放在返回的響應的頭部,以便接收者可以查看頭部並查看處理花費了多長時間。以下代碼不起作用:
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
long startTime = System.nanoTime();
if(response instanceof HttpServletResponse) {
HttpServletResponse httpResp = (HttpServletResponse)response;
httpResp.addHeader("Start-time", Long.toString(startTime));
}
chain.doFilter(request, response);
long endTime = System.nanoTime();
if(response instanceof HttpServletResponse) {
HttpServletResponse httpResp = (HttpServletResponse)response;
httpResp.addHeader("End-time", Long.toString(endTime));
}
}
標題僅包含開始時間而非結束時間。我假設這是因爲消息已經發送,所以修改對象將不起作用。
有沒有人有一個優雅的/聰明的解決方案,通過使用過濾器將時間放入標題?
感謝, 菲爾
更新
我已經調查使用HttpServletResponseWrapper來解決這一解決方案。這仍然無法在響應頭上輸出XXX-EndTime或YYY-EndTime。
@Override
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
long startTime = System.nanoTime();
if(response instanceof HttpServletResponse) {
HttpServletResponse httpResp = (HttpServletResponse)response;
httpResp.addHeader("Access-Control-Allow-Origin", "*");
httpResp.addHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS");
httpResp.addHeader("Allow", "GET, HEAD, OPTIONS");
httpResp.addHeader("Access-Control-Allow-Headers", "*");
httpResp.addHeader("A-Runtime", Long.toString(startTime));
}
OutputStream out = response.getOutputStream();
GenericResponseWrapper wrapper = new GenericResponseWrapper((HttpServletResponse) response);
chain.doFilter(request,wrapper);
out.write(wrapper.getData());
if(response instanceof HttpServletResponse) {
HttpServletResponse httpResp = (HttpServletResponse)response;
httpResp.addHeader("XXX-EndTime", Long.toString(System.nanoTime() - startTime));
wrapper.addHeader("YYY-EndTime", Long.toString(System.nanoTime() - startTime));
}
out.close();
}
-1回答這個問題開了更多的問題,然後他們回答。 – Yevgeniy
@YevgeniyM。你是對的,應該只是一個評論,有這樣一個包裝類+1。 –
好吧,我已經使用HttpServletResponseWrapper實現了一個解決方案,但仍然無法使它工作!我將用我的新代碼更新上面的問題。 – Phil