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 CustomLogg
You can do it by following 2 ways :-
1) By using a timer interceptor :-
<timer-interceptor />
Put this at the end of your flow
2) Use custom interceptor to create your own timer interceptor :-
Use this at the end of the flow :-
<custom-interceptor class="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;
}
}
I can't advise to use an AbstractEnvelopeInterceptor for two reasins, it's not part of the public API and they don't do exactly what you want, as per:
EnvelopeInterceptor is an intercepter that will fire before and after an event is received.
Have you considered using server notifications?
I made the same test because I wanted to calculate the processing time of a flow.
Here it's the example:
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;
}
}
And this is the flow:
<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>
Notice that I calculate the processing time by using the "startTime" parameter of the "last" method, and I place the "custom-interceptor" element at the beginning of the flow.
This is the printed log:
2015-12-10 18:21:27,493 INFO [com.testing.interceptor.TimerInterceptor] - Flow:testingFlowClient-Procesing Time:341