KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > samples > loanbroker > SyncLoanBroker


1 /*
2  * $Id: SyncLoanBroker.java 3982 2006-11-22 14:28:01Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.samples.loanbroker;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.impl.MuleMessage;
16 import org.mule.impl.RequestContext;
17 import org.mule.umo.UMOEventContext;
18 import org.mule.umo.UMOMessage;
19
20 /**
21  * <code>SyncLoanBroker</code> is a synchronous Loan Broker that makes the calls to
22  * various components through the event context synchronously.
23  */

24 public class SyncLoanBroker
25 {
26     /**
27      * logger used by this class
28      */

29     protected static Log logger = LogFactory.getLog(SyncLoanBroker.class);
30
31     public UMOMessage getLoanQuote(LoanRequest request) throws Exception JavaDoc
32     {
33         logger.info("\nClient " + request.getCustomer().getName() + " with ssn= "
34                     + request.getCustomer().getSsn() + " requests a loan of amount= "
35                     + request.getLoanAmount() + " for " + request.getLoanDuration() + " months");
36
37         BankQuoteRequest bqr = new BankQuoteRequest();
38         bqr.setLoanRequest(request);
39         UMOEventContext context = RequestContext.getEventContext();
40
41         // get the customers credit profile
42
UMOMessage result = context.sendEvent(bqr);
43         bqr.getLoanRequest().setCreditProfile(
44             ((BankQuoteRequest)result.getPayload()).getLoanRequest().getCreditProfile());
45
46         // This asynchronous dispatch will invoke all the bank services concurrently
47
// The response of the Banks is handled by the response-router on this
48
// component
49
// that will block until the requests are received, then aggregate them and
50
// send back a response
51

52         // In order for the response router to tie up the response events with this
53
// request we must
54
// do a couple of things.
55
// 1. Create our outbound message first before dispatching. This is so that
56
// we can return the
57
// the message and the response router can correlate reply messages based on
58
// it's Id.
59
// 2. Call setStopFurtherProcessing() so that Mule knows not to do any more
60
// processing of the
61
// event
62
// 3. Must return the dispatched message from this call so that the response
63
// transformer can get
64
// the message id of the dispatched message. Of course custom implementations
65
// can ignore these requirements
66
// and implement a custom router that aggregates using something other than
67
// the UMOMessage.getUniqueId()
68

69         UMOMessage msg = new MuleMessage(bqr);
70         // dispatch the message using the default outbound router settings
71
context.dispatchEvent(msg);
72         context.setStopFurtherProcessing(true);
73         return msg;
74     }
75
76 }
77
Popular Tags