View Javadoc

1   /*
2    * $Id: Resequencer.java 19948 2010-10-15 18:11:04Z dfeist $
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  
11  package org.mule.routing;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.config.i18n.CoreMessages;
18  import org.mule.routing.correlation.CorrelationSequenceComparator;
19  import org.mule.routing.correlation.EventCorrelatorCallback;
20  import org.mule.routing.correlation.ResequenceMessagesCorrelatorCallback;
21  
22  import java.util.Comparator;
23  
24  /**
25   * <code>CorrelationEventResequencer</code> is used to resequence events according
26   * to their dispatch sequence in the correlation group. When the MessageSplitter
27   * router splits an event it assigns a correlation sequence to the individual message
28   * parts so that another router such as the <i>CorrelationEventResequencer</i> can
29   * receive the parts and reorder or merge them.
30   * <p>
31   * <b>EIP Reference:</b> <a href="http://www.eaipatterns.com/Resequencer.html">http://www.eaipatterns.com/Resequencer.html<a/>
32   */
33  public class Resequencer extends AbstractAggregator
34  {
35      protected Comparator eventComparator;
36  
37      public Resequencer()
38      {
39          super();
40          this.setEventComparator(new CorrelationSequenceComparator());
41      }
42  
43      @Override
44      public void initialise() throws InitialisationException
45      {
46          if (eventComparator == null)
47          {
48              throw new InitialisationException(CoreMessages.objectIsNull("eventComparator"), this);
49          }
50          super.initialise();
51      }
52  
53      public Comparator getEventComparator()
54      {
55          return eventComparator;
56      }
57  
58      public void setEventComparator(Comparator eventComparator)
59      {
60          this.eventComparator = eventComparator;
61      }
62  
63      protected EventCorrelatorCallback getCorrelatorCallback(MuleContext muleContext)
64      {
65          return new ResequenceMessagesCorrelatorCallback(getEventComparator(), muleContext);
66      }
67      
68      @Override
69      public MuleEvent process(MuleEvent event) throws MuleException
70      {
71          MuleEvent result = eventCorrelator.process(event);
72          if (result == null)
73          {
74              return null;
75          }
76          MuleEvent last = null;
77          for (MuleEvent muleEvent : (MuleEvent[]) result.getMessage().getPayload())
78          {
79              last = processNext(muleEvent);
80          }
81          // Respect existing behaviour by returning last event
82          return last;
83      }
84  
85  }