2013-11-21 71 views
2

我使用CXF和JAX-RS一起构建RESTFul API以为我的Web应用程序提供数据。我想知道是否有可能记录请求X转到我的API所需的时间,并进行处理,然后作为响应返回。CXF日志响应时间

我已经将自己的CXF记录器定义为JAX-RS功能,因为<cxf:logging />有点太多了。话虽如此,我知道有一个记录器的请求记录器,并有一个响应记录器。我的webapps实际登录的所有这样的请求/响应:

11-21 10:37:01,052 INFO [-8080-exec-7] +- CXF Request -- ID : [24], Address : [http://my.api.com], HTTP Method : [GET] @org.apache.cxf.interceptor.LoggingOutInterceptor 
11-21 10:37:01,089 INFO [-8080-exec-7] +- CXF Response -- ID : [24], Response Code : [200]  @org.apache.cxf.interceptor.LoggingInInterceptor 

有没有一种方法,我可以跟踪从客户端的时间和日志呢?

+0

我使用AOP与拦截器周围的建议来计算的时间(主要是我需要审计的目的 –

+0

任何更新,大卫? –

+0

我们放弃了这个想法。我正在实习,但我可能会在几周后再次来到这里,也许他们找到了方法,我会更新它。 – David

回答

2

线程是旧的,但共享我在客户端采取的方法。

  1. 我们必须与服务器的基于SOAP的交流,使我们的类扩展AbstractSoapInterceptor

  2. 在的handleMessage()我们发现消息(inboud或出站)的方向,并计算时间的处理需要。


public void handleMessage(SoapMessage message) throws Fault { 
     try { 
      boolean isOutbound = MessageUtils.isOutbound(message); 
      if (isOutbound) { 
       // outgoing 
       long requestSentTime = System.currentTimeMillis(); 

      LOGGER.trace("Sending request to server at {} milliseconds", requestSentTime); 
      message.getExchange().put(ApplicationConstants.CXF_REQUEST_TIME, requestSentTime); 
     } else { 
      // incoming 
      long requestSentTime = (long) message.getExchange().get(ApplicationConstants.CXF_REQUEST_TIME); 
      long requestReceiveTime = System.currentTimeMillis(); 

      LOGGER.trace("Receiving request from server at {} milliseconds", requestReceiveTime); 
      long executionTime = requestReceiveTime - requestSentTime; 
      LOGGER.info("Server execution time in milliseconds was {}", executionTime); 
     } 
    } catch (Exception e) { 
     LOGGER.error("handleMessage() threw exception {} ", e); 
     // Log and do nothing 
    } 

} 
  • 加入作为进出拦截到Spring应用程序上下文的xml

  • <bean id="processingTimeInterceptor" class="ProcessingTimeInterceptor" />  
    <jaxws:client ..."> 
         <jaxws:inInterceptors> 
          <ref bean="processingTimeInterceptor" />    
         </jaxws:inInterceptors> 
         <jaxws:outInterceptors> 
          <ref bean="processingTimeInterceptor" />    
         </jaxws:outInterceptors> 
        </jaxws:client>