KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > samples > loanbroker > esb > routers > BankQuotesResponseAggregator


1 /*
2  * $Id: BankQuotesResponseAggregator.java 3798 2006-11-04 04:07:14Z aperepel $
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.esb.routers;
12
13 import org.mule.config.i18n.Message;
14 import org.mule.impl.MuleMessage;
15 import org.mule.routing.inbound.EventGroup;
16 import org.mule.routing.response.ResponseCorrelationAggregator;
17 import org.mule.samples.loanbroker.esb.message.LoanQuote;
18 import org.mule.umo.UMOEvent;
19 import org.mule.umo.UMOMessage;
20 import org.mule.umo.routing.RoutingException;
21 import org.mule.umo.transformer.TransformerException;
22
23 import java.util.Iterator JavaDoc;
24
25 /**
26  * <code>BankQuotesAggregator</code> receives a number of quotes and selectes the
27  * lowest
28  *
29  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
30  * @version $Revision: 3798 $
31  */

32 public class BankQuotesResponseAggregator extends ResponseCorrelationAggregator
33 {
34     /**
35      * This method is invoked if the shouldAggregate method is called and returns
36      * true. Once this method returns an aggregated message the event group is
37      * removed from the router
38      *
39      * @param events the event group for this request
40      * @return an aggregated message
41      * @throws org.mule.umo.routing.RoutingException if the aggregation fails. in
42      * this scenario the whole event group is removed and passed to the
43      * exception handler for this componenet
44      */

45     protected UMOMessage aggregateEvents(EventGroup events) throws RoutingException
46     {
47         try
48         {
49             LoanQuote lowestQuote = null;
50             LoanQuote quote = null;
51             UMOEvent event = null;
52
53             for (Iterator JavaDoc iterator = events.iterator(); iterator.hasNext();)
54             {
55                 event = (UMOEvent)iterator.next();
56                 quote = (LoanQuote)event.getTransformedMessage();
57                 logger.info("Processing quote: " + quote);
58                 if (lowestQuote == null)
59                 {
60                     lowestQuote = quote;
61                 }
62                 else
63                 {
64                     if (quote.getInterestRate() < lowestQuote.getInterestRate())
65                     {
66                         lowestQuote = quote;
67                     }
68                 }
69             }
70             logger.info("Lowest quote is: " + lowestQuote);
71             return new MuleMessage(lowestQuote, event.getMessage());
72         }
73         catch (TransformerException e)
74         {
75             throw new RoutingException(Message.createStaticMessage("Failed to get lowest quote"),
76                 new MuleMessage(events), null, e);
77         }
78     }
79 }
80
Popular Tags