---
schema_version: "1"
id: 27c356e5f5df01ebae1d07b680f9c9118c7ad36735379ace08b6998e639b422e
title: Flow and Subflow Scopes
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/mule-runtime/4.4/flow-component.md
canonical_url: https://docs.mulesoft.com/mule-runtime/4.4/flow-component
source_site: docs.mulesoft.com
content_type: documentation
content_hash: f1dce264e19459250422bfd8a6203bbadcee1fb76aab02dc60613edac6c55056
description: MuleSoft Documentation Site
release: "4.4"
latest: false
---
---
product: Mule Runtime
version: 4.4
keywords: component, Mule 4
---

# Flow and Subflow Scopes

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Flow and Subflow scopes are components for grouping together a sequence of other Core components and
operations (provided by connectors and modules) to help automate integration processes. The Flow
component is fundamental to a Mule app. Because all Mule apps must contain at least one flow,
Anypoint Studio automatically provides the first Flow component in your Mule app.

A Mule app can contain additional flows and subflows, as this example shows:

![Example of Flow with Subflows](/mulesoft/current/en-us/mule-runtime/4.4/_images/component-flow-subflow.png)

Figure 1. Example: Flow with Subflows

For a more in-depth discussion of uses, capabilities, and limits of flows and subflows, see
[Flows and Subflows](https://docs.mulesoft.com/mule-runtime/4.4/about-flows).

Note that flows always function synchronously. If you need to achieve asynchronous patterns, such as
Fire-and-Forget, you can use the [Async Scope](https://docs.mulesoft.com/mule-runtime/4.4/async-scope-reference) (`<async/>`).

## Flow Configuration

Flows are configurable. For example, you can set them to start or remained stopped when the Mule app
starts, place limits on the allowed concurrency, or set up business events. You can also set up
error handling for a component (see [Error Handling](#error_handling)).

<table><colgroup><col> <col></colgroup><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><p>Name (<code>name</code>)</p></td><td><p>Name for the flow. Flows automatically receive an editable name that matches (or partially matches) the project name.</p></td></tr><tr><td><p>Initial State (<code>initialState</code>)</p></td><td><div><div><p>Values: Started, Stopped. These are Mule runtime settings for the flow. The default, Started (<code>initialState="started"</code>, also called Empty in Studio), indicates that the flow is active when you start the app. So it can be triggered by an internal or external Mule event source within the flow (such as an HTTP listener or Scheduler), by a Flow Reference from another flow or subflow, or through a call to the <a href="https://docs.mulesoft.com/dataweave/latest/dataweave-runtime-functions#functions_runtime">lookup</a> function.</p></div><div><p>If you set the initial state to Stopped (<code>initialState="stopped"</code>), activate the flow after deployment. For hybrid standalone and Anypoint Platform PCE deployments, use <a href="https://docs.mulesoft.com/runtime-manager/flow-management">Runtime Manager</a> to activate the flow. For CloudHub and Anypoint Runtime Fabric deployments, flow management through Runtime Manager isn’t supported. To activate the flow, redeploy the application or update the flow configuration to Started. When a flow is deployed with the initial state set to Stopped, the console output looks similar to this: <code>Flow flow_component_ex has not been started (initial state = 'stopped')</code>.</p></div><div><div data-type="WARNING"><div><p>For CloudHub deployments, avoid setting <code>initialState="stopped"</code> on flows that contain a Scheduler. The scheduler gets stuck in a permanent "Running" state, and you can’t manage it through Runtime Manager.</p></div></div></div></div></td></tr><tr><td><p>Max Concurrency (<code>maxConcurrency</code>)</p></td><td><div><div><p>Optional. Sets the maximum number of concurrent messages that a flow can process. If not set, the container thread pool determines the maximum number of threads the flow can use to optimize the performance when processing messages. While the flow is processing the maximum number of concurrent messages, it cannot receive additional requests.</p></div><div><p>Set <code>maxConcurrency</code> to <code>1</code> to cause the flow to process requests one at a time.</p></div><div><p>See <a href="https://docs.mulesoft.com/mule-runtime/4.4/execution-engine#backpressure">Back-pressure</a> for details about Mule’s behavior after the the maximum concurrency value is reached.</p></div></div></td></tr><tr><td><p>Business Events</p></td><td><div><div><p>Optional: Defaults to <code>false</code>. For Mule apps that you deploy to CloudHub, you can enable business events (XML example: <code>tracking:enable-default-event="true"</code>) and add a Transaction ID (XML example: <code>&lt;tracking:transaction id=12345</code>/&gt;). See <a href="https://docs.mulesoft.com/mule-runtime/4.4/business-events">Business Events</a>.</p></div></div></td></tr><tr><td><p>Metadata</p></td><td><p>As with many other components, you can set metadata for this component. For more on this topic, see the Studio document <a href="https://docs.mulesoft.com/studio/metadata-editor-concept">Metadata Editor</a>.</p></td></tr></tbody></table>

## Subflow Configuration

Subflow scopes provide a way to edit the name of the subflow and to add metadata. Unlike flows,
subflows lack a built-in mechanism for setting up error handling, though you can use error handling
provided by the [Try Scope](https://docs.mulesoft.com/mule-runtime/4.4/try-scope-concept) within a subflow if you need to perform error
handling.

<table><colgroup><col> <col></colgroup><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><p>Name (<code>name</code>)</p></td><td><p>Name for the subflow. Subflows automatically receive an editable name that matches (or partially matches) the project name.</p></td></tr><tr><td><p>Metadata</p></td><td><p>As with many other components, you can set up metadata for this component. For more on this topic, see the Studio document <a href="https://docs.mulesoft.com/studio/metadata-editor-concept">Metadata Editor</a>.</p></td></tr></tbody></table>

## XML for Flows and Subflows

This example shows the XML for a simple flow (`<flow/>`) that uses a Scheduler as a source to
trigger execution of the flow every 10 seconds, and it connects to a subflow (`<sub-flow/>`) through
a Flow Ref (`<flow-ref/>`) component. That subflow then connects to another subflow, also using a
Flow Ref component. Note that when you configure components through the Studio UI, Studio
automatically adds the XML to the Configuration XML for your Mule app.

Example: XML for a Flow and Subflows

```xml
<flow name="flow_component_ex">
  <scheduler doc:name="Scheduler" >
    <scheduling-strategy >
      <fixed-frequency frequency="10" timeUnit="SECONDS"/>
    </scheduling-strategy>
  </scheduler>
  <ee:transform doc:name="Transform Message">
    <ee:message >
      <ee:set-payload ><![CDATA[
 %dw 2.0
 var myXml =  read('<prices>
    <basic>9.99</basic>
    <premium>53</premium>
    <vip>398.99</vip>
  </prices>', 'application/xml')
 output application/xml
 ---
 myXml
]]></ee:set-payload>
    </ee:message>
  </ee:transform>
  <flow-ref name="subflow_ex1"/>
</flow>
<sub-flow name="subflow_ex1">
  <ee:transform doc:name="Transform Message">
    <ee:message>
      <ee:set-payload><![CDATA[%dw 2.0
import * from dw::util::Timer
output application/json
var conversionRate=13
---
priceList: payload.prices mapObject(value, key, index) -> {
  (key) : {
       dollars: value,
       localCurrency: value * conversionRate,
       index_plus_1: index + 1,
       timer : currentMilliseconds()
   }
}]]>
      </ee:set-payload>
   </ee:message>
 </ee:transform>
 <flow-ref name="subflow_ex2"/>
</sub-flow>
<sub-flow name="subflow_ex2">
  <logger level="INFO" doc:name="Logger" message="#[payload]" />
</sub-flow>
```

The example produces the following output:

Output

```json
{
  "priceList": {
    "basic": {
      "dollars": "9.99",
      "localCurrency": 129.87,
      "index_plus_1": 1,
      "timer": 1533024312658
    },
    "premium": {
      "dollars": "53",
      "localCurrency": 689,
      "index_plus_1": 2,
      "timer": 1533024312659
    },
    "vip": {
      "dollars": "398.99",
      "localCurrency": 5186.87,
      "index_plus_1": 3,
      "timer": 1533024312659
    }
  }
}
```

## Error Handling

You can add [error handlers](https://docs.mulesoft.com/mule-runtime/4.4/error-handling) to the Flow components (but not to subflows).

In Studio, you can simply expand the Error Handling area at the bottom of the Flow component and
drag the error components into the flow. Note that this process automatically places the component
into the Error Handler component, so there is no need to add the Error Handler component first.

Near the end of this XML example, the flow shows an On Error Continue configuration
(`<on-error-continue/>`):

Example: XML for Flow with On Error Continue Configuration

```xml
<flow name="flow_subflowFlow" >
  <scheduler doc:name="Scheduler" >
    <scheduling-strategy >
      <fixed-frequency frequency="10" timeUnit="SECONDS"/>
    </scheduling-strategy>
  </scheduler>
  <ee:transform doc:name="Transform Message" >
    <ee:message >
      <ee:set-payload ><![CDATA[
 %dw 2.0
 var myXml =  read('<prices></prices>', 'application/xml')
 output application/xml
 ---
 myXml
]]></ee:set-payload>
    </ee:message>
  </ee:transform>
  <logger level="INFO" doc:name="Logger" message='#[payload.prices]'/>
  <error-handler >
    <on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" type="EXPRESSION" when="#[isEmpty(payload.prices)]">
      <logger level="ERROR" doc:name="Logger" message='"An Error Occurred"'/>
    </on-error-continue>
  </error-handler>
</flow>
```

The preceding example produces an error. Notice the following:

1.  The variable `var myXml` in `<set-payload/>` configures an empty `<prices></prices>` tag as the
    payload.

2.  The Logger beside the `<set-payload/>` component returns `null` because it is set to log the
    value of the empty tag.

3.  The `<on-error-continue/>` component returns an error because the DataWeave condition
    `isEmpty(payload.prices)` returns `true`.

4.  The Logger (`<logger/>`) within `<on-error-continue/>` configures an error message to print to
    the console when an error occurs. The resulting error message looks something like this:

```text
ERROR 2018-07-30 23:58:45,293 [[MuleRuntime].cpuLight.06:
 [flow_subflow].flow_subflowFlow.CPU_LITE @1b1529b2]
 [event: 0-2aba3280-948f-11e8-82d0-f45c898f2549]
 org.mule.runtime.core.internal.processor.LoggerMessageProcessor:
 "An Error Occurred"
```

For details about these components, see [On-Error](https://docs.mulesoft.com/mule-runtime/4.4/on-error-scope-concept) error handlers.

## See Also

- [Back-Pressure and MaxConcurrency](https://docs.mulesoft.com/mule-runtime/4.4/tuning-backpressure-maxconcurrency)
