I found it crucial to be able to share some logic, encapsulated within (private) flow or subflow. In Mule ESB we have a couple of ways to handle that scenario. I will focus on custom maven project. You will see how easy it is to prepare reusable mule library.

Common libraries

It is a common practice to create our own common mule library. I highly recommend that, as this may not be only reusable for the current project but for others as well. It only depends from that, how much generic it is, what we are sharing.

Common features project

We may share custom logic encapsulated within flows/subflows. The same applies to functions that were prepared in DataWeave. In this article I have described how to create modules. Such files can be attached to our shared project.

Project creation

First we need to create a new maven project. Then we need to amend slightly the project’s structure.  I use similar structure that is used in Mule project. Therefore I added src/main/mule and src/main/resources folder like in the screenshot below.

Mule Common Project
Mule Common Project

In contract to the previous version of mule, we now could not create a mule project with shared code and attach dependency to it. This is related to the new build model, briefly mentioned below.

New build model

Mule 4 introduced completely new build model. When you build your project you should see a  jar file instead of a zip in the target library. File name patters is as follows:
{artifact-id}-{version}-mule-application.jar
Previously it was:
{artifact-id}-{version}.zip

When you build your project, content from mule and resources folder won’t be transferred to the jar file. We need to instruct maven plugin, that we are interested in content stored in our custom folders. In order to do this, we need to modify pom.xml file.

In the build section you need to add resources element. Each new folder needs to be mentioned by name. Like in the snippet below:

<resources>
	<resource>
		<directory>src/main/mule</directory>
	</resource>
	<resource>
		<directory>src/main/resources</directory>
	</resource>
</resources>

Mule configuration files

Now we are ready to create new mule configuration files with reusable set of features. It is a good practice to create separate configuration files per feature. For example logging related subflows can be stored in logging.xml file. When we have exception handlers for rest/soap services we may store them in error-handling.xml.

As long as it is easy to read and maintain , you may do it differently. It is up to you.

Ready to use

You may as well download maven based project from my GitHub. In order to use that project in Anypoint Studio you need to use Import functionality and choose Existing Maven Projects.

Installation

Project needs to be available through maven. In order to install our project in local maven repository go the project’s root folder and invoke following command:

mvn clean install

Project usage

In order to use our awesome features we need first to load the dependency. Therefore go to pom.file and add dependency like:

<dependency>
	<groupId>pl.profit-online</groupId>
	<artifactId>mule-commons</artifactId>
	<version>1.0.0</version>
</dependency>

To find if dependency has been downloaded go to Project Libraries in Package Explorer. Then navigate to you library name. In my case that is mule-commons-1.0.0.jar. As you can see I have there three mule configuration files. 

To use imported flows in our project we need to import these files. This typically is done in global.xml file on Global Elements view.  Click Create button and then search for Import. You type the name of the file that is present in your jar. 

<import doc:name="Import" doc:id="d36b6742-2c87-4f46-bbbc-c476a105e9c6" file="logging.xml" />

When you save your project. You will be able to refer to flows defined in imported files. 

When we refer to DataWeave modules we do it by import keyword followed by package name and file name. Like in the snippet below. Details how to import and use modules is described here.

%dw 2.0
output application/json skipNullOn="everywhere"
import dw2::json::utils::emptiness_filter
---
emptiness_filter::filterEmptyObjects({})

Summary

It an easy task to extract common features into a separate project and reuse them across different projects. This is possible by using maven project. This require some initial setup but it pays off later on. I highly recommend to do that for your projects.

Sharing flows in external lib
Tagged on:     

Leave a Reply

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