Dynamic property key in Mule 4

I have guided you on how to read and use properties file in Mule 4. A need that maybe sometimes you will need when working with the .properties configuration file for Mule 4 application is to get the property value with dynamic property key. This means that the property key value will depend on the processing of business logic. How is it in details? I will introduce to you in this tutorial!

First, I will create a new Mule project as an example:

Dynamic property key in Mule 4

As an example, I will create a new Mule flow that allows users to pass the name of a student, then our application will get the full name of this student. Of course, student information will be saved in the properties file. Checking the student name and then return the full name, there is nothing to discuss, but if you do so, our Choice will be a lot of When. We need to use the dynamic property key to depend on the student name that the user passed in, we can determine the property key we need to use to get the student name.

My Mule flow will be as follows:

Dynamic property key in Mule 4

With:

HTTP Listener Connector is configured as follows:

Dynamic property key in Mule 4

Global Configuration:

Dynamic property key in Mule 4

Set Variable endpoint will take the name of the student from the user’s request:

Dynamic property key in Mule 4

The Logger endpoint will rely on this name to get the full name of the student and print the results.

The student name will be configured in the application.properties file located in the src/main/resource directory as follows:

In order for Logger endpoint to do this, we will need to use DataWeave 2 in the Logger component with the support of function p(). Function p() is a function that allows us to retrieve the property value of a property key. To get a dynamic value for property key, we’ll pass the Mule Flow Variable in the parameter of function p() and the rest of the property key is as follows:

Dynamic property key in Mule 4

The result when running the above application and request to http://localhost:8081/test?name=khanh and then http://localhost:8081/test?name=hoa will be as follows:

Dynamic property key in Mule 4

One thought on “Dynamic property key in Mule 4

Add Comment