View Javadoc

1   /*
2    * $Id: ResponseCorrelationAggregator.java 11531 2008-04-08 15:34:34Z rossmason $
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 CPAL v1.0
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.response;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.routing.RoutingException;
16  import org.mule.routing.inbound.EventGroup;
17  import org.mule.routing.EventCorrelatorCallback;
18  
19  /**
20   * <code>ResponseCorrelationAggregator</code> Correlates one or more events on a
21   * response flow using the Correlation Id to group events.
22   */
23  
24  public abstract class ResponseCorrelationAggregator extends AbstractResponseAggregator
25  {
26  
27      protected EventCorrelatorCallback getCorrelatorCallback()
28      {
29          return new DefaultEventCorrelatorCallback();
30      }
31  
32      public class DefaultEventCorrelatorCallback implements EventCorrelatorCallback
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.routing.AggregationException
42           *          if the aggregation fails. in this scenario the
43           *          whole event group is removed and passed to the exception handler
44           *          for this component
45           */
46          public MuleMessage aggregateEvents(EventGroup events) throws RoutingException
47          {
48              return ResponseCorrelationAggregator.this.aggregateEvents(events);
49          }
50  
51          /**
52           * Determines if the event group is ready to be aggregated. if the group is ready
53           * to be aggregated (this is entirely up to the application. it could be
54           * determined by volume, last modified time or some oher criteria based on the
55           * last event received)
56           *
57           * @param events
58           * @return true if the event group is ready of aggregation
59           */
60          public boolean shouldAggregateEvents(EventGroup events)
61          {
62              int expected = events.expectedSize();
63              int current = events.size();
64  
65              if (expected == -1)
66              {
67                  logger.warn("Correlation Group Size not set, but CorrelationAggregator is being used.  Message is being forwarded");
68                  return true;
69              }
70  
71              if (logger.isDebugEnabled())
72              {
73                  logger.debug("Correlation size is " + expected + ", current event group size is " + current
74                          + " for correlation group " + events.getGroupId());
75              }
76  
77              return expected == current;
78          }
79  
80          public EventGroup createEventGroup(MuleEvent event, Object id)
81          {
82              return new EventGroup(id, event.getMessage().getCorrelationGroupSize());
83      }
84      }
85  }