Jersey records all service response times

I have a Java application and I want to log all the execution time for each REST service.

How do I measure the lead time for each request? How will the filter be configured?

+3


source to share


2 answers


I added a filter for all service requests and it works great:



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");        
}

      

+1


source


Jersey event listeners

should be what you need.

There are two options for this event listeners

, as stated in their monitoring and tracking docs :



  • ApplicationEventListener

    to listen for application events, and
  • RequestEventListener

    to listen for request processing events.

Only the first type, ApplicationEventListener, can be directly registered as an application provider. RequestEventListener is for each specific request and can only be returned from the ApplicationEventListener itself.

+4


source







All Articles