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.apache.commons.logging.Log; 031 import org.apache.commons.logging.LogFactory; 032 033 public class QueueSubscription extends PrefetchSubscription implements LockOwner { 034 035 private static final Log LOG = LogFactory.getLog(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 if (broker.isExpired(n)) { 054 queue.messageExpired(context, this, node); 055 } else { 056 LOG.debug("ignoring ack " + ack + ", for already expired message: " + n); 057 } 058 return; 059 } 060 queue.removeMessage(context, this, node, ack); 061 } 062 063 protected boolean canDispatch(MessageReference n) throws IOException { 064 boolean result = true; 065 QueueMessageReference node = (QueueMessageReference)n; 066 if (node.isAcked() || node.isDropped()) { 067 result = false; 068 } 069 result = result && (isBrowser() || node.lock(this)); 070 return result; 071 } 072 073 /** 074 * Assigns the message group to this subscription and set the flag on the 075 * message that it is the first message to be dispatched. 076 */ 077 protected void assignGroupToMe(MessageGroupMap messageGroupOwners, MessageReference n, String groupId) throws IOException { 078 messageGroupOwners.put(groupId, info.getConsumerId()); 079 Message message = n.getMessage(); 080 if (message instanceof ActiveMQMessage) { 081 ActiveMQMessage activeMessage = (ActiveMQMessage)message; 082 try { 083 activeMessage.setBooleanProperty("JMSXGroupFirstForConsumer", true, false); 084 } catch (JMSException e) { 085 LOG.warn("Failed to set boolean header: " + e, e); 086 } 087 } 088 } 089 090 public synchronized String toString() { 091 return "QueueSubscription:" + " consumer=" + info.getConsumerId() + ", destinations=" + destinations.size() + ", dispatched=" + dispatched.size() + ", delivered=" 092 + this.prefetchExtension + ", pending=" + getPendingQueueSize(); 093 } 094 095 public int getLockPriority() { 096 return info.getPriority(); 097 } 098 099 public boolean isLockExclusive() { 100 return info.isExclusive(); 101 } 102 103 /** 104 */ 105 public void destroy() { 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 }