EIP经典案例LoadBroker——MuleESB实践author:mythmoon@163.comVersion0.1(draft)转载保留出处读者背景:具有2年以上J2EE开发背景,熟悉Spring,Hibernate,Jbpm,Axis,Tuscany,Mule等框架,对EJB,Jms,soap,ws-*,BPM,SCA\SDO等技术规范,对ESB,SOA,通讯网关有一定了解,同时对底层Socket通讯协议HTTP,FTP等也有一定了解.当然对主流数据库也要熟悉,在示例中,使用DB2V8.1LoadBroker案例背景ObtainingaLoanQuoteWhenshoppingforaloan,acustomerusuallycallsseveralbankstofindthedealwiththebestpossibleinterestrate.Eachbankasksthecustomerforhisorhersocialsecuritynumber,theamountoftheloan,andthedesiredterm(i.e.,thenumberofmonthsuntiltheloanhastobepaidoff).Eachbanktheninvestigatesthecustomer'screditbackground,usuallybycontactingacreditagency.Basedontherequestedtermsandthecustomer'scredithistory,thebankrespondswithaninterestratequotetotheconsumer(ordeclinesthankfully).Oncethecustomerhasreceivedquotesfromallbanks,heorshecanthenselectthebestofferwiththelowestinterestrate.注解:AConsumerTalkingtoBankstoGetaLoanQuoteFigureI-1Becausecontactingmultiplebankswithaloanquoterequestisatedioustask,loanbrokersofferthisservicetoconsumers.Aloanbrokeristypicallynotaffiliatedwithanyonebankbuthasaccesstomanylendinginstitutions.Thebrokergathersthecustomerdataonceandcontactsthecreditagencytoobtainthecustomer'scredithistory.Basedonthecreditscoreandhistory,thebrokerpresentstherequesttoanumberofbanksthatarebestsuitedtomeetthecustomer'scriteria.Thebrokergatherstheresultingquotesfromthebanksandselectsthebestoffertopassbacktotheconsumer.注解:ALoanBrokerActingasIntermediaryFigureI-2LoadBroker分析设计DesigningtheMessageFlowWewanttodesignaloanbrokersystemusingintegrationpatternsdiscussedinthepreviouschapters.Todothis,let'sfirstlisttheindividualtasksthattheloanbrokerneedstoperform.1.Receivetheconsumer'sloanquoterequest.2.Obtaincreditscoreandhistoryfromcreditagency.3.Determinethemostappropriatebankstocontact.4.Sendarequesttoeachselectedbank.5.Collectresponsesfromeachselectedbank.6.Determinethebestresponse.7.Passtheresultbacktotheconsumer.Let'sseewhichpatternscouldhelpusdesignandimplementtheloanbroker.Thefirststepdescribeshowthebrokerreceivestheincomingrequest.MessagingEndpoints,sofornow,let'sskipoverthisstepandassumethemessageissomehowreceivedbytheloadbroker.Next,thebrokerhastoretrievesomeadditionalinformation:thecustomer'screditscore.AContentEnrichersoundsliketheidealchoiceforthistask.Oncethebrokerhasthecompleteinformation,thebrokermustdeterminetheappropriatebankstoroutetherequestmessageto.WecanaccomplishthiswithanotherContentEnricherthatcomputesthelistofrecipientsfortherequest.SendingarequestmessagetomultiplerecipientsandrecombiningtheresponsesbackintoasinglemessageisthespecialtyoftheScatter-Gather.TheScatter-GathercanuseaPublish-SubscribeChanneloraRecipientListtosendtherequesttothebanks.Oncethebanksreplywiththeirratequotes,theScatter-GatheraggregatestheindividualratequotesintoasinglequotefortheconsumerusinganAggregator.Ifwemodelthemessageflowusingthesepatterns,wearriveatthefollowingdesign:注解:SimpleLoanBrokerDesignFigureI-3Wehavenotyetaccountedforthelikelyeventthateachbankmayuseaslightlydifferentmessageformatfortheloanrequestandresponse.Becausewewanttoseparatetheroutingandaggregationlogicfromthebanks'proprietaryformats,weneedtoinsertMessageTranslatorsintothecommunicationlinesbetweenthebrokerandthebanks.WecanuseaNormalizertotranslatetheindividualresponsesintoacommonformat:CompleteLoanBrokerDesignFigureI-4扩展:核心技术与实现方案Sequencing:SynchronousversusAsynchronousSofar,wehavedescribedtheflowofthemessagesandtheroutingandtransformationpatternswecanusetodescribethedesignoftheloanbrokercomponent.Wehavenotyetdiscussedthetimingofthebrokeroperation.Wehavetwoprimarychoices:Synchronous(Sequential):Thebrokerasksonebankforthequoteandwaitsforaresponsebeforecontactingthenextbank.Asynchronous(Parallel):Thebrokersendsallquoterequestsatonceandwaitsfortheanswerstocomeback.WecanuseUMLsequencediagramstoillustratethetwooptions.Thesynchronousoptionimpliesasequentialprocessingofallloanrequests(seethefollowingfigure).Thissolutionhastheadvantageofbeingsimplertomanagebecausewedonothavetodealwithanyconcurrencyissuesorthreads.However,itisaninefficientsolutionbecausewedonottakeadvantageofthefactthateachbankpossessesindependentprocessingpowerandcouldbeexecutingrequestssimultaneously.Asaresult,theconsumermighthavetowaitalongtimeforananswer.Synchronous,SequentialProcessingofLoanRequestsFigureI-5Theasynchronoussolutionissuesallrequestsrightawaysothateachbankcanstartprocessing.Asthebanksfinishthecomputation,theyreturntheresultstotheloanbroker.Thissolutionallowsforamuchquickerresponse.Forexample,ifallbankstakeasimilaramountoftimetoproducealoanquote,thissolutionisalmostntimesfaster,wherenisthenumberofbankswearedealingwith.Theloanbrokernowmustbeabletoaccepttheloanquotereplymessagesinanyorder,becausethereisnoguaranteethatresponsesarriveinthesameorderthatrequestsweremade.Thefollowingsequencediagramillustratesthisoption.Theopenarrowheadonaloanquoterequestindicatesanasynchronousinvocation.Asynchronous,ParallelProcessingofLoanRequestsFigureI-6Anothersignificantadvantageofusingasynchronousinvocationviaamessagequeueistheabilitytoc