Package org.codehaus.activemq.service.boundedvm

Source Code of org.codehaus.activemq.service.boundedvm.TransientTopicBoundedMessageContainer

/**
*
* Copyright 2004 Protique Ltd
*
* Licensed 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.codehaus.activemq.service.boundedvm;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jms.JMSException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.broker.BrokerClient;
import org.codehaus.activemq.filter.Filter;
import org.codehaus.activemq.message.ActiveMQMessage;
import org.codehaus.activemq.message.ConsumerInfo;
import org.codehaus.activemq.message.util.BoundedPacketQueue;
import org.codehaus.activemq.service.Service;
import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;

/**
* A MessageContainer for transient topics One of these exists for every active Connection consuming transient Topic
* messages
*
* @version $Revision: 1.2 $
*/
public class TransientTopicBoundedMessageContainer implements Service, Runnable {
    private SynchronizedBoolean started;
    private BrokerClient client;
    private BoundedPacketQueue queue;
    private Thread worker;
    private CopyOnWriteArrayList subscriptions;
    private Log log;

    /**
     * Construct this beast
     *
     * @param client
     * @param queue
     */
    public TransientTopicBoundedMessageContainer(BrokerClient client, BoundedPacketQueue queue) {
        this.client = client;
        this.queue = queue;
        this.started = new SynchronizedBoolean(false);
        this.subscriptions = new CopyOnWriteArrayList();
        this.log = LogFactory.getLog("TransientTopicBoundedMessageContainer for client: " + client);
    }

    /**
     * @return true if this Container has no active subscriptions
     */
    public boolean isInactive() {
        return subscriptions.isEmpty();
    }

    /**
     * Add a consumer to dispatch messages to
     *
     * @param filter
     * @param info
     */
    public void addConsumer(Filter filter, ConsumerInfo info) {
        TransientTopicSubscription ts = findMatch(info);
        if (ts == null) {
            ts = new TransientTopicSubscription(filter, info);
            subscriptions.add(ts);
        }
    }

    /**
     * Remove a consumer
     *
     * @param info
     */
    public void removeConsumer(ConsumerInfo info) {
        TransientTopicSubscription ts = findMatch(info);
        if (ts != null) {
            subscriptions.remove(ts);
        }
    }

    /**
     * start working
     */
    public void start() {
        if (started.commit(false, true)) {
            worker = new Thread(this);
            worker.setPriority(Thread.NORM_PRIORITY + 1);
            worker.start();
        }
    }

    /**
     * See if this container should get this message and dispatch it
     *
     * @param message
     * @return true if it is a valid container
     * @throws JMSException
     */
    public boolean targetAndDispatch(ActiveMQMessage message) throws JMSException {
        boolean result = false;
        List tmpList = null;
        for (Iterator i = subscriptions.iterator();i.hasNext();) {
            TransientTopicSubscription ts = (TransientTopicSubscription) i.next();
            if (ts.isTarget(message)) {
                if (tmpList == null) {
                    tmpList = new ArrayList();
                }
                tmpList.add(ts);
            }
        }
        dispatchToQueue(message, tmpList);
        return tmpList != null;
    }

    /**
     * stop working
     */
    public void stop() {
        started.set(false);
        queue.clear();
    }

    /**
     * close down this container
     */
    public void close() {
        if(started.get()) {
          stop();
        }
        queue.close();
    }

    /**
     * do some dispatching
     */
    public void run() {
        int count = 0;
        while (started.get()) {
            try {
                ActiveMQMessage message = (ActiveMQMessage) queue.dequeue(2000);
                if (message != null) {
                    client.dispatch(message);
                    if (++count == 250) {
                        count = 0;
                        Thread.yield();
                    }
                }
            }
            catch (Exception e) {
                stop();
                log.warn("stop dispatching", e);
            }
        }
    }

    private void dispatchToQueue(ActiveMQMessage message, List list) throws JMSException {
        if (list != null) {
            int[] ids = new int[list.size()];
            for (int i = 0;i < list.size();i++) {
                TransientTopicSubscription ts = (TransientTopicSubscription) list.get(i);
                ids[i] = ts.getConsumerInfo().getConsumerNo();
            }
            message.setConsumerNos(ids);
            try {
                queue.enqueue(message);
            }
            catch (InterruptedException e) {
                log.warn("queue interuppted, closing", e);
                close();
            }
        }
    }

    private TransientTopicSubscription findMatch(ConsumerInfo info) {
        TransientTopicSubscription result = null;
        for (Iterator i = subscriptions.iterator();i.hasNext();) {
            TransientTopicSubscription ts = (TransientTopicSubscription) i.next();
            if (ts.getConsumerInfo().equals(info)) {
                result = ts;
                break;
            }
        }
        return result;
    }
}
TOP

Related Classes of org.codehaus.activemq.service.boundedvm.TransientTopicBoundedMessageContainer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.