【问题标题】:Calculating Processing time for a Mule flow by using Interceptors使用拦截器计算 Mule 流的处理时间
【发布时间】:2015-03-07 14:48:23
【问题描述】:

我想计算我的 mule 流执行所需的执行时间,所以我使用了拦截器, 这是我的拦截器代码

    class CustomLoggerInterceptor extends AbstractEnvelopeInterceptor {
       @Override
      public MuleEvent last(MuleEvent event, ProcessingTime time, long startTime,
        boolean exceptionWasThrown) throws MuleException {
             long totalTime=time.getStatistics().getTotalProcessingTime();
             LOG.info("Start time for flow: "+event.getFlowConstruct().getName()+" is: "+startTime+" total execution time is: "+totalTime);
             return event;
       }
       //other inherited methods

    }

现在的问题是,每当我执行我的 mule 流程时,我从 time.getStatistics().getTotalProcessingTime() 获得的所有值始终是“0”。

是我使用了正确的方法还是我犯了一些错误?

我基本上需要一种从ProcessingTime 对象中查找执行时间的方法。

欢迎指点

谢谢!

【问题讨论】:

    标签: java mule


    【解决方案1】:

    我不建议将 AbstractEnvelopeInterceptor 用于两个原因,它不是公共 API 的一部分,而且它们不会完全按照您的要求做:

    EnvelopeInterceptor is an intercepter that will fire before and after an event is received.
    

    您是否考虑过使用server notifications

    【讨论】:

    • 嘿,维克多,出于好奇,我只是想知道“它不是公共 API 的一部分”是什么意思,我从哪里可以获得这些类/接口是其中一部分的信息公共 API,您可以在进行自定义时使用
    【解决方案2】:

    您可以通过以下两种方式来做到这一点:-

    1) 通过使用定时器拦截器:-

    <timer-interceptor />
    

    将其放在流程的末尾

    2) 使用自定义拦截器创建自己的定时器拦截器:-

    在流程结束时使用它:-

    <custom-interceptor class="com.customInterceptor.TimerInterceptor" />
    

    和 com.customInterceptor.TimerInterceptor 类:-

    import org.mule.api.MuleEvent;
    import org.mule.api.MuleException;
    import org.mule.api.interceptor.Interceptor;
    import org.mule.processor.AbstractInterceptingMessageProcessor;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    /**
     * <code>TimerInterceptor</code> simply times and displays the time taken to
     * process an event.
     */
    public class TimerInterceptor extends AbstractInterceptingMessageProcessor
            implements Interceptor {
        /**
         * logger used by this class
         */
        private static Log logger = LogFactory.getLog(TimerInterceptor.class);
    
        public MuleEvent process(MuleEvent event) throws MuleException {
            long startTime = System.currentTimeMillis();
    
            MuleEvent resultEvent = processNext(event);
    
            if (logger.isInfoEnabled()) {
                long executionTime = System.currentTimeMillis() - startTime;
                logger.info("Custom Timer : "+resultEvent.getFlowConstruct().getName() + " took "
                        + executionTime + "ms to process event ["
                        + resultEvent.getId() + "]");
            }
    
            return resultEvent;
        }
    }
    

    【讨论】:

    • 听起来不错.. 如果我使用 timer-interceptor 只需一个查询,然后它会自动将结果记录到特定流程的 Mule-console 中吗?因为我希望花费时间及其流程名称,因为我的应用程序有多个流,我希望为每个流记录时间
    • 在这种情况下,您可以按照以下方式进行操作:- 如果您只在 Mule 流程中使用 timer-interceptor 标签,您可以在 timer-interceptor 之前放置一个记录器并使用如下表达式:- #[flow.name]在控制台中获取流名称
    • 如果您觉得我的回答对您有帮助,请接受我的回答
    • 我尝试使用 timer-interceptor 执行,但每次它返回 0ms,但在其他方法的情况下,它给了我一些价值,因为它使用了系统时间。我在计时器中错过了什么-拦截器?
    • @AnirbanSenChowdhary 我认为计时器拦截应该在流程开始时使用,以防您想测量流程中消息所花费的处理时间,如果您使用将它放在最后它总是会显示接近零的东西。
    【解决方案3】:

    我做了同样的测试,因为我想计算一个流的处理时间。

    这是一个例子:

    package com.testing.interceptor;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.mule.api.MuleEvent;
    import org.mule.api.MuleException;
    import org.mule.interceptor.AbstractEnvelopeInterceptor;
    import org.mule.management.stats.ProcessingTime;
    
    public class CustomInterceptor extends AbstractEnvelopeInterceptor{
    
        private static Log logger = LogFactory.getLog(CustomInterceptor.class);
    
    
        @Override
        public MuleEvent last(MuleEvent event, ProcessingTime time, long startTime,
          boolean exceptionWasThrown) throws MuleException {
            long endTime = System.currentTimeMillis();
    
            logger.info("Flow:"+event.getFlowConstruct().getName()+"-Procesing Time:"+(endTime-startTime));
            return event;
         }
    
        @Override
        public MuleEvent before(MuleEvent event) throws MuleException {
    
            return event;
        }
    
        @Override
        public MuleEvent after(MuleEvent event) throws MuleException {
    
    
            return event;
        }
    }
    

    这就是流程:

    <flow name="testingFlowClient">
            <http:listener config-ref="HTTP_Listener_Configuration" path="/client/*" doc:name="HTTP"/>
            <custom-interceptor class="com.testing.interceptor.CustomInterceptor" />
            <logger message="Before Transformation" level="INFO" />
            <json:json-to-object-transformer returnClass="com.testing.domain.GeneralRequest" />
            <set-variable variableName="processTypeJob" value="#[payload.processTypeJob]"/>
            <set-variable variableName="waitTime" value="#[payload.waitTime]"/>
            <logger message="After Transformation" level="INFO" />
            <json:object-to-json-transformer/>
            <http:request config-ref="HTTP_Request_Configuration" path="/service/{waitTime}" method="POST" 
                            responseTimeout="50000000">
                <http:request-builder>
                    <http:query-param paramName="api_key"
                        value="abcde" />
                    <http:query-param paramName="processTypeJob" value="#[processTypeJob]" />
                    <http:query-param paramName="fields" value="averages" />
                    <http:uri-param paramName="waitTime" value="#[waitTime]" />
                </http:request-builder>
            </http:request>
        </flow>
    

    请注意,我使用“last”方法的“startTime”参数计算处理时间,并将“custom-interceptor”元素放在流程的开头。

    这是打印出来的日志:

    2015-12-10 18:21:27,493 INFO [com.testing.interceptor.TimerInterceptor] - Flow:testingFlowClient-Procesing Time:341 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-09
      • 2018-07-21
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多