DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Automating Power Automate: How to Ensure Cloud Flows Are Active After Every Pipeline Deployment
  • How Power Automate Helps Analysts Send Alert Emails Faster and How AI Builder Takes It to the Next Level
  • 2 Hidden Bottlenecks in Large-Scale Azure Migrations
  • Streaming vs In-Memory DataWeave: Designing for 1M+ Records Without Crashing

Trending

  • Native SQL in Java Without JDBC Boilerplate — Meet Ujorm3
  • From Open SQL to CDS Views: Rewriting SAP Data Access for Performance at Scale
  • Spring AI Advisors: Chat Memory, Token Tracking, and Message Logging
  • AI Agents in Java: Architecting Intelligent Health Data Systems
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. How to Configure Common Flows in Mule 4 to Reuse Common Functionalities

How to Configure Common Flows in Mule 4 to Reuse Common Functionalities

This tutorial explains how to reuse common functionalities in your implementation flow in Mule 4.

By 
Harsha Ajjampura Shivamurthy user avatar
Harsha Ajjampura Shivamurthy
·
Oct. 13, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
8.7K Views

Join the DZone community and get the full member experience.

Join For Free

Reusing common functionalities is very important in any coding language to avoid redundancy. In MuleSoft, we will also come across situations where we will have to reuse common functionalities, for example, error handling. In this article, I will explain how to reuse common flows in your implementation flow. 

There are two ways of doing it:

  1. Create Maven dependencies in the Maven repository and reuse them in the main flow.
  2. Create the JAR file and use it in the main flow.

Create Maven Dependencies in the Maven Repository and Reuse Them in the Main Flow

  1. Create the common Mule Project in Studio that you are planning to reuse. In Studio, under Package Explorer, right click -> click New - > select "Mule Project." Enter the Project Name (in my case, common-lib-prj) and click Finish. 
  2. Create two flows in it with a logger inside the flow as below. 
  3. Open the pom.xml file and modify the Mule Maven Plugin by adding the classifier as "mule-plugin" as below.  
  4. Open the command prompt and go to the workspace project directory of common-lib-prj. Enter the command "mvn clean install." By doing this, common-lib-prj will be created as a plugin project, and it will be available to use as a Maven dependency once the build is a success.

  5. Create the new Mule project (in my example, I am creating sample-main-prj) in which you will be using this common-lib-prj. 
  6. Create a sample flow in the Mule configuration file as below. Drag the HTTP listener from the palette, add the default connector configuration with port 8081 and path as /api/*, and then add one Logger and Flow Reference Component.
  7. Copy groupId, artifactId, and version from pom.xml file from common-lib-prj as below. 
  8. Add the above-copied properties and also the classifier into the pom.xml file in sample-main-prj as a dependency. Once you save the pom.xml file, you will see common-lib-prj getting added in to sample-main-prj in the Package Explorer. 
  9. Go to sample-main-prj Mule configuration file and click on the tab Global elements to add Import as Global configuration. 
  10. Add the common-lib-prj.xml Mule configuration file name as an Import Global Configuration as below.
  11.  Now you should be able to see both flows which we built under common-lib-prj in the Flow Reference drop-down menu in sample-main-prj as below. 
  12. Now deploy the flow sample-main-prj by running the project. 
  13. Open Postman or Advanced REST Client and send the request as below. 
  14. Check the Studio Console output and you should see the logger getting printed from common-lib-prj as below. 

Create the JAR File and Use It in the Main Flow

  1. Create the common Mule Project in Studio that you are planning to reuse. In Studio, under Package Explorer, right click -> click new - > select "Mule Project." Enter the Project Name (in my case, common-lib-prj) and click Finish. 
  2. Create two flows in it with a logger inside the flow as below. 
  3. Deploy the common-lib-prj by running the project.
  4. Once the flow is successfully deployed, just export as a Mule Project.
  5. Create a new Project called implementation-prj where you will be using common-lib-prj flows.
  6. Right-click on implementation-prj and add common-lib-prj.jar into the implementation project as a Maven dependency. 
  7. In the next pop-up window, click on "Install a local dependency," browse the common-lib-prj.jar file and install it. Then click Finish. The JAR file will be successfully added to the local repository. 
  8. You should be able to see the dependency of common-lib-prj in the pom.xml file of implementation-prj as below. And also you should be able to see common-lib-prj added under "Project Libraries." 
  9. Create the flow in implementation-prj as below with the default HTTP listener configuration, logger, and Flow Reference. 
  10. Go to Global Elements and import the common-lib-prj Mule Configuration file. With this, we can reference any common flows and configurations which we can utilize or access as shared resources. We can import multiple files.
  11. Now you should be able to see both the flows which we built under common-lib-prj in the Flow Reference drop-down menu in implementation-prj as below. 
  12. Now deploy the flow implementation-prj by running the project.
  13. Open Postman or Advanced REST Client and send the request as below. 
  14. Check the Studio Console output and you should see the logger getting printed from common-lib-prj as below. 

You can deploy these two flows in CloudHub and see the result. 

MULE Flow (web browser)

Opinions expressed by DZone contributors are their own.

Related

  • Automating Power Automate: How to Ensure Cloud Flows Are Active After Every Pipeline Deployment
  • How Power Automate Helps Analysts Send Alert Emails Faster and How AI Builder Takes It to the Next Level
  • 2 Hidden Bottlenecks in Large-Scale Azure Migrations
  • Streaming vs In-Memory DataWeave: Designing for 1M+ Records Without Crashing

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook