Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » broker » region » policy » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.broker.region.policy;
   18   
   19   import java.util.Iterator;
   20   import java.util.List;
   21   import org.apache.activemq.broker.region.MessageReference;
   22   import org.apache.activemq.broker.region.Subscription;
   23   import org.apache.activemq.filter.MessageEvaluationContext;
   24   import org.apache.commons.logging.Log;
   25   import org.apache.commons.logging.LogFactory;
   26   
   27   /**
   28    * Simple dispatch policy that sends a message to every subscription that
   29    * matches the message.
   30    * 
   31    * @org.apache.xbean.XBean
   32    * @version $Revision$
   33    */
   34   public class RoundRobinDispatchPolicy implements DispatchPolicy {
   35       static final Log LOG = LogFactory.getLog(RoundRobinDispatchPolicy.class);
   36   
   37       /**
   38        * @param node
   39        * @param msgContext
   40        * @param consumers
   41        * @return true if dispatched
   42        * @throws Exception
   43        * @see org.apache.activemq.broker.region.policy.DispatchPolicy#dispatch(org.apache.activemq.broker.region.MessageReference,
   44        *      org.apache.activemq.filter.MessageEvaluationContext, java.util.List)
   45        */
   46       public boolean dispatch(MessageReference node,
   47               MessageEvaluationContext msgContext, List<Subscription> consumers)
   48               throws Exception {
   49           int count = 0;
   50   
   51           Subscription firstMatchingConsumer = null;
   52           synchronized (consumers) {
   53               for (Iterator<Subscription> iter = consumers.iterator(); iter
   54                       .hasNext();) {
   55                   Subscription sub = iter.next();
   56   
   57                   // Only dispatch to interested subscriptions
   58                   if (!sub.matches(node, msgContext)) {
   59                       continue;
   60                   }
   61   
   62                   if (firstMatchingConsumer == null) {
   63                       firstMatchingConsumer = sub;
   64                   }
   65   
   66                   sub.add(node);
   67                   count++;
   68               }
   69   
   70               if (firstMatchingConsumer != null) {
   71                   // Rotate the consumer list.
   72                   try {
   73                       consumers.remove(firstMatchingConsumer);
   74                       consumers.add(firstMatchingConsumer);
   75                   } catch (Throwable bestEffort) {
   76                   }
   77               }
   78           }
   79           return count > 0;
   80       }
   81   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » broker » region » policy » [javadoc | source]