我将CXF和JAX-RS一起使用来构建RESTFul API,以为Web应用程序提供数据。我想知道是否可以记录请求X进入我的API,进行处理然后作为响应返回所花费的时间。
我已经将自己的CXF记录器定义为JAX-RS功能,因为<cxf:logging />
有点太多了。话虽这么说,我知道有一个请求程序记录器和一个响应记录器。我的web应用实际上记录了所有的请求/响应,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以从客户端跟踪时间并进行记录?
小智 5
该线程是一个旧的线程,但是共享了我在客户端使用的方法。
我们与服务器进行了基于SOAP的交换,因此我们的类扩展了AbstractSoapInterceptor
在handleMessage()中,我们找到消息的方向(入站或出站),并计算处理所花费的时间。
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
}
}
Run Code Online (Sandbox Code Playgroud)
<bean id="processingTimeInterceptor" class="ProcessingTimeInterceptor" />
<jaxws:client ...">
<jaxws:inInterceptors>
<ref bean="processingTimeInterceptor" />
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="processingTimeInterceptor" />
</jaxws:outInterceptors>
</jaxws:client>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1457 次 |
最近记录: |