Contact Us 1-800-596-4880

Async Scope Reference

Overview

An async scope is a branch processing block that executes simultaneously with the parent message flow. This type of processing block can prove useful for executing time-consuming operations (such as printing a file or connecting to a mail server) - as long as those operations do not require sending a response back to the parent flow. In other words, the main flow can continue execution while it initiates and processes the asynchronous scope; it does not have to pause until the last message processor embedded in the asynchronous flow has completed its task.

To facilitate this simulataneous branch processing, the async sends one copy of the message it has received to the first embedded message processor in its own processing block; at the same time it sends another copy of the message to the next message processor in the main flow (see below).

Async+scope+schematic

Since they operate on a copy of the message on a different thread, async scopes cannot, by definition, support request-response exchange patterns. Instead, they must implement one of several supported one-way processing strategies, as detailed in the configuration section, below.

If no processing strategy is configured for the async scope, Mule applies the default processing strategy: queued asynchronous.

Async Scopes vs. Asychronous Child Flows

An async scope is similar to an asynchronous child flow in that:

  • It processes the message asynchronously with the main flow, so the message is simultaneously processed in the async scope without pausing the processing in the main flow thread

  • Does not pass data from the scope back into the main flow thread

  • It can have its own processing strategy

However, unlike an asynchronous child flow, an async scope:

  • Exists in-line with the main flow thread

  • Is not called by a flow reference component

  • Is not re-usable

  • Cannot have its own exception handling strategy - it inherits this from in which it resides

Async Scopes vs. Subflows

An async scope is similar to a subflow in that it inherits the exception strategy of the main flow.

However, unlike a subflow, an async scope:

  • Processes messages asynchronously

  • Does not pass data back to the main flow

  • Exist in-line with the main flow thread

  • Is not called by a flow reference component

  • Is not re-usable

Even though the Async scope receives a copy of the Mule message, the payload is not copied. The same payload object(s) will be referenced by both Mule messages: the one that continues down the original flow and the one processed by the Async scope.

In other words, if the payload of your message is mutable object (for example a bean with different fields in it) and a message processor in your async scope changes the value of one of the fields, the message processor outside of the Async scope will see the changed values.

Configurations

STUDIO Visual Editor

  1. Drag the async scope to the canvas and position it within the sequence of message processor that make up your flow at the point where you want to initiate an asynchronous processing block.

    Studio_Async_step1
  2. Within the scope area, add and configure the sequence of message processors that you want to execute asynchronously with the main flow. See example below.

    Studio_Async_Step2
  3. Optionally, configure the async scope itself with a Display Name. If you wish to specify a Processing Strategy, see the instructions in the next section.

    Studio_Async_Step4

XML Editor or Standalone

  1. Add an async element to your flow at the point where you want to initiate an asynchrounous processing block. Refer to the code sample below.

  2. Configure the scope according to the tables below.

    Element Description

    async

    Use to create a block of message processors that execute asynchronously while the rest of the flow continues to execute in parallel.

    Attibute Description

    doc:name

    Customize to display a unique name for the async scope in your application.

    Note: Attribute not required in Mule Standalone configuration.

  3. Configure one or more child elements within the async scope to define the processing that should occur within the asynchronous processing block. Refer to code sample below. If you wish to specify a Processing Strategy, see this instructions in the next section.

    Child Element Description

    processingStrategy

    Optional. Specify the name of a processing strategy that you have defined as a global element.

<flow name="Async_Scope_ExampleFlow1" doc:name="Async_Scope_FlowFlow1" >
   <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
      <async doc:name="Async">
            <component doc:name="Takes a long time"/>
            <jms:outbound-endpoint doc:name="Store result"/>
      </async>
   <expression-transformer doc:name="Create Response"/>
</flow>

Configuring a Processing Strategy

Although it is optional, you can configure the Processing Strategy of the async scope to the following one of the available processing strategies.

Strategy Description

Asynchronous Processing Strategy

After the inbound endpoint finished processing the message, the rest of the flow runs in another thread.

Custom Processing Strategy

A user-written processor strategy

Queued Asynchronous Processing Strategy

After the inbound endpoint finishes processing the message, it write the message to a SEDA queue. The rest of the flow runs in a thread from the SEDA queue’s thread pool.

Queued Thread Per Processor Processing Strategy

After the inbound endpoint finishes processing the message, it writes that messages to a SEDA queue. From that point onward, every remaining processor in the flow runs sequentially in a different thread.

Thread Per Processor Processor Strategy

After the inbound endpoint finishes processing the message, every remaining processor in the flow runs sequentially in a different thread

For more information about these processing strategies and how to configure them, see Flow Processing Strategies.

STUDIO Visual Editor

  1. Click the add to the right of the Processing Strategy field.

    Studio_Async_ScopeProperties_Add
  2. In the Choose Global Type window, select from the list of available processing strategies, then click OK.

    Studio_Async_ChooseGlobalType
  3. Configure the processing strategy as needed. For more information, see Flow Processing Strategies.

Studio or Standalone XML

  1. Define your processing strategy as a global element, with any necessary configuration or optional fine-tuning. (For more information, see [Flow Processing Strategies].) Refer to code sample below.

  2. Add a processingStrategy attribute to your async element to specify the processing strategy by name, as in the code sample.

    <queued-asynchronous-processing-strategy name="Allow42Threads" maxThreads="42" doc:name="Queued Asynchronous Processing Strategy"/>
    
    <flow name="Async_Scope_ExampleFlow1" doc:name="Async_Scope_FlowFlow1" >
       <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
          <async doc:name="Async" processingStrategy="Allow42Threads">
                <component doc:name="Takes a long time"/>
                <jms:outbound-endpoint doc:name="Store result"/>
          </async>
       <expression-transformer doc:name="Create Response"/>
    </flow>

Complete Example Code

Namespace:

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.0" 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

Body:

<queued-asynchronous-processing-strategy name="Allow42Threads" maxThreads="42" doc:name="Queued Asynchronous Processing Strategy"/>

<flow name="Async_Scope_ExampleFlow1" doc:name="Async_Scope_FlowFlow1" >
   <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
      <async doc:name="Async" processingStrategy="Allow42Threads">
            <component doc:name="Takes a long time"/>
            <jms:outbound-endpoint doc:name="Store result"/>
      </async>
   <expression-transformer doc:name="Create Response"/>
</flow>

See Also