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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <!--xml version= "1.0" encoding= "UTF-8" ?--> <mule xmlns= "http://www.mulesoft.org/schema/mule/core" xmlns:doc= "http://www.mulesoft.org/schema/mule/documentation" xmlns:spring= "http://www.springframework.org/schema/beans" version= "EE-3.8.3" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd" > <configuration doc:name= "Configuration" > <expression-language autoresolvevariables= "true" > <import class= "org.mule.util.StringUtils" > <import class= "org.mule.el.datetime.DateTime" > <global-functions> 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 } </global-functions> </import></import></expression-language> </configuration> </mule> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <!--xml version= "1.0" encoding= "UTF-8" ?--> <mule xmlns:dw= "http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http= "http://www.mulesoft.org/schema/mule/http" xmlns= "http://www.mulesoft.org/schema/mule/core" xmlns:doc= "http://www.mulesoft.org/schema/mule/documentation" xmlns:spring= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd" > <flow name= "global_functionsFlow" > <http:listener config-ref= "HTTP_Listener_Configuration" path= "/advanced/global-function" doc:name= "HTTP" > <dw:transform-message doc:name= "Transform Message" > <dw:set-payload><!--[CDATA[%dw 1.0 %output application/json --- payload map ((record) ---> { 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" ) })]]></dw:set-payload> </dw:transform-message> <set-variable variablename= "Test-Reason" value= "#[getReasonId2('First - Second')]" doc:name= "Variable" > <logger message= "#[payload]" level= "INFO" doc:name= "Logger" > </logger></set-variable></http:listener></flow> </mule> |
Sample Input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [ { "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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [ { "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:1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!--xml version= "1.0" encoding= "UTF-8" ?--> <mule xmlns= "http://www.mulesoft.org/schema/mule/core" xmlns:doc= "http://www.mulesoft.org/schema/mule/documentation" xmlns:spring= "http://www.springframework.org/schema/beans" version= "EE-3.8.3" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd" > <configuration doc:name= "Configuration" > <expression-language autoresolvevariables= "true" > <import class= "org.mule.util.StringUtils" > <import class= "org.mule.el.datetime.DateTime" > <global-functions file= "my_global_functions.mvel" > </global-functions> </import></import></expression-language> </configuration> </mule> |
Put my_global_functions.mven under src/main/resources, or any places within classpath.