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 */
017package org.apache.activemq.broker.region;
018
019import java.io.IOException;
020import javax.jms.InvalidSelectorException;
021import org.apache.activemq.broker.Broker;
022import org.apache.activemq.broker.ConnectionContext;
023import org.apache.activemq.command.ConsumerInfo;
024import org.apache.activemq.command.MessageAck;
025import org.apache.activemq.filter.MessageEvaluationContext;
026import org.apache.activemq.usage.SystemUsage;
027
028public class QueueBrowserSubscription extends QueueSubscription {
029
030    int queueRefs;
031    boolean browseDone;
032    boolean destinationsAdded;
033
034    public QueueBrowserSubscription(Broker broker,SystemUsage usageManager, ConnectionContext context, ConsumerInfo info)
035        throws InvalidSelectorException {
036        super(broker,usageManager, context, info);
037    }
038
039    protected boolean canDispatch(MessageReference node) {
040        return !((QueueMessageReference)node).isAcked();
041    }
042
043    public synchronized String toString() {
044        return "QueueBrowserSubscription:" + " consumer=" + info.getConsumerId() + ", destinations="
045               + destinations.size() + ", dispatched=" + dispatched.size() + ", delivered="
046               + this.prefetchExtension + ", pending=" + getPendingQueueSize();
047    }
048
049    synchronized public void destinationsAdded() throws Exception {
050        destinationsAdded = true;
051        checkDone();
052    }
053
054    private void checkDone() throws Exception {
055        if( !browseDone && queueRefs == 0 && destinationsAdded) {
056            browseDone=true;
057            add(QueueMessageReference.NULL_MESSAGE);
058        }
059    }
060
061    public boolean matches(MessageReference node, MessageEvaluationContext context) throws IOException {
062        return !browseDone && super.matches(node, context);
063    }
064
065    /**
066     * Since we are a browser we don't really remove the message from the queue.
067     */
068    protected void acknowledge(ConnectionContext context, final MessageAck ack, final MessageReference n)
069        throws IOException {
070        if (info.isNetworkSubscription()) {
071                super.acknowledge(context, ack, n);
072        }
073    }
074
075    synchronized public void incrementQueueRef() {
076        queueRefs++;        
077    }
078
079    synchronized public void decrementQueueRef() throws Exception {
080        queueRefs--;
081        checkDone();
082    }
083
084}