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 javax.jms.JMSException;
020    
021    import org.apache.activemq.broker.BrokerService;
022    import org.apache.activemq.broker.ConnectionContext;
023    import org.apache.activemq.command.ActiveMQDestination;
024    import org.apache.activemq.command.ConsumerInfo;
025    import org.apache.activemq.command.MessageDispatchNotification;
026    import org.apache.activemq.thread.TaskRunnerFactory;
027    import org.apache.activemq.usage.SystemUsage;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    /**
032     * @version $Revision: 1.7 $
033     */
034    public class TempQueueRegion extends AbstractTempRegion {
035        private static final Log LOG = LogFactory.getLog(TempQueueRegion.class);
036        private final BrokerService brokerService;
037        
038        public TempQueueRegion(RegionBroker broker, BrokerService brokerService, DestinationStatistics destinationStatistics, SystemUsage memoryManager, TaskRunnerFactory taskRunnerFactory,
039                               DestinationFactory destinationFactory) {
040            super(broker, destinationStatistics, memoryManager, taskRunnerFactory, destinationFactory);
041            // We should allow the following to be configurable via a Destination
042            // Policy
043            // setAutoCreateDestinations(false);
044            this.brokerService = brokerService;
045        }
046    
047        protected Destination doCreateDestination(ConnectionContext context, ActiveMQDestination destination) throws Exception {  
048            TempQueue result = new TempQueue(brokerService, destination, null, destinationStatistics, taskRunnerFactory);
049            result.initialize();
050            return result;
051        }
052    
053        protected Subscription createSubscription(ConnectionContext context, ConsumerInfo info) throws JMSException {
054            if (info.isBrowser()) {
055                return new QueueBrowserSubscription(broker,usageManager,context, info);
056            } else {
057                return new QueueSubscription(broker,usageManager,context, info);
058            }
059        }
060    
061        public String toString() {
062            return "TempQueueRegion: destinations=" + destinations.size() + ", subscriptions=" + subscriptions.size() + ", memory=" + usageManager.getMemoryUsage().getPercentUsage() + "%";
063        }
064    
065        public void removeDestination(ConnectionContext context, ActiveMQDestination destination, long timeout) throws Exception {
066    
067            // Force a timeout value so that we don't get an error that
068            // there is still an active sub. Temp destination may be removed
069            // while a network sub is still active which is valid.
070            if (timeout == 0) {
071                timeout = 1;
072            }
073    
074            super.removeDestination(context, destination, timeout);
075        }
076        
077        /*
078         * For a Queue, dispatch order is imperative to match acks, so the dispatch is deferred till 
079         * the notification to ensure that the subscription chosen by the master is used.
080         * 
081         * (non-Javadoc)
082         * @see org.apache.activemq.broker.region.AbstractRegion#processDispatchNotification(org.apache.activemq.command.MessageDispatchNotification)
083         */
084        public void processDispatchNotification(MessageDispatchNotification messageDispatchNotification) throws Exception {
085            processDispatchNotificationViaDestination(messageDispatchNotification);
086        }
087    
088    }