View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.interceptor;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.interceptor.Interceptor;
12  import org.mule.processor.AbstractInterceptingMessageProcessor;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  /**
18   * <code>TimerInterceptor</code> simply times and displays the time taken to process
19   * an event.
20   */
21  public class TimerInterceptor extends AbstractInterceptingMessageProcessor implements Interceptor
22  {
23      /**
24       * logger used by this class
25       */
26      private static Log logger = LogFactory.getLog(TimerInterceptor.class);
27  
28      public MuleEvent process(MuleEvent event) throws MuleException
29      {
30          long startTime = System.currentTimeMillis();
31  
32          MuleEvent resultEvent = processNext(event);
33  
34          if (logger.isInfoEnabled())
35          {
36              long executionTime = System.currentTimeMillis() - startTime;
37              logger.info(resultEvent.getFlowConstruct().getName() + " took " + executionTime
38                          + "ms to process event [" + resultEvent.getId() + "]");
39          }
40  
41          return resultEvent;
42      }
43  }