Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Foreach Processing and Choice Routing Example

This Mule ESB example application demonstrates the power of content-based routing in Web service integrations.

Based on the Loan Broker Example included in chapter nine of Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions (Hohpe, Woolf, 2004), this application minimizes a borrower’s work in acquiring the best interest rate for a loan. This example should help you to make informed decisions about using the four elements in your Mule applications listed below. 

Content-Based Routing

content_routingMule has the ability to intelligently route a message through different processing pathways according to its content. Using a Choice Router, Mule uses an expression to evaluate a message’s properties, or part of its payload, then routes it to a specific pathway (i.e. series of message processors). Content-based routing, as this activity is called, dynamically applies routing criteria to a message at runtime.

Foreach Processing

iteration2An iterative processor, Foreach splits collections into elements, then processes them iteratively without losing any of the message payload. After Foreach splits a message collection and processes the individual elements, it returns a reference to the original message thus resulting in "Java in, Java out" processing.

Message Enriching

enriched4Mule uses Message Enrichers to enrich message payloads with data (i.e. add to the payload), rather than changing payload contents. Mule enriches a message’s payload so that other message processors in the application can access the original payload.

Assumptions

This document assumes that you are familiar with Mule ESB and the Anypoint™ Studio interface. To increase your familiarity with Studio, consider completing one or more Anypoint Studio Tutorials.

This document describes the details of the example within the context of Anypoint Studio, Mule ESB’s graphical user interface (GUI), and includes XML configuration details in separate tabs.

Example Use Case

This example application exposes a Web service that processes end user requests for interest rate quotes from banks. From a Web browser, an end user submits a request which contains the following:

  • borrower’s name

  • amount of the loan

  • repayment term of the loan

  • borrower’s social security number (SSN)

The application accesses other Web services to process the request, then responds to the end user. The response identifies the bank name and interest rate of the lowest quote.

The Web service uses five elements to maximize its processing efficiency:

  • Choice Flow Controls to route messages based on content

  • Transformers to convert message payloads’ data format

  • Message Enrichers to add to, rather than change, message payloads

  • Foreach to iteratively, and synchronously, process message payloads

  • Dynamic connectors to send requests to different banks depending on the message payload

Set Up and Run the Example

As with other example templates, you can create template applications straight out of the box in Anypoint Studio or, in this case, also in Mule Standalone where this example is called loanbroker-simple. You can tweak the configurations of these use case-based examples to create your own customized applications in Mule.

Follow the procedure below to create, then run the Loan Broker application in Mule ESB.

  1. Create, then run the example application in Anypoint Studio or Standalone Mule.

  2. Open your Web browser.

  3. Type http://localhost:11081/?name=Muley&amount=20000&term=48&ssn=1234 into the address bar of your browser, then press enter to elicit a response from the application (below). 

    submitted_m

  4. In your browser’s address bar, replace the amount value with 5000, then press enter to elicit a new response from the application (below). 

    submitted2_m

How it Works

This example application consists of several flows and subflows. These flows integrate Web services to process end user requests for interest rate quotes from banks.

The sections below offer flow-by-flow descriptions of the application’s actions as it processes end user requests.

loan-broker-sync Flow

As the main flow in the application, this flow coordinates data collection among flows to produce an end user response.

mainLoanBrokerFlow


    
             
          
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<flow doc:description=" The main loanbroker flow that: i) Receives a customer request ii) Performs a lookup of the customer credit profile using a component binding iii) Determines the bank that should be used to request quotes iv) Sends the request to the selected banks and aggregates responses v) Selects the lowest quote from the list of quotes vi) Returns the response to the client   " doc:name="loan-broker-sync" name="loan-broker-sync">
        <description>
            The main loanbroker flow that:
            i) Receives a customer request
            ii) Performs a lookup of the customer credit profile using a component
            binding
            iii) Determines the bank that should be used to request quotes
            iv) Sends the request to the selected banks and aggregates responses
            v) Selects the lowest quote from the list of quotes
            vi) Returns the response to the client
        </description>
        <http:inbound-endpoint address="http://0.0.0.0:11081" doc:name="HTTP" exchange-pattern="request-response"/>
        <http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/>
        <choice doc:name="Choice">
            <when expression="!(payload.name == null || payload.ssn == null || payload.amount == null || payload.term==null)">
                <expression-component doc:name="create customer request"><![CDATA[import org.mule.example.loanbroker.message.CustomerQuoteRequest;
import org.mule.example.loanbroker.model.Customer;
payload = new CustomerQuoteRequest(new Customer(payload.name,
Integer.parseInt(payload.ssn)),
Integer.parseInt(payload.amount),
Integer.parseInt(payload.term));]]></expression-component>
                <enricher doc:name="Enrich with creditProfile" source="#[payload]" target="#[flowVars.creditProfile]">
                    <flow-ref doc:name="lookupCustomerCreditProfile" name="lookupCustomerCreditProfile"/>
                </enricher>
                <enricher doc:name="Enrich with banks" source="#[payload]" target="#[flowVars.banks]">
                    <flow-ref doc:name="lookupBanks" name="lookupBanks"/>
                </enricher>
                <set-variable doc:name="create empty quotes" value="#[new java.util.LinkedList()]" variableName="quotes"/>
                <foreach collection="#[flowVars.banks]" doc:name="Foreach">
                    <enricher doc:name="Message Enricher" target="#[quotes.add($)]">
                        <flow-ref doc:name="lookupLoanQuote" name="lookupLoanQuote"/>
                    </enricher>
                </foreach>
                <flow-ref doc:name="findLowestLoanQuote" name="findLowestLoanQuote"/>
                <object-to-string-transformer doc:name="Object to String"/>
            </when>
            <otherwise>
                <expression-component doc:name="set error message"><![CDATA[payload="Error: incomplete request"]]></expression-component>
            </otherwise>
        </choice>
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload doc:name="Set error message" value="Error processing loan request"/>
        </catch-exception-strategy>
    </flow>

The request-response HTTP Inbound connector in this flow receives an end user request. Because it has a request-response exchange pattern, this HTTP connector is responsible for both receiving and returning messages.

Next, the Body to Parameter Map Transformer converts the data format of the message payload from http://en.wikipedia.org/wiki/HTTP_body_data[HTTP body data] to a Java http://en.wikipedia.org/wiki/Associative_array[map]. The Loan Broker application only processes Java message payloads.

Then, Mule employs a content-based router to direct the message for further processing. The Choice Router routes each message to one of two processing pathways according to its payload contents (see image and code below).

  • If the message payload contains a complete request (i.e. the borrower’s name and SSN, and the amount and the term of the loan), the choice flow control passes the message to the create customer request Expression Component.

  • If the message payload is an incomplete request, the choice flow control passes the message to the set error message expression component. This component sets the payload of the message to read Error: incomplete request. Mule processes the message no further. Instead, it responds to the end user with the error message. 

choiceproperties


    
             
          
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<choice doc:name="Choice">
            <when expression="!(payload.name == null || payload.ssn == null || payload.amount == null || payload.term==null)">
                <expression-component doc:name="create customer request"><![CDATA[import org.mule.example.loanbroker.message.CustomerQuoteRequest;
import org.mule.example.loanbroker.model.Customer;
payload = new CustomerQuoteRequest(new Customer(payload.name,
Integer.parseInt(payload.ssn)),
Integer.parseInt(payload.amount),
Integer.parseInt(payload.term));]]></expression-component>
                <enricher doc:name="Enrich with creditProfile" source="#[payload]" target="#[flowVars.creditProfile]">
                    <flow-ref doc:name="lookupCustomerCreditProfile" name="lookupCustomerCreditProfile"/>
                </enricher>
                <enricher doc:name="Enrich with banks" source="#[payload]" target="#[flowVars.banks]">
                    <flow-ref doc:name="lookupBanks" name="lookupBanks"/>
                </enricher>
                <set-variable doc:name="create empty quotes" value="#[new java.util.LinkedList()]" variableName="quotes"/>
                <foreach collection="#[flowVars.banks]" doc:name="Foreach">
                    <enricher doc:name="Message Enricher" target="#[quotes.add($)]">
                        <flow-ref doc:name="lookupLoanQuote" name="lookupLoanQuote"/>
                    </enricher>
                </foreach>
                <flow-ref doc:name="findLowestLoanQuote" name="findLowestLoanQuote"/>
                <object-to-string-transformer doc:name="Object to String"/>
            </when>
            <otherwise>
                <expression-component doc:name="set error message"><![CDATA[payload="Error: incomplete request"]]></expression-component>
            </otherwise>
        </choice>

The create customer request component uses expressions to extract data from the message payload. It uses the data to create a new Java object with three values:

  1. the Customer, which identifies both the borrower’s name and SSN

  2. one Integer, which identifies the amount of the loan

  3. a second Integer, which identifies the loan repayment term


          
       
1
2
3
4
5
6
<expression-component doc:name="create customer request"><![CDATA[import org.mule.example.loanbroker.message.CustomerQuoteRequest;
import org.mule.example.loanbroker.model.Customer;
payload = new CustomerQuoteRequest(new Customer(payload.name,
Integer.parseInt(payload.ssn)),
Integer.parseInt(payload.amount),
Integer.parseInt(payload.term));]]></expression-component>

With a new CustomerQuoteRequest object in its payload, the message encounters its first Message Enricher. Throughout this flow, Mule enriches messages with data rather than changing the payload contents. By enriching a message, Mule preserves the payload content so that other elements in the application can access the original data.

The Enrich with creditProfile enricher contains only a Flow Reference Component. This type of component invokes other flows, or subflows, in the application to acquire, then add data to the message. In this case, the lookupCustomerCreditProfile component demands that the lookupCustomerCreditProfile subflow access an external Web service to acquire the borrower’s credit score. Mule enriches the message with the credit score, then passes the message to the next enricher in the flow.

As with its predecessor, the Enrich with Banks enricher uses a flow reference component to invoke a subflow and acquire data. In this case, instead of adding a credit score, Mule uses the result of the LookupBanks subflow to add a http://en.wikipedia.org/wiki/List_(abstract_data_type)[list] of banks to the message payload.

Mule then uses a Variable Transformer to create an empty list variable. Mule will fill this empty quotes list variable with the quotes it fetches from banks. With an empty list to fill, the message next encounters a Foreach scope. One by one, this iterative processor fetches data to populate each item on the list.

To fetch these data, the flow reference component first invokes the lookupLoanQuote subflow to acquire a quote from a bank. Then, the message enricher adds the quote to the list variable. Foreach continues to invoke, then enrich, until it has acquired a quote from each bank on the list of banks. Foreach then passes the message to the next message processor in the flow.

To illustrate foreach’s behavior with an example, imagine a message payload with the following contents:

  • an empty quotes list variable

  • banks list variable naming two banks from which Mule must request a quote: MultiNational Bank and IndustrialGrowth Bank

Foreach processes the message payload as follows:

  1. Foreach consults the banks list variable to learn that it should send its first request to MultiNational.

  2. Foreach invokes the lookupLoanQuote subflow.

  3. The lookupLoanQuote subflow calls the getLoanQuote Web service to obtain an interest rate quote from MultiNational.

  4. The lookupLoanQuote subflow provides the Web service response to the loan-broker-sync flow.

  5. The message enricher inserts the interest rate quote from MultiNational into the quotes list variable.

  6. Foreach consults the banks list variable to learn that it should send its second request to IndustrialGrowth.

  7. Foreach invokes the lookupLoanQuote subflow.

  8. The lookupLoanQuote subflow calls the getLoanQuote Web service to obtain an interest rate quote from IndustrialGrowth.

  9. The lookupLoanQuote subflow provides the Web service response to the loan-broker-sync flow.

  10. The message enricher inserts the interest rate quote from IndustrialGrowth into the quotes list variable.

  11. Foreach consults the banks list variable to find no more items on the list. It passes the message — now with a list containing two interest rate quotes — to the next message processor. Refer to the table below for a before-and-after comparison of message contents.

Message Contents Before
Iterative Processing
Message Contents After
Iterative Processing

banks list variable:
www.multinational.com/loans/quotes
www.industrialgrowth.com/loans/quotes

banks list variable:
www.multinational.com/loans/quotes
www.industrialgrowth.com/loans/quotes

quote list variable:

quote list variable:
• 6.99
• 6.84

The penultimate message processor in this flow references yet another subflow in the application. The findLowestLoanQuote subflow determines which quote in the list is the lowest, then logs the result in the message payload.

Finally, the Object to String Transformer converts the message payload’s data format from Java to a string. The HTTP connector sends the response to the end user.

Notice that the loan-broker-sync flow also contains a Catch Exception Strategy. Rather than use Mule’s default exception strategy, this flow uses a customized exception strategy to handle errors. If an error occurs in the flow, the exception strategy’s Set Payload Transformer sets an error message on the payload. The application sends this error message, which reads, Error processing loan request, as a response to the end user.

lookupCustomerCreditProfile Subflow

Invoked upon demand by the loan-broker-sync flow, this subflow acquires and logs the borrower’s credit score on the message payload.

lookupCustomerCreditProfile


    
             
          
1
2
3
4
5
6
7
8
9
10
11
&lt;sub-flow doc:description="    Returns the customer credit profile obtained form the Credit Agency   " doc:name="lookupCustomerCreditProfile" name="lookupCustomerCreditProfile"&gt;
        &lt;description&gt;
            Returns the customer credit profile obtained form the Credit Agency
        &lt;/description&gt;
        &lt;set-payload doc:name="customer" value="#[payload.customer]"/&gt;
        &lt;processor-chain doc:name="Processor Chain"&gt;
            &lt;cxf:jaxws-client doc:name="getCreditProfile" operation="getCreditProfile" serviceClass="org.mule.example.loanbroker.creditagency.CreditAgencyService"/&gt;
            &lt;http:outbound-endpoint address="http://localhost:18080/mule/TheCreditAgencyService" doc:name="HTTP"/&gt;
        &lt;/processor-chain&gt;
        &lt;logger doc:name="creditProfile" level="INFO" message="Credit profile: #[payload]"/&gt;
    &lt;/sub-flow&gt;

To acquire the credit score, the customer transformer sets the payload to Customer, as defined by the create customer request expression transformer. (Recall that the Customer variable contains the borrower’s name and SSN.) Mule sends a request to the getCreditProfile SOAP Web service. The HTTP connector inserts the Web service’s response into the subflow.

Mule leverages http://cxf.apache.org/[Apache’s CXF framework] to build Web services. The Processor Chain that wraps the CXF Component and HTTP outbound connector is a CXF requirement. It ensures that Mule completes all processing activities prior to logging the processing result.

Last in this flow, the Logger Component logs the payload of the Web service’s response on the message payload as the Credit Profile.

lookupBanks Subflow

The application prevents exposing all banks to all loan quote requests. A bank that caters to premiere clients, for example, would be irked to receive a request for a quote for a small loan from a borrower with poor credit. To prevent such irksome calls to banks’ Web services, the Loan Broker application employs the LookupBanks subflow.

lookupBanks


    
             
          
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
&lt;sub-flow doc:description="    Returns the list of banks to contact and returns it as a flow variable    'banks'   " doc:name="lookupBanks" name="lookupBanks"&gt;
        &lt;description&gt;
            Returns the list of banks to contact and returns it as a flow variable
            'banks'
        &lt;/description&gt;
        &lt;choice doc:name="Choice"&gt;
            &lt;when expression="payload.getLoanAmount() &gt;= 20000"&gt;
                &lt;expression-component doc:name="Bank1, Bank2"&gt;&lt;![CDATA[payload=[new  java.net.URI('http://localhost:10080/mule/TheBank1'), new java.net.URI('http://localhost:20080/mule/TheBank2')]]]&gt;&lt;/expression-component&gt;
            &lt;/when&gt;
            &lt;when expression="payload.getLoanAmount() &gt;= 10000 || payload.getLoanAmount() &amp;lt;= 19999"&gt;
                &lt;expression-component doc:name="Bank3, Bank4"&gt;&lt;![CDATA[payload=[new java.net.URI('http://localhost:30080/mule/TheBank3'), new java.net.URI('http://localhost:40080/mule/TheBank4')]]]&gt;&lt;/expression-component&gt;
            &lt;/when&gt;
            &lt;otherwise&gt;
                &lt;expression-component doc:name="Bank5"&gt;&lt;![CDATA[payload=[new java.net.URI('http://localhost:50080/mule/TheBank5')]]]&gt;&lt;/expression-component&gt;
            &lt;/otherwise&gt;
        &lt;/choice&gt;
        &lt;logger doc:name="banks" level="INFO" message="Banks to contact: #[payload]"/&gt;
    &lt;/sub-flow&gt;

Mule first uses a choice flow control to examine the amount in the payload, then routes the message according to the size of the loan.

  • If the loan is more that $20,000, the flow control routes the message to the first expression component, labeled Bank 1, Bank 2.

  • If the loan is more than $10,000, the flow control routes the message to the second expression component, labeled Bank 3, Bank 4.

  • if otherwise (i.e. if the loan is $10,000 or less), the flow control routes the message to the third expression component, labeled Bank 5

choice2props


    
             
          
1
2
3
4
5
6
7
8
9
10
11
&lt;choice doc:name="Choice"&gt;
            &lt;when expression="payload.getLoanAmount() &gt;= 20000"&gt;
                &lt;expression-component doc:name="Bank1, Bank2"&gt;&lt;![CDATA[payload=[new  java.net.URI('http://localhost:10080/mule/TheBank1'), new java.net.URI('http://localhost:20080/mule/TheBank2')]]]&gt;&lt;/expression-component&gt;
            &lt;/when&gt;
            &lt;when expression="payload.getLoanAmount() &gt;= 10000 || payload.getLoanAmount() &amp;lt;= 19999"&gt;
                &lt;expression-component doc:name="Bank3, Bank4"&gt;&lt;![CDATA[payload=[new java.net.URI('http://localhost:30080/mule/TheBank3'), new java.net.URI('http://localhost:40080/mule/TheBank4')]]]&gt;&lt;/expression-component&gt;
            &lt;/when&gt;
            &lt;otherwise&gt;
                &lt;expression-component doc:name="Bank5"&gt;&lt;![CDATA[payload=[new java.net.URI('http://localhost:50080/mule/TheBank5')]]]&gt;&lt;/expression-component&gt;
            &lt;/otherwise&gt;
        &lt;/choice&gt;

Note that the choice flow control directs the message to the first expression that evaluates to true. For example, it directs a quote request for a loan of $30,000 only to the Bank 1, Bank 2 component.

Each expression component in this subflow contains the URIs of the banks willing to provide an interest rate quote. For example, messages that pass into the Bank 3, Bank 4 component earn, as a payload addition, the URIs for Banks 3 and 4. The banks Logger component records the list of appropriate banks to which to send a request.

lookupLoanQuote Subflow

This sends a quote request to banks' Web services.

lookupLoanQuote


    
             
          
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
&lt;sub-flow doc:description="    Returns a loanQuote from a given bank's URI   " doc:name="lookupLoanQuote" name="lookupLoanQuote"&gt;
        &lt;description&gt;
            Returns a loanQuote from a given bank's URI
        &lt;/description&gt;
        &lt;set-variable doc:name="bankUri" value="#[payload]" variableName="bankUri"/&gt;
        &lt;expression-component doc:name="create LoanBrokerLoanRequest"&gt;&lt;![CDATA[import org.mule.example.loanbroker.message.LoanBrokerQuoteRequest;
LoanBrokerQuoteRequest bqr = new LoanBrokerQuoteRequest();
bqr.setCreditProfile(flowVars.creditProfile);
payload = bqr;]]&gt;&lt;/expression-component&gt;
        &lt;processor-chain doc:name="Processor Chain"&gt;
            &lt;cxf:jaxws-client doc:name="getLoanQuote" operation="getLoanQuote" serviceClass="org.mule.example.loanbroker.bank.BankService"/&gt;
            &lt;http:outbound-endpoint address="http://#[flowVars.bankUri.getHost()]:#[flowVars.bankUri.getPort()]#[flowVars.bankUri.getPath()]" doc:name="HTTP"/&gt;
        &lt;/processor-chain&gt;
        &lt;logger doc:name="quote" level="INFO" message="LoanQuote from #[flowVars.bankUri]: #[payload]"/&gt;
    &lt;/sub-flow&gt;

First, the variable transformer stores the Mule message payload — the bank’s URI — as a variable named bankUri. (Recall that this subflow receives requests one at a time from foreach in the Loan-broker-sync flow. Each request’s payload a the URI of a bank.)

The create LoanBrokerLoanRequest component uses expressions to extract the borrower’s credit profile (logged by the creditProfile logger in the LookupCustomerCreditProfile flow) from the message payload. It uses the data to create a request to send to the getLoanQuote Web service. Mule uses a CXF component configured as a JAXWS-client to send the request to a bank’s Web service. The HTTP outbound connector dynamically determines where to send the request based on the bank’s URI in the message payload. It receives the response from the banks’ Web service and pushes the response payload to the quote logger to record.

findLowestLoanRequest Subflow

This simple subflow uses an expression component to determine which item, in the list of quotes, offers the lowest interest rate. The Logger records the result.

findLowest


    
             
          
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
&lt;sub-flow doc:description="    Returns the loan quote with the lowest interest rate   " doc:name="findLowestLoanQuote" name="findLowestLoanQuote"&gt;
        &lt;description&gt;
            Returns the loan quote with the lowest interest rate
        &lt;/description&gt;
        &lt;expression-component doc:name="Expression"&gt;&lt;![CDATA[import org.mule.example.loanbroker.model.LoanQuote;
LoanQuote lowestQuote = null;
for (Object current : (List) flowVars.quotes)
{
    LoanQuote loanQuote = (LoanQuote) current;
    if (lowestQuote == null)
    {
    lowestQuote = loanQuote;
    }
    else if (loanQuote.getInterestRate() &lt; lowestQuote.getInterestRate())
    {
        lowestQuote = loanQuote;
    }
}
payload = lowestQuote;]]&gt;&lt;/expression-component&gt;
        &lt;logger doc:name="lowestQuote" level="INFO" message="Lowest loan quote: #[payload]"/&gt;
    &lt;/sub-flow&gt;

The expression in the component compares the getInterestRate of items in the list to each other to determine which one is the lowest (see image below).

expressionLoan


    
             
          
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
&lt;expression-component doc:name="Expression"&gt;&lt;![CDATA[import org.mule.example.loanbroker.model.LoanQuote;
LoanQuote lowestQuote = null;
for (Object current : (List) flowVars.quotes)
{
    LoanQuote loanQuote = (LoanQuote) current;
    if (lowestQuote == null)
    {
    lowestQuote = loanQuote;
    }
    else if (loanQuote.getInterestRate() &lt; lowestQuote.getInterestRate())
    {
        lowestQuote = loanQuote;
    }
}
payload = lowestQuote;]]&gt;&lt;/expression-component&gt;

Mock Flows

The remaining six flows in the application are “mock flows.” They act as external Web services to which the five legitimate flows and subflows call to request data.

Each flow contains:

  • a request-response HTTP connector and CXF component to receive the requests

  • Java Component which produces random data to mimic Web service processing

You do not need to include these flows your customized application; they exist only to support a functional example.

Full Code

foreachProcessingexample


    
            
         
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;mule version="EE-3.5.0" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd  http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd  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 "&gt;
    &lt;description&gt;
        This Synchronous variant of loan broker example is modeled on the
        Enterprise integration Patterns book sample.
        See: http://www.eaipatterns.com/ComposedMessagingExample.html
    &lt;/description&gt;
    &lt;flow doc:description=" The main loanbroker flow that: i) Receives a customer request ii) Performs a lookup of the customer credit profile using a component binding iii) Determines the bank that should be used to request quotes iv) Sends the request to the selected banks and aggregates responses v) Selects the lowest quote from the list of quotes vi) Returns the response to the client   " doc:name="loan-broker-sync" name="loan-broker-sync"&gt;
        &lt;description&gt;
            The main loanbroker flow that:
            i) Receives a customer request
            ii) Performs a lookup of the customer credit profile using a component
            binding
            iii) Determines the bank that should be used to request quotes
            iv) Sends the request to the selected banks and aggregates responses
            v) Selects the lowest quote from the list of quotes
            vi) Returns the response to the client
        &lt;/description&gt;
        &lt;http:inbound-endpoint address="http://0.0.0.0:11081" doc:name="HTTP" exchange-pattern="request-response"/&gt;
        &lt;http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/&gt;
        &lt;choice doc:name="Choice"&gt;
            &lt;when expression="!(payload.name == null || payload.ssn == null || payload.amount == null || payload.term==null)"&gt;
                &lt;expression-component doc:name="create customer request"&gt;&lt;![CDATA[import org.mule.example.loanbroker.message.CustomerQuoteRequest;
import org.mule.example.loanbroker.model.Customer;
payload = new CustomerQuoteRequest(new Customer(payload.name,
Integer.parseInt(payload.ssn)),
Integer.parseInt(payload.amount),
Integer.parseInt(payload.term));]]&gt;&lt;/expression-component&gt;
                &lt;enricher doc:name="Enrich with creditProfile" source="#[payload]" target="#[flowVars.creditProfile]"&gt;
                    &lt;flow-ref doc:name="lookupCustomerCreditProfile" name="lookupCustomerCreditProfile"/&gt;
                &lt;/enricher&gt;
                &lt;enricher doc:name="Enrich with banks" source="#[payload]" target="#[flowVars.banks]"&gt;
                    &lt;flow-ref doc:name="lookupBanks" name="lookupBanks"/&gt;
                &lt;/enricher&gt;
                &lt;set-variable doc:name="create empty quotes" value="#[new java.util.LinkedList()]" variableName="quotes"/&gt;
                &lt;foreach collection="#[flowVars.banks]" doc:name="Foreach"&gt;
                    &lt;enricher doc:name="Message Enricher" target="#[quotes.add($)]"&gt;
                        &lt;flow-ref doc:name="lookupLoanQuote" name="lookupLoanQuote"/&gt;
                    &lt;/enricher&gt;
                &lt;/foreach&gt;
                &lt;flow-ref doc:name="findLowestLoanQuote" name="findLowestLoanQuote"/&gt;
                &lt;object-to-string-transformer doc:name="Object to String"/&gt;
            &lt;/when&gt;
            &lt;otherwise&gt;
                &lt;expression-component doc:name="set error message"&gt;&lt;![CDATA[payload="Error: incomplete request"]]&gt;&lt;/expression-component&gt;
            &lt;/otherwise&gt;
        &lt;/choice&gt;
        &lt;catch-exception-strategy doc:name="Catch Exception Strategy"&gt;
            &lt;set-payload doc:name="Set error message" value="Error processing loan request"/&gt;
        &lt;/catch-exception-strategy&gt;
    &lt;/flow&gt;
    &lt;sub-flow doc:description="    Returns the customer credit profile obtained form the Credit Agency   " doc:name="lookupCustomerCreditProfile" name="lookupCustomerCreditProfile"&gt;
        &lt;description&gt;
            Returns the customer credit profile obtained form the Credit Agency
        &lt;/description&gt;
        &lt;set-payload doc:name="customer" value="#[payload.customer]"/&gt;
        &lt;processor-chain doc:name="Processor Chain"&gt;
            &lt;cxf:jaxws-client doc:name="getCreditProfile" operation="getCreditProfile" serviceClass="org.mule.example.loanbroker.creditagency.CreditAgencyService"/&gt;
            &lt;http:outbound-endpoint address="http://localhost:18080/mule/TheCreditAgencyService" doc:name="HTTP"/&gt;
        &lt;/processor-chain&gt;
        &lt;logger doc:name="creditProfile" level="INFO" message="Credit profile: #[payload]"/&gt;
    &lt;/sub-flow&gt;
    &lt;sub-flow doc:description="    Returns the list of banks to contact and returns it as a flow variable    'banks'   " doc:name="lookupBanks" name="lookupBanks"&gt;
        &lt;description&gt;
            Returns the list of banks to contact and returns it as a flow variable
            'banks'
        &lt;/description&gt;
        &lt;choice doc:name="Choice"&gt;
            &lt;when expression="payload.getLoanAmount() &gt;= 20000"&gt;
                &lt;expression-component doc:name="Bank1, Bank2"&gt;&lt;![CDATA[payload=[new  java.net.URI('http://localhost:10080/mule/TheBank1'), new java.net.URI('http://localhost:20080/mule/TheBank2')]]]&gt;&lt;/expression-component&gt;
            &lt;/when&gt;
            &lt;when expression="payload.getLoanAmount() &gt;= 10000 || payload.getLoanAmount() &amp;lt;= 19999"&gt;
                &lt;expression-component doc:name="Bank3, Bank4"&gt;&lt;![CDATA[payload=[new java.net.URI('http://localhost:30080/mule/TheBank3'), new java.net.URI('http://localhost:40080/mule/TheBank4')]]]&gt;&lt;/expression-component&gt;
            &lt;/when&gt;
            &lt;otherwise&gt;
                &lt;expression-component doc:name="Bank5"&gt;&lt;![CDATA[payload=[new java.net.URI('http://localhost:50080/mule/TheBank5')]]]&gt;&lt;/expression-component&gt;
            &lt;/otherwise&gt;
        &lt;/choice&gt;
        &lt;logger doc:name="banks" level="INFO" message="Banks to contact: #[payload]"/&gt;
    &lt;/sub-flow&gt;
    &lt;sub-flow doc:description="    Returns a loanQuote from a given bank's URI   " doc:name="lookupLoanQuote" name="lookupLoanQuote"&gt;
        &lt;description&gt;
            Returns a loanQuote from a given bank's URI
        &lt;/description&gt;
        &lt;set-variable doc:name="bankUri" value="#[payload]" variableName="bankUri"/&gt;
        &lt;expression-component doc:name="create LoanBrokerLoanRequest"&gt;&lt;![CDATA[import org.mule.example.loanbroker.message.LoanBrokerQuoteRequest;
LoanBrokerQuoteRequest bqr = new LoanBrokerQuoteRequest();
bqr.setCreditProfile(flowVars.creditProfile);
payload = bqr;]]&gt;&lt;/expression-component&gt;
        &lt;processor-chain doc:name="Processor Chain"&gt;
            &lt;cxf:jaxws-client doc:name="getLoanQuote" operation="getLoanQuote" serviceClass="org.mule.example.loanbroker.bank.BankService"/&gt;
            &lt;http:outbound-endpoint address="http://#[flowVars.bankUri.getHost()]:#[flowVars.bankUri.getPort()]#[flowVars.bankUri.getPath()]" doc:name="HTTP"/&gt;
        &lt;/processor-chain&gt;
        &lt;logger doc:name="quote" level="INFO" message="LoanQuote from #[flowVars.bankUri]: #[payload]"/&gt;
    &lt;/sub-flow&gt;
    &lt;sub-flow doc:description="    Returns the loan quote with the lowest interest rate   " doc:name="findLowestLoanQuote" name="findLowestLoanQuote"&gt;
        &lt;description&gt;
            Returns the loan quote with the lowest interest rate
        &lt;/description&gt;
        &lt;expression-component doc:name="Expression"&gt;&lt;![CDATA[import org.mule.example.loanbroker.model.LoanQuote;
LoanQuote lowestQuote = null;
for (Object current : (List) flowVars.quotes)
{
    LoanQuote loanQuote = (LoanQuote) current;
    if (lowestQuote == null)
    {
    lowestQuote = loanQuote;
    }
    else if (loanQuote.getInterestRate() &lt; lowestQuote.getInterestRate())
    {
        lowestQuote = loanQuote;
    }
}
payload = lowestQuote;]]&gt;&lt;/expression-component&gt;
        &lt;logger doc:name="lowestQuote" level="INFO" message="Lowest loan quote: #[payload]"/&gt;
    &lt;/sub-flow&gt;
    &lt;!-- MOCK SERVICES --&gt;
    &lt;flow doc:description="    The credit agency service will get the credit profile for a customer   " doc:name="TheCreditAgencyService" name="TheCreditAgencyService"&gt;
        &lt;description&gt;
            The credit agency service will get the credit profile for a customer
        &lt;/description&gt;
        &lt;http:inbound-endpoint address="http://localhost:18080/mule/TheCreditAgencyService" doc:name="HTTP" exchange-pattern="request-response"/&gt;
        &lt;cxf:jaxws-service doc:name="SOAP" serviceClass="org.mule.example.loanbroker.creditagency.DefaultCreditAgency"/&gt;
        &lt;component doc:name="creditAgency"&gt;
            &lt;singleton-object class="org.mule.example.loanbroker.creditagency.DefaultCreditAgency"/&gt;
        &lt;/component&gt;
    &lt;/flow&gt;
    &lt;flow doc:description="    Mock flow representing Bank 1   " doc:name="Bank1Flow" name="Bank1Flow"&gt;
        &lt;description&gt;
            Mock flow representing Bank 1
        &lt;/description&gt;
        &lt;http:inbound-endpoint address="http://localhost:10080/mule/TheBank1" doc:name="HTTP" exchange-pattern="request-response"/&gt;
        &lt;cxf:jaxws-service doc:name="SOAP" serviceClass="org.mule.example.loanbroker.bank.Bank"/&gt;
        &lt;component doc:name="Bank 1"&gt;
            &lt;singleton-object class="org.mule.example.loanbroker.bank.Bank"&gt;
                &lt;property key="bankName" value="Bank #1"/&gt;
            &lt;/singleton-object&gt;
        &lt;/component&gt;
    &lt;/flow&gt;
    &lt;flow doc:description="    Mock flow representing Bank 2   " doc:name="Bank2Flow" name="Bank2Flow"&gt;
        &lt;description&gt;
            Mock flow representing Bank 2
        &lt;/description&gt;
        &lt;http:inbound-endpoint address="http://localhost:20080/mule/TheBank2" doc:name="HTTP" exchange-pattern="request-response"/&gt;
        &lt;cxf:jaxws-service doc:name="SOAP" serviceClass="org.mule.example.loanbroker.bank.Bank"/&gt;
        &lt;component doc:name="Bank 2"&gt;
            &lt;singleton-object class="org.mule.example.loanbroker.bank.Bank"&gt;
                &lt;property key="bankName" value="Bank #2"/&gt;
            &lt;/singleton-object&gt;
        &lt;/component&gt;
    &lt;/flow&gt;
    &lt;flow doc:description="    Mock flow representing Bank 3   " doc:name="Bank3Flow" name="Bank3Flow"&gt;
        &lt;description&gt;
            Mock flow representing Bank 3
        &lt;/description&gt;
        &lt;http:inbound-endpoint address="http://localhost:30080/mule/TheBank3" doc:name="HTTP" exchange-pattern="request-response"/&gt;
        &lt;cxf:jaxws-service doc:name="SOAP" serviceClass="org.mule.example.loanbroker.bank.Bank"/&gt;
        &lt;component doc:name="Bank 3"&gt;
            &lt;singleton-object class="org.mule.example.loanbroker.bank.Bank"&gt;
                &lt;property key="bankName" value="Bank #3"/&gt;
            &lt;/singleton-object&gt;
        &lt;/component&gt;
    &lt;/flow&gt;
    &lt;flow doc:description="    Mock flow representing Bank 4   " doc:name="Bank4Flow" name="Bank4Flow"&gt;
        &lt;description&gt;
            Mock flow representing Bank 4
        &lt;/description&gt;
        &lt;http:inbound-endpoint address="http://localhost:40080/mule/TheBank4" doc:name="HTTP" exchange-pattern="request-response"/&gt;
        &lt;cxf:jaxws-service doc:name="SOAP" serviceClass="org.mule.example.loanbroker.bank.Bank"/&gt;
        &lt;component doc:name="Bank 4"&gt;
            &lt;singleton-object class="org.mule.example.loanbroker.bank.Bank"&gt;
                &lt;property key="bankName" value="Bank #4"/&gt;
            &lt;/singleton-object&gt;
        &lt;/component&gt;
    &lt;/flow&gt;
    &lt;flow doc:description="    Mock flow representing Bank 5   " doc:name="Bank5Flow" name="Bank5Flow"&gt;
        &lt;description&gt;
            Mock flow representing Bank 5
        &lt;/description&gt;
        &lt;http:inbound-endpoint address="http://localhost:50080/mule/TheBank5" doc:name="HTTP" exchange-pattern="request-response"/&gt;
        &lt;cxf:jaxws-service doc:name="SOAP" serviceClass="org.mule.example.loanbroker.bank.Bank"/&gt;
        &lt;component doc:name="Bank 5"&gt;
            &lt;singleton-object class="org.mule.example.loanbroker.bank.Bank"&gt;
                &lt;property key="bankName" value="Bank #5"/&gt;
            &lt;/singleton-object&gt;
        &lt;/component&gt;
    &lt;/flow&gt;
&lt;/mule&gt;

See Also