Mule Tutorial Series: Using All Flow control component

In continuation of the ‘Mule Tutorial Series’, this blog post intends to explain the use of all flow control component.

While modeling certain flows there is a need to pass the same message to multiple flow paths and later on aggregate the information received from individual path. For example consider that a consumer wants to determine the price offered by multiple online vendors for a specific product; ‘All’ flow component helps achieve this functionality.

The graphical depiction of a Mule flow using the ‘All’ component is shown below:

All Flow Component

The starting point of this Mule flow the HTTP endpoint is configured as below:

In the General tab of the endpoint configure the following properties with the adjoining values:

Exchange-patterns – Request-response
Host – localhost
Port – 8089
Path – fnj

The next component Body to Parameter Map converts the HTTP request and query parameters into Map. The next component is ‘All’. Retain its default values. The parallel paths move processing to Java components. The configuration of the individual Java component is as below:

In the General tab of Java component define the ‘Class name’ property as com.vinraj.integration.forknjoin.PriceGrabber1 and com.vinraj.integration.forknjoin.PriceGrabber2.

Each Java component represents an online vendor and returns price value for that vendor.

The next component is the Logger component. Its configuration is as shown:

Set the message property in the general tab to #[string:Price from multiple vendors: #[payload]]

The Logger output is submitted to another Java component. This component converts the Collection payload into a XML.

Set the class name of the Java component to com.vinraj.integration.forknjoin.PriceGrabberResponseCreator.

The source code of the 3 Java classes is shown below:

package com.vinraj.integration.forknjoin;

import java.util.Map;

public class PriceGrabber1 {
	
	public Integer processReq(Map map) {
		return Integer.valueOf(4);
	}
}
package com.vinraj.integration.forknjoin;

import java.util.Map;

public class PriceGrabber2 {

	public Integer processReq(Map map) {
		return Integer.valueOf(17);
	}	
}
package com.vinraj.integration.forknjoin;

import java.util.List;

public class PriceGrabberResponseCreator {
	
	public String processPrices(List priceList) {
		StringBuffer sb = new StringBuffer();
		sb.append("<Prices>\n");
		for (Object object : priceList) {
			sb.append("\t <Price>");
			sb.append(object.toString());
			sb.append("</Price>\n");

		}
		sb.append("</Prices>");
		return sb.toString();
	}
}

To test the flow type in the following URL in browser window

http://localhost:8089/fnj

The output generated is shown below:

<Prices>
	 <Price>4</Price>
	 <Price>17</Price>
</Prices>

The Mule flow source code is shown below:

<?xml version="1.0" encoding="UTF-8"?>

<mule>
    <flow name="http_flowcontrolforknjoinFlow1" doc:name="http_flowcontrolforknjoinFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8089" path="fnj"  doc:name="HTTP"/>
        <http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/>
        <response>
            <component class="com.vinraj.integration.forknjoin.PriceGrabberResponseCreator" doc:name="Java"/>
        </response>
        <response>
            <all doc:name="All" enableCorrelation="ALWAYS">
                <component class="com.vinraj.integration.forknjoin.PriceGrabber1" doc:name="Java"/>
                <component class="com.vinraj.integration.forknjoin.PriceGrabber2" doc:name="Java"/>
            </all>
            <!-- <collection-aggregator doc:name="Collection Aggregator" />-->
            <logger level="WARN" message="#[string:Price from multiple vendors: #[payload]]" doc:name="Logger"/>
        </response>
    </flow>
</mule>

Note: the XML namespaces added within the mule element tag has been removed for code legibility

Advertisements

3 thoughts on “Mule Tutorial Series: Using All Flow control component

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s