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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Error Handling Inside Kumologica Subflow
  • Error Handling Wikipedia in MuleSoft
  • MDC Logging With MuleSoft Runtime 4.4
  • MuleSoft Integration With RabbitMQ

Trending

  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • System Coexistence: Bridging Legacy and Modern Architecture
  • How to Convert XLS to XLSX in Java
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Understanding Anypoint Flows and Sub-Flows With MuleSoft

Understanding Anypoint Flows and Sub-Flows With MuleSoft

Sub-flows are helpful whether you want to process messages synchronously or asynchronously. They are useful when it comes to code re-use, breaking down flows, and more.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Feb. 21, 17 · Opinion
Likes (5)
Comment
Save
Tweet
Share
29.7K Views

Join the DZone community and get the full member experience.

Join For Free

A Mule application basically contains one or more flows. Mule flows start processing a message when it is received by inbound endpoints. This flow either contains all the processing stages or routes the message to other flows or sub-flows to perform a specific task. A Mule flow can be either a synchronous or asynchronous flow. The sub-flow is always synchronous.

Advantages of Mule Flow

Asynchronously triggered flow can perform time-consuming tasks, such as writing bulk data to a database or emailing a message without stalling the execution of triggering flow. Triggering flows and asynchronously triggered flows can both be executed in parallel.

You can break up complex flows into various flows or sub-flows that are easy to read.

Processing actions in a flows or sub-flows can be called and used by multiple flows in an application. You can achieve code reusability.

Sub-Flows

Sub-flows process messages synchronously and inherit the processing as well as exception strategy from triggering flows. During sub-flow running, processing on triggering flows stalls until the sub-flow completes the processing and hands over the message to the triggering flow.

  • Sub-flows are best-suited for code reuse, so you can write the block of code once within a sub-flow and it can be triggered by any flow within the same application.

  • You are breaking a flow into various sub-flows that make the GUI view intuitive and XML easy to read.

  • Sub-flows inherit processing as well as exception strategies from the triggering flow, so you don't have to implement processing and exception strategies for sub-flows.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
<flow name="apdev-examplesFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/hello" doc:name="HTTP"/>
<flow-ref name="subflow1" doc:name="subflow1"/>
<flow-ref name="subflow2" doc:name="subflow2"/>
<logger level="INFO" doc:name="Logger" message="Name: #[message.outboundProperties.qpname] Type: #[flowVars.qptype] Color: #[sessionVars.color]"/>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<set-payload value="Error while processing the message" doc:name="Set Payload"/>
</catch-exception-strategy>
</flow>
<sub-flow name="subflow1">
<set-payload value="#['Hello World'.toUpperCase()]" doc:name="Set Payload"/>
<set-property propertyName="qpname" value="#[message.inboundProperties.'http.query.params'.name]" doc:name="Property"/>
</sub-flow>
<sub-flow name="subflow2">
<set-variable variableName="qptype" value="#[message.inboundProperties.'http.query.params'.type]" doc:name="Variable"/>
<set-session-variable variableName="color" value="gray" doc:name="Session Variable"/>
</sub-flow>
</mule>

Synchronous Flow

Synchronous flows are similar to sub-flows. During triggered synchronous flow running, processing on triggering flow stalls until the triggered flow completes the processing and hands over the message to the triggering flow.

Synchronous flows don't inherit the processing and exception strategy from the triggering flow. This type of flow processes the message within a single thread.

Flow references can be used to invoke other flows:

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
<flow name="flowexampleFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/GET" doc:name="HTTP"/>
<logger level="INFO" doc:name="Logger"/>
<flow-ref name="flowexampleFlow1" doc:name="FlowReference"/>
</flow>
<flow name="flowexampleFlow1">
<set-variable variableName="code" value="#[message.inboundProperties.'http.query.params'.code]" doc:name="Variable"/>
<file:outbound-endpoint path="src/test/resources" responseTimeout="10000" doc:name="File"/>
</flow>
</mule>

Asynchronous Flow

Asynchronous flows process the message in parallel to the triggered and triggering flow. The flow passes the message to the asynchronous flow, thus triggering this flow. At the same time, the triggering flow sends a copy of the message to another processor of its own flow.

So, triggering and triggered flows execute simultaneously and independently of each other. Asynchronous flows don't inherit the processing and exception strategy from the triggering flow. This type of flow processes the message within multiple threads.

For asynchronous flows, the flow reference must be wrapped with async scope in the triggering flow.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
<flow name="flowexampleFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/GET" doc:name="HTTP"/>
<logger level="INFO" doc:name="Logger"/>
<async doc:name="Async">
<flow-ref name="flowexampleFlow1" doc:name="FlowReference"/>
</async>
</flow>
<flow name="flowexampleFlow1">
<set-payload doc:name="Set Payload"/>
<file:outbound-endpoint path="src/test/resources" responseTimeout="10000" doc:name="File"/>
</flow>
</mule>

I hope that this article clarifies any confusion that you may have had with Mule flows and sub-flows.

Flow (web browser) Processing MuleSoft

Published at DZone with permission of Jitendra Bafna, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Error Handling Inside Kumologica Subflow
  • Error Handling Wikipedia in MuleSoft
  • MDC Logging With MuleSoft Runtime 4.4
  • MuleSoft Integration With RabbitMQ

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!