DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Using Mule Notifications to Calculate Total Execution Time of a Mule Component

Check out this tutorial, which explains how to use Mule notifications to calculate the total execution time of a Mule component.

Enrico Rafols Dela Cruz user avatar by
Enrico Rafols Dela Cruz
·
Sep. 11, 18 · Tutorial
Like (2)
Save
Tweet
Share
8.33K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I will show you how to use the Mule Notifications functionality of Mule 3.x to calculate the total execution time of a Sub-Flow, HTTP Request Connector, Script Component, and the Transform Message (Dataweave). For more details regarding this functionality, you can check the documentation https://docs.mulesoft.com/mule-user-guide/v/3.9/mule-server-notifications.

Mule Configuration: In this configuration, I'm creating a bean for my custom listener class and referencing it to my Notification Configuration. Also note that the event MESSAGE-PROCESSOR allows the notification to be sent before and after a Mule component is invoked.

    <spring:beans>
      <spring:bean name="muleComponentProcessListener" 
       class="com.custom.mule.notification.listener.MuleComponentProcessListener"/>         
    </spring:beans>
    <notifications>
      <notification event="MESSAGE-PROCESSOR"/>
      <notification-listener ref="muleComponentProcessListener"/>
    </notifications>

Listener Class: This custom class will serve as the listener class of the Mule notification. It simply calculates the total execution time of the above Mule components.

package com.custom.mule.notification.listener;

import java.util.HashMap;

import org.apache.log4j.Logger;
import org.mule.api.context.notification.MessageProcessorNotificationListener;
import org.mule.api.processor.MessageProcessor;
import org.mule.context.notification.MessageProcessorNotification;
import org.mule.module.http.internal.request.DefaultHttpRequester;
import org.mule.processor.chain.SubflowInterceptingChainLifecycleWrapper;

public class MuleComponentProcessListener implements MessageProcessorNotificationListener<MessageProcessorNotification> {
private Logger log = Logger.getLogger(getClass().getName());
private long startTime =0;
private HashMap<String,Long> componentStartTimeHolder = new HashMap<String,Long>();

@Override
public void onNotification(MessageProcessorNotification notification) {
    MessageProcessor process = notification.getProcessor();
    String className = process.getClass().getSimpleName();
    boolean isOutbound = process instanceof DefaultHttpRequester;

    if(className.equalsIgnoreCase("DefaultHttpRequester") && isOutbound){        
    try{    
    DefaultHttpRequester def = (DefaultHttpRequester)process;
    if (notification.getAction() ==  MessageProcessorNotification.MESSAGE_PROCESSOR_PRE_INVOKE){
     startTime = System.currentTimeMillis();
}
 if(notification.getAction() ==  MessageProcessorNotification.MESSAGE_PROCESSOR_POST_INVOKE){ 
     long executionTime = System.currentTimeMillis() - startTime;
     log.warn("HTTP Request Call To : "+ def.getHost()+ " "+ def.getPath()+ " took " + executionTime + "ms response time.");
}
    }catch(Exception e){}
    } else if (className.equalsIgnoreCase("SubflowInterceptingChainLifecycleWrapper")){
    SubflowInterceptingChainLifecycleWrapper subFlow = (SubflowInterceptingChainLifecycleWrapper) process;
    if (notification.getAction() ==  MessageProcessorNotification.MESSAGE_PROCESSOR_PRE_INVOKE){
     startTime = System.currentTimeMillis();
     componentStartTimeHolder.put(subFlow.getSubFlowName(), startTime);
}
if(notification.getAction() ==  MessageProcessorNotification.MESSAGE_PROCESSOR_POST_INVOKE){ 
     long executionTime = System.currentTimeMillis() - (long)componentStartTimeHolder.get(subFlow.getSubFlowName());
     log.warn("Sub-flow Processing Time: "+ subFlow.getSubFlowName() + " took " + executionTime + "ms response time.");
}
    }  else if (className.equalsIgnoreCase("WeaveMessageProcessor")){
    if (notification.getAction() ==  MessageProcessorNotification.MESSAGE_PROCESSOR_PRE_INVOKE){
     startTime = System.currentTimeMillis();
     componentStartTimeHolder.put(process.toString(), startTime);
}
if(notification.getAction() ==  MessageProcessorNotification.MESSAGE_PROCESSOR_POST_INVOKE){ 
     long executionTime = System.currentTimeMillis() - (long)componentStartTimeHolder.get(process.toString());
     log.warn("Dataweave Processing Time: "+ process.toString() + " took " + executionTime + "ms response time.");
}
    } else if (className.equalsIgnoreCase("ScriptTransformer")){
    if (notification.getAction() ==  MessageProcessorNotification.MESSAGE_PROCESSOR_PRE_INVOKE){
     startTime = System.currentTimeMillis();
     componentStartTimeHolder.put(process.toString(), startTime);
}
if(notification.getAction() ==  MessageProcessorNotification.MESSAGE_PROCESSOR_POST_INVOKE){ 
     long executionTime = System.currentTimeMillis() - (long)componentStartTimeHolder.get(process.toString());
     log.warn("ScriptTransformer Processing Time: "+ process.toString() + " took " + executionTime + "ms response time.");
}
    }

}

}

TESTING:

Image title

Thanks for reading, and let me know your thoughts or any questions you might have in the comments section. 

Execution (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • How To Handle Secrets in Docker
  • HTTP vs Messaging for Microservices Communications
  • Fargate vs. Lambda: The Battle of the Future

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: