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.
Join the DZone community and get the full member experience.
Join For Freewe 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
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.
Opinions expressed by DZone contributors are their own.
Comments