Generating Property Key at Run Time in Mule ESB

    0
    34

    Introduction

    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

    The configuration XML of the flow is given below:

    <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/Abhay/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/Abhay/Desktop/Test/Interface 002

    Conclusion

    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.

    LEAVE A REPLY