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