View Javadoc

1   /*
2    * $Id: RoundRobin.java 20320 2010-11-24 15:03:31Z dfeist $
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;
12  
13  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
14  import org.mule.DefaultMuleEvent;
15  import org.mule.api.MessagingException;
16  import org.mule.api.MuleEvent;
17  import org.mule.api.MuleException;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.endpoint.OutboundEndpoint;
20  import org.mule.api.processor.MessageProcessor;
21  import org.mule.api.routing.CouldNotRouteOutboundMessageException;
22  import org.mule.api.routing.RoutingException;
23  import org.mule.routing.outbound.AbstractOutboundRouter;
24  
25  
26  /**
27   * RoundRobin divides the messages it receives among its target routes in round-robin fashion. This includes messages
28   * received on all threads, so there is no guarantee that messages received from a splitter are sent to consecutively
29   * numbered targets. 
30   */
31  public class RoundRobin extends AbstractOutboundRouter implements MessageProcessor
32  {
33      /** Index of target route to use */
34      AtomicInteger index = new AtomicInteger(0);
35  
36      /**
37       *  Process the event using the next target route in sequence
38       */
39      public MuleEvent route(MuleEvent event) throws MessagingException
40      {
41          int index = getAndIncrementModuloN(routes.size());
42          if (index < 0)
43          {
44              throw new CouldNotRouteOutboundMessageException(event, this);
45          }
46          MessageProcessor mp = routes.get(index);
47          MuleEvent toProcess = event;
48          if (mp instanceof OutboundEndpoint)
49          {
50              toProcess = new DefaultMuleEvent(event.getMessage(), (OutboundEndpoint)mp, event.getSession());
51          }
52          try
53          {
54              return mp.process(toProcess);
55          }
56          catch (MuleException ex)
57          {
58              throw new RoutingException(event, this, ex);
59          }
60      }
61  
62      /**
63       * Get the index of the processor to use
64       */
65      private int getAndIncrementModuloN(int modulus)
66      {
67          if (modulus == 0)
68          {
69              return -1;
70          }
71          while (true)
72          {
73              int lastIndex = index.get();
74              int nextIndex = (lastIndex + 1) % modulus;
75              if (index.compareAndSet(lastIndex, nextIndex))
76              {
77                  return nextIndex;
78              }
79          }
80      }
81  
82      public boolean isMatch(MuleMessage message) throws MuleException
83      {
84          return true;
85      }
86  }