使用拦截器计算Mule流的处理时间

Vih*_*har 1 java mule

我想计算我的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

    }
Run Code Online (Sandbox Code Playgroud)

现在的问题是,每当我执行我的骡子流时,我得到的所有值time.getStatistics().getTotalProcessingTime()总是为'0'.

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

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

任何指针赞赏

谢谢!

Ani*_*ary 7

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

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

<timer-interceptor />
Run Code Online (Sandbox Code Playgroud)

把它放在流程的最后

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

在流程结束时使用此: -

<custom-interceptor class="com.customInterceptor.TimerInterceptor" />
Run Code Online (Sandbox Code Playgroud)

和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;
    }
}
Run Code Online (Sandbox Code Playgroud)