gpt4 book ai didi

CXF 日志响应时间

转载 作者:行者123 更新时间:2023-12-03 05:35:17 27 4
gpt4 key购买 nike

我使用 CXF 和 JAX-RS 构建 RESTFul API 来为我的 Web 应用程序提供数据。我想知道是否可以记录请求 X 发送到我的 API、进行处理然后作为响应返回所需的时间。

我已经将自己的 CXF 记录器定义为 JAX-RS 功能,因为 <cxf:logging />有点太多了。话虽如此,我知道有一个用于请求记录器的记录器,并且有一个响应记录器。我的网络应用程序实际上记录了所有请求/响应,如下所示:

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

有没有办法可以从客户端跟踪时间并记录它?

最佳答案

该线程是一个旧线程,但分享了我在客户端采取的方法。

  1. 我们与服务器进行了基于 SOAP 的交换,因此我们的类扩展了 AbstractSoapInterceptor

  2. 在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
}

}
  • 将拦截器作为输入和输出拦截器添加到 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>

    关于CXF 日志响应时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20125276/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com