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;
020
021import javax.jms.JMSException;
022
023import org.apache.activemq.broker.Broker;
024import org.apache.activemq.broker.ConnectionContext;
025import org.apache.activemq.broker.region.group.MessageGroupMap;
026import org.apache.activemq.command.ActiveMQMessage;
027import org.apache.activemq.command.ConsumerInfo;
028import org.apache.activemq.command.Message;
029import org.apache.activemq.command.MessageAck;
030import org.apache.activemq.usage.SystemUsage;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034public class QueueSubscription extends PrefetchSubscription implements LockOwner {
035
036    private static final Logger LOG = LoggerFactory.getLogger(QueueSubscription.class);
037
038    public QueueSubscription(Broker broker, SystemUsage usageManager, ConnectionContext context, ConsumerInfo info) throws JMSException {
039        super(broker,usageManager, context, info);
040    }
041
042    /**
043     * In the queue case, mark the node as dropped and then a gc cycle will
044     * remove it from the queue.
045     *
046     * @throws IOException
047     */
048    @Override
049    protected void acknowledge(final ConnectionContext context, final MessageAck ack, final MessageReference n) throws IOException {
050        this.setTimeOfLastMessageAck(System.currentTimeMillis());
051
052        final Destination q = (Destination) n.getRegionDestination();
053        final QueueMessageReference node = (QueueMessageReference)n;
054        final Queue queue = (Queue)q;
055        queue.removeMessage(context, this, node, ack);
056    }
057
058    @Override
059    protected boolean canDispatch(MessageReference n) throws IOException {
060        boolean result = true;
061        QueueMessageReference node = (QueueMessageReference)n;
062        if (node.isAcked() || node.isDropped()) {
063            result = false;
064        }
065        result = result && (isBrowser() || node.lock(this));
066        return result;
067    }
068
069    @Override
070    public synchronized String toString() {
071        return "QueueSubscription:" + " consumer=" + info.getConsumerId() + ", destinations=" + destinations.size() + ", dispatched=" + dispatched.size() + ", delivered="
072               + this.prefetchExtension + ", pending=" + getPendingQueueSize() + ", prefetch=" + getPrefetchSize() + ", prefetchExtension=" + prefetchExtension.get();
073    }
074
075    @Override
076    public int getLockPriority() {
077        return info.getPriority();
078    }
079
080    @Override
081    public boolean isLockExclusive() {
082        return info.isExclusive();
083    }
084
085    /**
086     */
087    @Override
088    public void destroy() {
089        setSlowConsumer(false);
090    }
091
092
093    @Override
094    protected boolean isDropped(MessageReference node) {
095       boolean result = false;
096       if(node instanceof IndirectMessageReference) {
097           QueueMessageReference qmr = (QueueMessageReference) node;
098           result = qmr.isDropped();
099       }
100       return result;
101    }
102
103}