View Javadoc

1   /*
2    * $Id: ResequenceMessagesCorrelatorCallback.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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  package org.mule.routing.correlation;
11  
12  import org.mule.DefaultMuleEvent;
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleContext;
15  import org.mule.api.MuleEvent;
16  import org.mule.routing.AggregationException;
17  import org.mule.routing.EventGroup;
18  
19  import java.util.Arrays;
20  import java.util.Comparator;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * A Correlator that correlates messages based on Mule correlation settings
27   * Note that the {@link #aggregateEvents(org.mule.routing.EventGroup)} method only resequences the events and
28   * returns an MuleEvent[] wrapped in a MuleMessage impl.  This means that this callback can ONLY be used with a
29   * {@link org.mule.routing.inbound.CorrelationEventResequencer}
30   */
31  public class ResequenceMessagesCorrelatorCallback extends CollectionCorrelatorCallback
32  {
33      /**
34       * logger used by this class
35       */
36      protected transient final Log logger = LogFactory.getLog(ResequenceMessagesCorrelatorCallback.class);
37  
38      protected Comparator eventComparator;
39      protected MuleContext muleContext;
40  
41      public ResequenceMessagesCorrelatorCallback(Comparator eventComparator, MuleContext muleContext)
42      {
43          super(muleContext);
44          this.eventComparator = eventComparator;
45          this.muleContext = muleContext;
46      }
47  
48      /**
49       * This method is invoked if the shouldAggregate method is called and returns
50       * true. Once this method returns an aggregated message, the event group is
51       * removed from the router.
52       *
53       * @param events the event group for this request
54       * @return an aggregated message
55       * @throws org.mule.routing.AggregationException
56       *          if the aggregation fails. in this scenario the
57       *          whole event group is removed and passed to the exception handler
58       *          for this componenet
59       */
60      public MuleEvent aggregateEvents(EventGroup events) throws AggregationException
61      {
62          MuleEvent[] results = (events == null) ? new MuleEvent[0] : events.toArray();
63          Arrays.sort(results, eventComparator);
64          //This is a bit of a hack since we return a collection of events on one message
65          return new DefaultMuleEvent(new DefaultMuleMessage(results, muleContext), results[0]);
66      }
67  
68  }