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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why 200 OK Is the Most Dangerous Lie in Enterprise Integration
  • Jakarta Faces Flow Scope: Managing Multi-Step UX Without Session State
  • 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

Trending

  • Git Branching Strategies for Maintainable Test Automation
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • Integrating Apache Spark With Drools: A Loan Approval Demo
  • The Permission Was Valid. The Action Was Not Authorized.

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Oct. 13, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Why 200 OK Is the Most Dangerous Lie in Enterprise Integration
  • Jakarta Faces Flow Scope: Managing Multi-Step UX Without Session State
  • 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

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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