KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > samples > loanbroker > routers > BankQuotesAggregator


1 /*
2  * $Id: BankQuotesAggregator.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.routers;
12
13 import org.mule.config.i18n.Message;
14 import org.mule.impl.MuleMessage;
15 import org.mule.routing.AggregationException;
16 import org.mule.routing.inbound.CorrelationAggregator;
17 import org.mule.routing.inbound.EventGroup;
18 import org.mule.samples.loanbroker.LoanQuote;
19 import org.mule.umo.UMOEvent;
20 import org.mule.umo.UMOMessage;
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 BankQuotesAggregator extends CorrelationAggregator
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 AggregationException if the aggregation fails. in this scenario the
42      * whole event group is removed and passed to the exception handler
43      * for this componenet
44      */

45     protected UMOMessage aggregateEvents(EventGroup events) throws AggregationException
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
59                 if (lowestQuote == null)
60                 {
61                     lowestQuote = quote;
62                 }
63                 else
64                 {
65                     if (quote.getInterestRate() < lowestQuote.getInterestRate())
66                     {
67                         lowestQuote = quote;
68                     }
69                 }
70             }
71
72             return new MuleMessage(lowestQuote, event.getMessage());
73         }
74         catch (TransformerException e)
75         {
76             throw new AggregationException(Message.createStaticMessage("Failed to get lowest quote"), events,
77                 null, e);
78         }
79     }
80
81     /**
82      * Determines if the event group is ready to be aggregated. if the group is ready
83      * to be aggregated (this is entirely up to the application. it could be
84      * determined by volume, last modified time or some oher criteria based on the
85      * last event received)
86      *
87      * @param events
88      * @return true if the events are ready to be aggregated
89      */

90     protected boolean shouldAggregate(EventGroup events)
91     {
92         return super.shouldAggregate(events);
93     }
94 }
95
Popular Tags