Monday, May 29, 2017

Mule Application Development - Advanced Topics (1): Global Functions

Introduction

In my previous post, I have talked about using Data-weave functions. Now, you will see that you may use those functions in different places. Sometimes, you may need to use the same function in MEL. For these kind of situations, the global function will resolve the reuse of functions. In this post, I am going to demonstrate how to you define these functions and how to use them.

Define Global Functions

Global functions can be defined any where in mule configurations files. However, it is better to define in a specific file. Here I have a file named: common.xml with the following contents:





     
         
             
             
             
              
                 def getReasonId2(reason) {
                   return StringUtils.splitAndTrim(reason, "-")[1];
                 } 
                 
                 def normalizeDate (aDateString) {
                  date = new DateTime(aDateString, 'MM/dd/yyyy');
             date.format('yyyy-MM-dd');
             return date;
                 }
                 
                 def mergeArray(data) {
                  sValue = StringUtils.join(data,',');
                  return sValue
                 }
              
            
         
     

In the above code, I defined 3 functions, getReasonId2, normalizeDate, and mergeArray. These functions use class of org.mule.util.StringUtils and org.mule.el.datetime.DateTime. These are imported before the global-functions element. As you can see, we are using classes from other packages. you can develop your own java classes and imported here too. We will demonstrate that in the next blog. For now, let demonstrate how to use them. Actually, the usage is the same as we use in data-weave functions. Mule runtime will be able to figure out where it is defined.

Usages




    
        
        
             {
 staff_id: record."Buyer Reference",
 Contract_End_Date: normalizeDate(record."End Date") as :date,
 Last_Day_of_Work: normalizeDate(record."End Date") as :date,
 termination_reason: getReasonId2(record."Closed Reason")
})]]>
        
        
        
    


Sample Input

[
  {
    "Buyer Reference": "207157",
    "Start Date": "05/01/2017",
    "End Date": "05/01/2017",
    "Closed Reason": "Voluntary - Assignment Completed"
  },
   {
    "Buyer Reference": "207157",
    "Start Date": "05/01/2017",
    "End Date": "05/01/2017",
    "Closed Reason": "Voluntary - Assignment Completed"
  }
]

Output From Sample Input

[
  {
    "staff_id": "207157",
    "Contract_End_Date": "2017-05-01",
    "Last_Day_of_Work": "2017-05-01",
    "termination_reason": "Assignment Completed"
  },
  {
    "staff_id": "207157",
    "Contract_End_Date": "2017-05-01",
    "Last_Day_of_Work": "2017-05-01",
    "termination_reason": "Assignment Completed"
  }
]

The above code demonstrates the usage of global functions in data-weave and MEL expression.

Further Improvement

The global-functions can be defined in a file. In this way, we separate the mule configuration files with global functions definition files. The following is the improved configuration file:




     
         
             
             
                 
            
         
     

Put my_global_functions.mven under src/main/resources, or any places within classpath.

4 comments:


  1. A collaboration of IT Consulting Firm and Your IT Department: When you need to innovate, you need to collaborate.
    Why Business Need to Hire IT Consulting Company or Professionals?

    ReplyDelete
  2. Very informative post for mulesoft developers.You can also visit goformule.com for mulesoft stuff.

    ReplyDelete
  3. Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing
    Mule 4 Training
    Best Mulesoft Online Training

    ReplyDelete

Anypoint Studio Error: The project is missing Munit lIbrary to run tests

Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...