KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > inbound > CorrelationAggregator


1 /*
2  * $Id: CorrelationAggregator.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.routing.inbound;
12
13 import org.mule.umo.UMOEvent;
14
15 /**
16  * <code>CorrelationAggregator</code> Uses the CorrelationID and
17  * CorrelationGroupSize properties of the {@link org.mule.umo.UMOMessage} to manage
18  * message groups.
19  *
20  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
21  * @version $Revision: 3798 $
22  */

23 public abstract class CorrelationAggregator extends AbstractEventAggregator
24 {
25
26     /**
27      * Determines if the event group is ready to be aggregated. if the group is ready
28      * to be aggregated (this is entirely up to the application. it could be
29      * determined by volume, last modified time or some oher criteria based on the
30      * last event received)
31      *
32      * @param events
33      * @return
34      */

35     protected boolean shouldAggregate(EventGroup events)
36     {
37         int size = events.expectedSize();
38         if (size == -1)
39         {
40             logger.warn("Correlation Group Size not set, but CorrelationAggregator is being used. Message is being forwarded");
41             return true;
42         }
43         if (logger.isDebugEnabled())
44         {
45             logger.debug("Aggregator: Current Event groups = " + eventGroups.size());
46             logger.debug("correlation size is " + size + ". current event group size is " + events.size()
47                          + " for correlation " + events.getGroupId());
48         }
49         return size == events.size();
50     }
51
52     /**
53      * Adds the event to an event group. Groups are defined by the correlationId on
54      * the message. If no correlationId is set a default group is created for all
55      * events without a correlationId. If there is no group for the current
56      * correlationId one will be created and added to the router.
57      *
58      * @param event
59      * @return
60      */

61     protected EventGroup addEvent(UMOEvent event)
62     {
63         String JavaDoc cId = event.getMessage().getCorrelationId();
64         int groupSize = event.getMessage().getCorrelationGroupSize();
65         if (cId == null)
66         {
67             cId = NO_CORRELATION_ID;
68         }
69         EventGroup eg = (EventGroup)eventGroups.get(cId);
70         if (eg == null)
71         {
72             eg = new EventGroup(cId, groupSize);
73             eg.addEvent(event);
74             eventGroups.put(eg.getGroupId(), eg);
75         }
76         else
77         {
78             eg.addEvent(event);
79         }
80         return eg;
81     }
82 }
83
Popular Tags