Reading Time: 10 minutes

This post was written by one of the stars in our developer community, Rakesh Kumar Jha. 

As a MuleSoft Certified Architect, Designer and Developer, I recently worked on API implementations for one of our clients using MuleSoft’s CloudHub. One common feature that we used across our APIs implementations is reading a properties file in Mule flows.

latest report
Learn why we are the Leaders in API management and iPaaS

There is a lot of documentation that shows users different ways to read a properties file in Mule flows. Here are three approaches on how you can do this:

  1. Reading a properties file using ${Key} expression
  2. Reading a properties file using ![p[‘Key’]] expression
  3. Reading a properties file using p() function from DataWeave

In this blog, I will demonstrate the first two approaches, and then conclude with a discussion of the third approach, which works best in Mule runtime 3.8.5 or later.

Approach #1: Reading a Properties File Using a ${Key} Expression

The most optimal way to read a Mule properties file from a flow is using the ${Key} expression.

This approach will work well for reading data from a Mule app properties file. Here, we first have to store the data in the Mule app properties file, as shown below, and then we have to use the “Key” to read the value.

policies.type =AUTO
policies.code=COMAUTO
<set-variable variableName="PoliciesType" value="${policies.type}" doc:name="Variable"/>

This approach will also work for reading a custom properties file, defended as context property placeholder from a Mule flow. Here, we first keep the properties file in the project folder under src/main/resource, and then define the property placeholder using the context property placeholder, as shown below.

Then we will store the data and read the properties file value using “Key,” similar to the way that we did it above.

<context:property-placeholder location="config.properties" />

The ${key} expression can be used to read Mule properties files (Mule app properties and custom properties defined using context) from activities such as Variable, Groovy, Java etc. The major advantage of this approach is that it work well in terms of performance.

It also works well for reading the latest properties file, especially when the data within the properties file changes frequently. However, this approach cannot be used to read a properties file directly from DataWeave. To access a property value from DataWeave using ${key}, we have to first store the value in some flow variable, as described above, and then use this flow variable in DataWeave for transformation.

Approach #2: Reading a Properties File Using a  ![p[‘Key’]] Expression

Now, let’s move on to the second approach. This approach works for reading a value from a Mule properties file (with Mule app properties and custom properties placed under src/main/resources) that is defined using an expression property placeholder, as described below.

<expression-language:property-placeholder location="mule-app.properties" />
<expression-language:property-placeholder location="config.properties" />

We first have to define the property placeholder with the above expression, and then store the data in the properties file, as shown below. Then, we will use the ![p[‘Key’]] expression to read the value.

api.host=abc.com
api.port=80
<http:request-config name="HTTP_Request_Configuration" host="![p['api.host']]" port="![p['api.port']]" doc:name="HTTP Request Configuration"/>  
<logger message="value:- ![p['api.host']]" level="INFO" doc:name="Logger"/>

The ![p[‘Key’]] expression can be used to read the Mule properties file from activities, such as HTTP listeners/requesters, WS consumer, logger, etc. The major advantage of this approach is that it works great in terms of performance and also allows users to read the latest properties file. Similar to the above approach, this approach does not work for reading a properties file directly from DataWeave.

Ultimately, the above two approaches are the best ways used to read a properties file’s data in Mule flows. The only limitation is that it prevents users from directly reading a properties file value from a Mule DataWeave transformation.

Now, let’s move on to the third approach. This approach is for reading a Mule properties file’s values from DataWeave, using the DataWeave lookup function p().

Approach #3: Reading a Properties File Using p() Function from DataWeave

This approach is for reading Mule properties files (with Mule app properties and custom properties placed under src/main/resources) directly from DataWeave using p().

Here, we first have to store the data in the Mule app properties file, as shown below, then need to use the DataWeave p() function to read the value directly from DataWeave.

policies.type =AUTO
policies.code=COMAUTO

%dw 1.0
%output application/java
---
{
data: p('policies.type')
}

This approach will also work for reading a custom properties file, defended as a context property placeholder from a Mule flow. Here, we keep the properties file in a project folder under src/main/resource, then define the property placeholder using the context property placeholder, as shown below. Then, we will store the data and use the DataWeave p() function to read the value directly from DataWeave as above.

<context:property-placeholder location="config.properties" />

This approach works well for reading a Mule properties value directly from DataWeave. However, the major problem with this approach is that has a cache issue. When we change the properties file value on Mule runtime and re-deploy the Mule API, this approach fails to read the latest properties file in Mule 3.8.4 and lower due to a caching bug. This bug has since been fixed in Mule Runtime 3.8.5, so we suggest trying this approach in version 3.8.5 or later!.

And that’s it! This how-to describes three approaches users can use to read a Mule properties file within a flow. There are numerous ways to go about this, which approach do you use? Try out the examples above and comment below with other approaches!