home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

ActiveMQ example source code file (RoundRobinDispatchPolicy.java)

This example ActiveMQ source code file (RoundRobinDispatchPolicy.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - ActiveMQ tags/keywords

dispatchpolicy, exception, exception, iterator, iterator, list, log, logger, messageevaluationcontext, messageevaluationcontext, roundrobindispatchpolicy, subscription, subscription, throwable, util

The ActiveMQ RoundRobinDispatchPolicy.java source code

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

import java.util.Iterator;
import java.util.List;
import org.apache.activemq.broker.region.MessageReference;
import org.apache.activemq.broker.region.Subscription;
import org.apache.activemq.filter.MessageEvaluationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Simple dispatch policy that sends a message to every subscription that
 * matches the message.
 * 
 * @org.apache.xbean.XBean
 * 
 */
public class RoundRobinDispatchPolicy implements DispatchPolicy {
    static final Logger LOG = LoggerFactory.getLogger(RoundRobinDispatchPolicy.class);

    /**
     * @param node
     * @param msgContext
     * @param consumers
     * @return true if dispatched
     * @throws Exception
     * @see org.apache.activemq.broker.region.policy.DispatchPolicy#dispatch(org.apache.activemq.broker.region.MessageReference,
     *      org.apache.activemq.filter.MessageEvaluationContext, java.util.List)
     */
    public boolean dispatch(MessageReference node,
            MessageEvaluationContext msgContext, List<Subscription> consumers)
            throws Exception {
        int count = 0;

        Subscription firstMatchingConsumer = null;
        synchronized (consumers) {
            for (Iterator<Subscription> iter = consumers.iterator(); iter
                    .hasNext();) {
                Subscription sub = iter.next();

                // Only dispatch to interested subscriptions
                if (!sub.matches(node, msgContext)) {
                    sub.unmatched(node);
                    continue;
                }

                if (firstMatchingConsumer == null) {
                    firstMatchingConsumer = sub;
                }

                sub.add(node);
                count++;
            }

            if (firstMatchingConsumer != null) {
                // Rotate the consumer list.
                try {
                    consumers.remove(firstMatchingConsumer);
                    consumers.add(firstMatchingConsumer);
                } catch (Throwable bestEffort) {
                }
            }
        }
        return count > 0;
    }
}

Other ActiveMQ examples (source code examples)

Here is a short list of links related to this ActiveMQ RoundRobinDispatchPolicy.java source code file:

new blog posts

 

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.