001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.broker.region.policy;
018    
019    import java.util.Iterator;
020    import java.util.List;
021    import org.apache.activemq.broker.region.MessageReference;
022    import org.apache.activemq.broker.region.Subscription;
023    import org.apache.activemq.filter.MessageEvaluationContext;
024    
025    /**
026     * Dispatch policy that causes every subscription to see messages in the same
027     * order.
028     * 
029     * @org.apache.xbean.XBean
030     * 
031     */
032    public class StrictOrderDispatchPolicy implements DispatchPolicy {
033    
034        /**
035         * @param node
036         * @param msgContext
037         * @param consumers
038         * @return true if dispatched
039         * @throws Exception
040         * @see org.apache.activemq.broker.region.policy.DispatchPolicy#dispatch(org.apache.activemq.broker.region.MessageReference,
041         *      org.apache.activemq.filter.MessageEvaluationContext, java.util.List)
042         */
043        public boolean dispatch(MessageReference node, MessageEvaluationContext msgContext, List consumers) throws Exception {
044            // Big synch here so that only 1 message gets dispatched at a time.
045            // Ensures
046            // Everyone sees the same order.
047            synchronized (consumers) {
048                int count = 0;
049                for (Iterator iter = consumers.iterator(); iter.hasNext();) {
050                    Subscription sub = (Subscription)iter.next();
051    
052                    // Only dispatch to interested subscriptions
053                    if (!sub.matches(node, msgContext)) {
054                        sub.unmatched(node);
055                        continue;
056                    }
057    
058                    sub.add(node);
059                    count++;
060                }
061                return count > 0;
062            }
063        }
064    
065    }