【问题标题】:CXF Log response timeCXF 日志响应时间
【发布时间】:2013-11-21 15:39:14
【问题描述】:

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

我已经将我自己的 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

有没有一种方法可以从客户端跟踪时间并记录下来?

【问题讨论】:

  • 我使用 AOP 和周围的建议来计算时间(主要是我需要用于审计目的
  • 我们放弃了这个想法。我正在实习,但我可能会在几周后再次在那里,也许他们找到了方法,我会更新它。

标签: cxf jax-rs slf4j


【解决方案1】:

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

  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
    }

}
  1. 将拦截器作为 in 和 out 拦截器添加到 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>

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    相关资源
    最近更新 更多