KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > region > policy > RoundRobinDispatchPolicy


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.broker.region.policy;
19
20
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import org.apache.activemq.broker.region.MessageReference;
24 import org.apache.activemq.broker.region.Subscription;
25 import org.apache.activemq.filter.MessageEvaluationContext;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * Simple dispatch policy that sends a message to every subscription that
31  * matches the message.
32  *
33  * @org.apache.xbean.XBean
34  *
35  * @version $Revision$
36  */

37 public class RoundRobinDispatchPolicy implements DispatchPolicy {
38     static final Log log=LogFactory.getLog(RoundRobinDispatchPolicy.class);
39     
40     /**
41      * @param node
42      * @param msgContext
43      * @param consumers
44      * @return true if dispatched
45      * @throws Exception
46      * @see org.apache.activemq.broker.region.policy.DispatchPolicy#dispatch(org.apache.activemq.broker.region.MessageReference, org.apache.activemq.filter.MessageEvaluationContext, java.util.List)
47      */

48     public boolean dispatch(MessageReference node, MessageEvaluationContext msgContext, List JavaDoc consumers) throws Exception JavaDoc {
49         
50         // Big synch here so that only 1 message gets dispatched at a time. Ensures
51
// Everyone sees the same order and that the consumer list is not used while
52
// it's being rotated.
53
synchronized(consumers) {
54             int count = 0;
55             
56             Subscription firstMatchingConsumer = null;
57             for (Iterator JavaDoc iter = consumers.iterator(); iter.hasNext();) {
58                 Subscription sub = (Subscription) iter.next();
59                 
60                 // Only dispatch to interested subscriptions
61
if (!sub.matches(node, msgContext))
62                     continue;
63                 
64                 if (firstMatchingConsumer == null) {
65                     firstMatchingConsumer = sub;
66                 }
67                 
68                 sub.add(node);
69                 count++;
70             }
71             
72             if (firstMatchingConsumer != null) {
73             // Rotate the consumer list.
74
try {
75                  consumers.remove(firstMatchingConsumer);
76                  consumers.add(firstMatchingConsumer);
77                 } catch (Throwable JavaDoc bestEffort) { }
78                              }
79             return count > 0;
80         }
81     }
82
83
84 }
85
Popular Tags