本文介绍了使用拦截器计算Mule流的处理时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to calculate the execution time which my mule flow takes for execution,so I have used interceptor for it,here is my interceptor code

    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'。

now the problem is,whenever I execute my mule flow all the value I get from time.getStatistics().getTotalProcessingTime() is always '0'.

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

Am I using the correct method or I have made some mistake?

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

I basically need a way to find execution time from ProcessingTime object.

任何指针赞赏

谢谢!

推荐答案

您可以通过以下两种方式实现: -

You can do it by following 2 ways :-

1)使用计时器拦截器: -

<timer-interceptor />

把它放在流程的最后

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

2) Use custom interceptor to create your own timer interceptor :-

在结束时使用此功能流程: -

Use this at the end of the flow :-

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

和com.customInterceptor.TimerInterceptor类: -

and com.customInterceptor.TimerInterceptor class :-

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;
    }
}

这篇关于使用拦截器计算Mule流的处理时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 06:29