Reading Time: 5 minutes

Mule has a new really cool module ready to be used in Muleforge called Dynamic Flows. The idea behind this new module might sound strange at first but it is actually quite simple: it allows you to add new flows to your existing mule application without restarting the server. It provides three simple operations for flow management:

1) Add

Creates a new mule context with the name and configuration provided. It allows adding to your application all the flows defined in the config XMLs passed to it. For example, the addFlow:

Could not embed GitHub Gist 1660068: Not Found

Receives a context name and a list of mule configs XMLs (the XMLs, not the files) through a http post, then the transformer creates a map grouping all the XMLs under the same key and finally the module processes those XMLs and creates a new context in your mule server. After the flow finishes, we have new flows ready to be used.

2) Run

It executes a dynamic flow, for example, consider the following flow:

Could not embed GitHub Gist 1660142: Not Found

If we write: http://localhost:10443/run?contextName=testContext&flowName=testVmFlow on our web browser, it executes the dynamic flow called “testVmFlow” in the dynamic context “testContext” added by the module.

There are two ways you can call a flow: using a VM transport or without it. If you choose the first option, remember to declare the vm inbound in your flows.

3) Remove

Removes a entire dynamic context from your mule application.

A demo application

This module is quite useful when you need to execute logic that depends on dynamic information, for example, let’s consider the following requirement:

Our users have several campaigns in Salesforce, all of them have a custom field called ‘info’. The information stored in this custom field depends on the campaign. For example, a user A has two campaigns: ‘Phone marketing’ and ‘Email sales’, for the first one,  the field ‘info’  must contain all the user’s available twilio phone numbers  separated by comas, but for the ‘Email sales’ campaign it must contain the email template stored in constant contact. 

We need to create a mule application that sets the ‘info’ field for our users.

As we can see, this problem is kind of tricky, because we don’t know which process we need to execute before hand, it depends on the salesforce campaign so what do we code in mule?, do we write an if condition in our mule context?, what happen if the user creates a third campaign?. Doing this application with the static mule configuration is hard, however we can simplify it a lot by using Dynamic Flows module. We just open MuleStudio, create the following flows:

And we are done, every time our users create a new campaign, they can tell mule how to get the data that has to stored into the ‘info’ field,  without stopping and starting the mule server.

Links and further reading

For further information, see Dynamic Flows documentation.

To see the code check the Dynamic Flows source code, you can find a demo app to see its usage.