Mule 4 Common Shared Flows

Any senior developer is going to want to create a library of commonly used features that will be shared among MuleSoft applications. In Mule, we create a common, shared project that has reusable components and flows. Domain projects don’t allow reusable flows, so this is another great option.

Updated April 12, 2024

This can include many things, the most popular being:

  • Global Error Handling
  • Global Logging
  • Common Business Logic Flows

This activity was completed using the following versions:

  • Mule 4.2.2
  • AnyPoint Studio 7.4.2
  • Mule Mavin Plugin 3.3.5

I will show as much of the setup in AnyPoint studio UI as possible. We’ll only edit the files manually when necessary.

First, create a Mule project that you want to have your shared code in. I called my project “mule-common-flows”. If you commonly use a domain application, DO NOT use it for this project. This project will not run on it’s own, it will only be referenced by other projects that, in turn, can be domain based projects.

I added two Mule configuration files. One that will have Error Handlers, and another that will have a re-usable flow.

Mule Global HTTP Handlers
Global HTTP Error Handlers for flows that use Requests
Mule Global Reusable Flows
Global Flow to create default values for a couple of variables.

Once you’re happy with your re-usable components, we need to edit the POM.XML file. We need to add a “classifier” tag in the “org.mule.tools.mavin” plugin section like so:

        <plugin>
            <groupId>org.mule.tools.maven</groupId>
            <artifactId>mule-maven-plugin</artifactId>
            <version>${mule.maven.plugin.version}</version>
            <extensions>true</extensions>
            <configuration>
                <classifier>mule-plugin</classifier>
            </configuration>
        </plugin>

Then go to a command prompt and navigate to the source folder for this project and mvn clean command like so:

C:\Source\mule-common-flows>mvn clean package install

If you receive the message: “The packaging for this project did not assign a file to the build artifact”, then you may be using the mule maven plugin version 3.8.x or later, you’ll need to remove the package option, like so:

C:\Source\mule-common-flows>mvn clean install

You should receive a “BUILD SUCCESS” message. If you don’t, you’ll have to resolve whatever issues you have before continuing. This will build a JAR file and place it in the /target folder in the project.

Leave this project open in your Package Explorer, although you can close all the XML file tabs.

In the project in which you wish to reference the shared items, we have to manually edit the POM.XML file again. Add a section in the dependencies section like below:

    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>mule-common-flows</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <classifier>mule-plugin</classifier>
    </dependency>

The dependency property values need to match the values in the POM.XML of the mule-common-flows project. The “classifier” property is additional.

Next, open one of the Mule Configuration Files in your project that is going to consume the mule-common-flows.

Click on the “Global Elements” tab.

Click on the “Create” button.

Navigate to Global Configurations – Import and click “OK”

AnyPoint Studio Choose Global Type
Select the Import item and click “OK”

DO NOT click on the ellipses to select a file. The file is already referenced in the JAR file of the mule-common-flows project. So you need to just manually enter the name of the configuration file. I’m going to add two, one for my global error handler and the other for the shared flows. Better to copy and paste the names to be sure. I put the names of the files in the Notes section too, so that they show up in the summary screen.

AnyPoint Studio Global Properties Element Import
Click “OK” when you’re done.
AnyPoint Studio Global Element Properties
Click “OK” when you’re done.

Your Global Configuration Elements should look something like the below. You can see how entering the names of the files in the notes is helpful.

AnyPoint Studio Global Configuration Elements
Our two imports are present.

Save all your changes.

Now to test them out. I’m going to setup my project to reference my new and shiny Global Error Handler. Click on “Create” again. Expand “Global Configuration” and click on “Configuration” and click “OK”.

Your Global Error Handler should now be in the drop down. If it’s not, then make sure you’ve saved everything. You can try closing and re-opening the project as we often need to do.

AnyPoint Studio Global Element AnyPoint Studio Global Properties Element Importt
The Globals_Error_Handler is from mule-common-flows project.

Click “OK” to save those changes.

Now, let’s go to the “Message Flow” tab. Drag a “Flow Reference” to the canvas if one doesn’t already exist.

Click on your “Flow name” drop down and you should now see our flow from the mule-common-flows project like highlighted below:

AnyPoint Studio Flow Reference
Globals_Initialize_Variables_Sub_Flow is from mule-common-flows

There you did it! Now go show off to all your colleagues.

References:

https://help.mulesoft.com/s/article/How-to-add-a-call-to-an-external-flow-in-Mule-4

Author: Jack Yasgar

Jack Yasgar has been developing software for various industries for two decades. Currently, he utilizes C#, JQuery, JavaScript, SQL Server with stored procedures and/or Entity Framework to produce MVC responsive web sites that converse to a service layer utilizing RESTful API in Web API 2.0 or Microsoft WCF web services. The infrastructure can be internal, shared or reside in Azure. Jack has designed dozens of relational databases that use the proper primary keys and foreign keys to allow for data integrity moving forward. While working in a Scrum/Agile environment, he is a firm believer that quality software comes from quality planning. Without getting caught up in analysis paralysis, it is still possible to achieve a level of design that allows an agile team to move forward quickly while keeping re-work to a minimum. Jack believes, “The key to long term software success is adhering to the SOLID design principles. Software written quickly, using wizards and other methods can impress the business sponsor / product owner for a short period of time. Once the honeymoon is over, the product owner will stay enamored when the team can implement changes quickly and fix bugs in minutes, not hours or days.” Jack has become certified by the Object Management Group as OCUP II (OMG Certified UML Professional) in addition to his certification as a Microsoft Certified Professional. The use of the Unified Modeling Language (UML) provides a visual guide to Use Cases and Activities that can guide the product owner in designing software that meets the end user needs. The software development teams then use the same drawings to create their Unit Tests to make sure that the software meets all those needs. The QA testing team can use the UML drawings as a guide to produce test cases. Once the software is in production, the UML drawings become a reference for business users and support staff to know what decisions are happening behind the scenes to guide their support efforts.

2 thoughts on “Mule 4 Common Shared Flows”

    1. Common flows are the same as any other flow that you create in MuleSoft. I’ve only used the standard Request and such in my common flows so that I don’t wind up with lots of dependencies that I don’t need in a particular microservice. If you create a common flow for DB or MQ access, then once you reference it, you should be able to use a flow or sub-flow in your common flows just as you would any other flow in your project.

Leave a Reply

Your email address will not be published. Required fields are marked *