I have a Java application and I want to log all execution times for each REST service.
How to measure execution time for each request? How the filter will be configured?
I have a Java application and I want to log all execution times for each REST service.
How to measure execution time for each request? How the filter will be configured?
I added a filter to all service requests an it works fine:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain p_filterChain) throws IOException, ServletException {
long startTime = System.currentTimeMillis();
String url = "unknown";
if (request instanceof HttpServletRequest) {
url = ((HttpServletRequest)request).getRequestURL().toString();
String queryString = ((HttpServletRequest)request).getQueryString();
if(queryString != null)
url += "?" + queryString;
}
p_filterChain.doFilter(request, response);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
LogManager logm = new LogManager();
logm.newLog(url,elapsedTime);
System.out.println("Request: " + url + " " + elapsedTime + "ms");
}
Jersey event listeners
must be what you're after.
There are two flavors of such event listeners
as mentioned in their docs on monitoring and tracing:
ApplicationEventListener
for listening to application events, andRequestEventListener
for listening to events of request processingOnly the first type, ApplicationEventListener can be directly registered as an application-wide provider. The RequestEventListener is designed to be specific to every request and can be only returned from the ApplicationEventListener as such.
I added a filter to all service requests an it works fine:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain p_filterChain) throws IOException, ServletException {
long startTime = System.currentTimeMillis();
String url = "unknown";
if (request instanceof HttpServletRequest) {
url = ((HttpServletRequest)request).getRequestURL().toString();
String queryString = ((HttpServletRequest)request).getQueryString();
if(queryString != null)
url += "?" + queryString;
}
p_filterChain.doFilter(request, response);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
LogManager logm = new LogManager();
logm.newLog(url,elapsedTime);
System.out.println("Request: " + url + " " + elapsedTime + "ms");
}
© 2022 - 2024 — McMap. All rights reserved.