Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Heads Up: Transform Component

DZone's Guide to

Heads Up: Transform Component

Dataweave is such a capable component, but when you overuse it, it will lead to performance issues.

· Integration Zone ·
Free Resource

WSO2 is the only open source vendor to be named a leader in The Forrester Wave™: API Management Solutions, Q4 2018 Report. Download the report now or try out our product for free.

DataWeave is such a capable component, but when you overuse it, it will lead to performance issues. I will explain one of the scenarios. 

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:context="http://www.springframework.org/schema/context" xmlns:mulerequester="http://www.mulesoft.org/schema/mule/mulerequester" 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/core http://www.mulesoft.org/schema/mule/core/current/mule.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
http://www.mulesoft.org/schema/mule/mulerequester http://www.mulesoft.org/schema/mule/mulerequester/current/mule-mulerequester.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration"/>
    <context:property-placeholder location="sample.properties"/>
    <flow name="sampleprojFlow1">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/trans2" doc:name="HTTP"/>
        <mulerequester:request resource="file://D:/testdel/trans2.json" doc:name="Mule Requester" returnClass="java.lang.String"/>
        <set-payload value="#[payload]" encoding="UTF-8" mimeType="application/json" doc:name="Set Payload"/>
        <logger message="personID start expression" level="INFO" doc:name="Logger"/>
        <set-variable variableName="personID" value="#[json:/data[0]/personID]" doc:name="personID - Variable"/>
        <logger message="personID fetch end " level="INFO" doc:name="Logger"/>
        <logger message="fetch using transform component start " level="INFO" doc:name="Logger"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-variable variableName="sample"><![CDATA[%dw 1.0
%output application/java
---
payload.data[0].personID]]></dw:set-variable>
        </dw:transform-message>
        <logger message="sfetch using transform component end " level="INFO" doc:name="Logger"/>
        <dw:transform-message doc:name="filter-records-for-non-oc-user" metadata:id="e73fb51c-1b04-4743-ae7b-109de078de3b" mode="deferred">
            <dw:input-payload mimeType="application/json"/>
            <dw:input-variable doc:sample="sample_data\string.dwl" mimeType="application/java" variableName="userCompanyId"/>
                                    <dw:set-payload><![CDATA[%dw 1.0
%var ocuser = p('oc.user')
%output application/json
---
({
data: (payload.data[?($.companyID != ocuser)]) when (1==1) otherwise payload
}) ++

{
"personID" : flowVars.personID,
"personID" : flowVars.sample
}]]></dw:set-payload>
                                </dw:transform-message>
        <logger message="end" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

I have copied a value from my payload using expression component and DataWeave. My log file below shows that my expression component takes less time than DataWeave transform component. 

INFO  2018-12-10 17:09:07,380  org.mule.api.processor.LoggerMessageProcessor: personID start expression
INFO  2018-12-10 17:09:07,656  org.mule.api.processor.LoggerMessageProcessor: personID fetch end 
INFO  2018-12-10 17:09:07,656  org.mule.api.processor.LoggerMessageProcessor: fetch using transform component start 
INFO  2018-12-10 17:09:11,312  org.mule.api.processor.LoggerMessageProcessor: sfetch using transform component end 

When we store the payload into another variable expression, MEL is the best to replace transform component. Until you require a transformation, consider using MEL.

The code below shows how different loading payload into a variable from DataWeave to MEL.

MEL:

<set-variable variableName="personID" value="#[json:data]" doc:name="personID - Variable"/>

DataWeave: 

        <dw:transform-message doc:name="Transform Message">
            <dw:set-variable variableName="sample"><![CDATA[%dw 1.0
%output application/java
---
payload.data]]></dw:set-variable>
        </dw:transform-message>

My log file below as per the latest modification:

INFO  2018-12-10 18:13:25,375  org.mule.api.processor.LoggerMessageProcessor: personID start expression
INFO  2018-12-10 18:13:26,090  org.mule.api.processor.LoggerMessageProcessor: personID fetch end 
INFO  2018-12-10 18:13:26,092  org.mule.api.processor.LoggerMessageProcessor: fetch using transform component start 
INFO  2018-12-10 18:13:29,907  org.mule.api.processor.LoggerMessageProcessor: sfetch using transform component end 

Thanks for reading. If you have questions, feel free to ask in the comments section. 

IAM is now more than a security project. It’s an enabler for an integration agile enterprise. If you’re currently evaluating an identity solution or exploring IAM, join this webinar.

Topics:
dataweave ,mule ,tutorial ,integration ,code snippet

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}