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

How to Generate Property Key at Run-Time

DZone's Guide to

How to Generate Property Key at Run-Time

In this article, Abhay Singh explains how to generate a property key at run-time and accessing the corresponding value using DataWeave.

· Integration Zone
Free Resource

Today’s data climate is fast-paced and it’s not slowing down. Here’s why your current integration solution is not enough. Brought to you in partnership with Liaison Technologies.

We are very much aware how important of a role property files play in Mule ESB in making integrations and applications dynamic, and even in making it reusable.

Generally, a value in the property file can be accessed by nesting the key within curly brackets, which is preceded by the $ symbol (like this: ${key}.).

With the idea of making integrations and applications as dynamic and fluid as possible, one can generate values from property files at compile-time or run-time.

One way of generating property values at runtime is by using DataWeave. DataWeave supports accessing property values in property files using property keys.

Scenario

In this scenario, we will be creating a file whose properties are in a property file. These properties are the name of the file, the content of the file, and the path where the file will be placed.

To begin with, we have an HTTP listener that accepts interfaceNumber as the input parameter. Based on the interface number, the application will form property keys in the Message Properties transformer, which will be assigned to a unique flow variable. This flow variable, when accessed inside DataWeave, will return the desired property value specific to that particular interface.

The applications endpoint is a File connector, wherein configurations such as File Name/Pattern and Path are generated dynamically depending on the interface.

Mule Flow

Image title

The configuration XML of the flow is given below:

<http:listener-config name="HTTP_Listener_Configuration" host="${http.host}" port="${http.port}" doc:name="HTTP Listener Configuration"/>
<flow name="runtimeProperties_flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="${http.path}" doc:name="HTTP"/>
        <message-properties-transformer scope="invocation" doc:name="Message Properties">
            <add-message-property key="fileName" value="#['interface.' + message.inboundProperties['http.query.params']['interfaceNumber'] + '.fileName']"/>
            <add-message-property key="fileContent" value="#['interface.' + message.inboundProperties['http.query.params']['interfaceNumber'] + '.fileContent']"/>
            <add-message-property key="filePath" value="#['interface.' + message.inboundProperties['http.query.params']['interfaceNumber'] + '.filePath']"/>
        </message-properties-transformer>
        <set-payload doc:name="Set Payload" value="#[dw('p(flowVars.fileContent)')]"/>
        <file:outbound-endpoint responseTimeout="10000" doc:name="File" outputPattern="#[dw('p(flowVars.fileName)')]" path="#[dw('p(flowVars.filePath)')]"/>

</flow>

The structure of the property file is given below:

####################### http properties  #######################
http.host=0.0.0.0
http.port=8081
http.path=/test

################### interface 001 properties ###################
interface.001.fileName=interface001.txt
interface.001.fileContent=This is test data for interface 001
interface.001.filePath=C:\Users/xyz/Desktop/Test/Interface 001

################### interface 002 properties ###################
interface.002.fileName=interface002.txt
interface.002.fileContent=This is test data for interface 002
interface.002.filePath=C:\Users/xyz/Desktop/Test/Interface 002

From the configuration XML, one can figure that we are creating a key structure similar to that in the property file and store it in a flow variable.

These flow variables are then accessed using the DataWeave function inside any component as per the configuration.

Hence, we have managed to dynamically access property values at compile time solely from the input received and successfully generated the desired result.

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

Topics:
dataweave ,integration ,property keys

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}