View Javadoc

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