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 */
017 package org.apache.activemq.broker.region;
018
019 import java.io.IOException;
020 import javax.jms.InvalidSelectorException;
021 import javax.jms.JMSException;
022 import org.apache.activemq.broker.Broker;
023 import org.apache.activemq.broker.ConnectionContext;
024 import org.apache.activemq.broker.region.group.MessageGroupMap;
025 import org.apache.activemq.command.ActiveMQMessage;
026 import org.apache.activemq.command.ConsumerInfo;
027 import org.apache.activemq.command.Message;
028 import org.apache.activemq.command.MessageAck;
029 import org.apache.activemq.usage.SystemUsage;
030 import org.slf4j.Logger;
031 import org.slf4j.LoggerFactory;
032
033 public class QueueSubscription extends PrefetchSubscription implements LockOwner {
034
035 private static final Logger LOG = LoggerFactory.getLogger(QueueSubscription.class);
036
037 public QueueSubscription(Broker broker, SystemUsage usageManager, ConnectionContext context, ConsumerInfo info) throws InvalidSelectorException {
038 super(broker,usageManager, context, info);
039 }
040
041 /**
042 * In the queue case, mark the node as dropped and then a gc cycle will
043 * remove it from the queue.
044 *
045 * @throws IOException
046 */
047 protected void acknowledge(final ConnectionContext context, final MessageAck ack, final MessageReference n) throws IOException {
048 final Destination q = n.getRegionDestination();
049 final QueueMessageReference node = (QueueMessageReference)n;
050 final Queue queue = (Queue)q;
051
052 if (n.isExpired()) {
053 // sync with message expiry processing
054 if (!broker.isExpired(n)) {
055 LOG.warn("ignoring ack " + ack + ", for already expired message: " + n);
056 return;
057 }
058 }
059 queue.removeMessage(context, this, node, ack);
060 }
061
062 protected boolean canDispatch(MessageReference n) throws IOException {
063 boolean result = true;
064 QueueMessageReference node = (QueueMessageReference)n;
065 if (node.isAcked() || node.isDropped()) {
066 result = false;
067 }
068 result = result && (isBrowser() || node.lock(this));
069 return result;
070 }
071
072 /**
073 * Assigns the message group to this subscription and set the flag on the
074 * message that it is the first message to be dispatched.
075 */
076 protected void assignGroupToMe(MessageGroupMap messageGroupOwners, MessageReference n, String groupId) throws IOException {
077 messageGroupOwners.put(groupId, info.getConsumerId());
078 Message message = n.getMessage();
079 if (message instanceof ActiveMQMessage) {
080 ActiveMQMessage activeMessage = (ActiveMQMessage)message;
081 try {
082 activeMessage.setBooleanProperty("JMSXGroupFirstForConsumer", true, false);
083 } catch (JMSException e) {
084 LOG.warn("Failed to set boolean header: " + e, e);
085 }
086 }
087 }
088
089 public synchronized String toString() {
090 return "QueueSubscription:" + " consumer=" + info.getConsumerId() + ", destinations=" + destinations.size() + ", dispatched=" + dispatched.size() + ", delivered="
091 + this.prefetchExtension + ", pending=" + getPendingQueueSize();
092 }
093
094 public int getLockPriority() {
095 return info.getPriority();
096 }
097
098 public boolean isLockExclusive() {
099 return info.isExclusive();
100 }
101
102 /**
103 */
104 public void destroy() {
105 setSlowConsumer(false);
106 }
107
108
109 protected boolean isDropped(MessageReference node) {
110 boolean result = false;
111 if(node instanceof IndirectMessageReference) {
112 QueueMessageReference qmr = (QueueMessageReference) node;
113 result = qmr.isDropped();
114 }
115 return result;
116 }
117
118 }